• 加解密算法一:散列算法、对称加解密


    .Net中的加解密操作所涉及的对象都在命名空间System.Security.Cryptography下,
    所以应先在程序中添加using System.Security.Cryptography

    1、散列算法:

      用来产生一些数据片段(例如消息或会话项)的散列值的算法。好的散列算法具有在输入数据中的更改可以更改结果散列值中每个比特的特性;因此,散列对于检测在诸如消息等大型信息对象中的任何变化很有用。此外,好的散列算法使得构造两个独立的有相同散列的输入不能通过计算方法实现。

      典型的散列算法包括 MD2、MD4、MD5 和 SHA-1。散列算法也被称为散列函数。散列算法就是争取一个萝卜一个坑的原则。

    1.1、MD5:

         /// <summary>
            /// MD5方式加密字符串的方法
            /// </summary>
            /// <param name="source">要进行加密的字符串</param>
            /// <returns>加密后的字符串</returns>
            public static string MD5Encrypt(string source)
            {
                try
                {
                    MD5 md5 = new MD5CryptoServiceProvider();
                    byte[] result = md5.ComputeHash(Encoding.Default.GetBytes(source));
                    string returnResult = "";
                    for (int i = 0; i < result.Length; i++)
                    {
                        returnResult += result[i].ToString("x");
                    }
                    return returnResult;
                }
                catch (Exception ex)
                {
                    throw new Exception("MD5方式加密字符串失败。错误信息:" + ex.Message);
                }
            }

    1.2、SHA1:

          /// <summary>
            /// SHA1方式加密字符串的方法
            /// </summary>
            /// <param name="source">要进行加密的字符串</param>
            /// <returns>加密后的字符串</returns>
            public static string SHA1Encrypt(string source)
            {
                try
                {
                    SHA1 sha1 = new SHA1CryptoServiceProvider();
                    byte[] result = sha1.ComputeHash(Encoding.Default.GetBytes(source));
                    string returnResult = "";
                    for (int i = 0; i < result.Length; i++)
                    {
                        returnResult += result[i].ToString("x");
                    }
                    return returnResult;
                }
                catch (Exception ex)
                {
                    throw new Exception("SHA1方式加密字符串失败。错误信息:" + ex.Message);
                }
            }

    2、对称加解密:

      发送者把原文通过一个加密的算法,用密钥进行加密后将密文发送给接收者;接收者再用这个密钥对密文进行解密,得到原文。由于常用的加密算法都是公开的,所以对原文的加密的关键就是密钥了。对于这种加解密都使用同样的密钥的算法,我们称之为对称加密,对称加密的代表算法就是DES算法。

      对称加解密的缺陷:由于加解密使用相同的密钥,那么这个密钥最少要保存在两个地方,如果加密的数据要发给多人,那么就会有很多人知道密钥,大大增加了密钥泄露的风险;并且密钥需要由发送方传递给接收方,那么如何保证密钥的传递的安全,则成了另外一个头疼的事情。为了解决这个问题,相对于对称加密,又出现了非对称加密。

            /// <summary>
            /// DES加密密钥
            /// </summary>
            public const string m_EncryptKey = "auto@#$&";
         /// <summary>
            /// DES方式加密字符串的方法
            /// </summary>
            /// <param name="source">要进行加密的字符串</param>
            /// <returns>加密后的字符串</returns>
            public static string DesEncrypt(string source)
            {
                byte[] byKey = null;
                byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
                try
                {
                    byKey = Encoding.Default.GetBytes(m_EncryptKey.Substring(0, 8));
                    DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                    byte[] inputByteArray = Encoding.Default.GetBytes(source);
                    MemoryStream ms = new MemoryStream();
                    CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);
                    cs.Write(inputByteArray, 0, inputByteArray.Length);
                    cs.FlushFinalBlock();
                    return Convert.ToBase64String(ms.ToArray());
                }
                catch (Exception ex)
                {
                    throw new Exception("DES方式加密字符串失败。错误信息:" + ex.Message);
                }
            }
          /// <summary>
            /// DES方式解密字符串的方法
            /// </summary>
            /// <param name="source">要进行解密的字符串</param>
            /// <returns>解密后的字符串</returns>
            public static string DesDecrypt(string source)
            {
                byte[] byKey = null;
                byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
                byte[] inputByteArray = new Byte[source.Length];
                try
                {
                    byKey = Encoding.Default.GetBytes(m_EncryptKey.Substring(0, 8));
                    DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                    inputByteArray = Convert.FromBase64String(source);
                    MemoryStream ms = new MemoryStream();
                    CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
                    cs.Write(inputByteArray, 0, inputByteArray.Length);
                    cs.FlushFinalBlock();
                    return Encoding.Default.GetString(ms.ToArray());
                }
                catch (Exception ex)
                {
                    throw new Exception("DES方式解密字符串失败。错误信息:" + ex.Message);
                }
            }

     

  • 相关阅读:
    input 标签取消readOnly属性
    python selenium 跑脚本的时候按钮无法点击的解决办法
    Python Selenium 调用IE浏览器失败Unexpected error launching Internet Explorer解决方法
    转载--Python random模块(获取随机数)常用方法和使用例子
    转载--python selenium实现文件、图片上传
    ieDriver启动IE浏览器显示This is the initial start page for the WebDriver server的解决办法
    自动化测试用例设计学习心得总结
    关于selene安装插件ide不能识别插件的问题解决办法
    cmd 启动mysql
    最大子序列
  • 原文地址:https://www.cnblogs.com/yaosuc/p/4545651.html
Copyright © 2020-2023  润新知