• C#简单的加密类


    AesManaged - 高级加密标准(AES) 对称算法的管理类

     1  public static class EncryptAndDecrypt
     2     {
     3         //加密
     4         public static string Encrypt(string input)
     5         {
     6             // 盐值
     7             string saltValue = "saltValue";
     8             // 密码值
     9             string pwdValue = "pwdValue";
    10             byte[] data = System.Text.UTF8Encoding.UTF8.GetBytes(input);
    11             byte[] salt = System.Text.UTF8Encoding.UTF8.GetBytes(saltValue);
    12             // AesManaged - 高级加密标准(AES) 对称算法的管理类
    13             System.Security.Cryptography.AesManaged aes = new System.Security.Cryptography.AesManaged();
    14             // Rfc2898DeriveBytes - 通过使用基于 HMACSHA1 的伪随机数生成器,实现基于密码的密钥派生功能 (PBKDF2 - 一种基于密码的密钥派生函数)
    15             // 通过 密码 和 salt 派生密钥
    16             System.Security.Cryptography.Rfc2898DeriveBytes rfc = new System.Security.Cryptography.Rfc2898DeriveBytes(pwdValue, salt);
    17             /**/
    18             /*
    19          * AesManaged.BlockSize - 加密操作的块大小(单位:bit)
    20          * AesManaged.LegalBlockSizes - 对称算法支持的块大小(单位:bit)
    21          * AesManaged.KeySize - 对称算法的密钥大小(单位:bit)
    22          * AesManaged.LegalKeySizes - 对称算法支持的密钥大小(单位:bit)
    23          * AesManaged.Key - 对称算法的密钥
    24          * AesManaged.IV - 对称算法的密钥大小
    25          * Rfc2898DeriveBytes.GetBytes(int 需要生成的伪随机密钥字节数) - 生成密钥
    26          */
    27             aes.BlockSize = aes.LegalBlockSizes[0].MaxSize;
    28             aes.KeySize = aes.LegalKeySizes[0].MaxSize;
    29             aes.Key = rfc.GetBytes(aes.KeySize / 8);
    30             aes.IV = rfc.GetBytes(aes.BlockSize / 8);
    31             // 用当前的 Key 属性和初始化向量 IV 创建对称加密器对象
    32             System.Security.Cryptography.ICryptoTransform encryptTransform = aes.CreateEncryptor();
    33             // 加密后的输出流
    34             System.IO.MemoryStream encryptStream = new System.IO.MemoryStream();
    35             // 将加密后的目标流(encryptStream)与加密转换(encryptTransform)相连接
    36             System.Security.Cryptography.CryptoStream encryptor = new System.Security.Cryptography.CryptoStream
    37                 (encryptStream, encryptTransform, System.Security.Cryptography.CryptoStreamMode.Write);
    38             // 将一个字节序列写入当前 CryptoStream (完成加密的过程)
    39             encryptor.Write(data, 0, data.Length);
    40             encryptor.Close();
    41             // 将加密后所得到的流转换成字节数组,再用Base64编码将其转换为字符串
    42             string encryptedString = Convert.ToBase64String(encryptStream.ToArray());
    43             return encryptedString;
    44         }
    45 
    46 
    47         #region silverlight密码解密
    48         /**/
    49         /// <summary>
    50         /// 解密数据
    51         /// </summary>
    52         /// <param name="input">加密后的字符串</param>
    53         /// <returns>加密前的字符串</returns>
    54         public static string Decrypt(string input)
    55         {
    56             // 盐值(与加密时设置的值一致)
    57             string saltValue = "saltValue";
    58             // 密码值(与加密时设置的值一致)
    59             string pwdValue = "pwdValue";
    60             byte[] encryptBytes = Convert.FromBase64String(input);
    61             byte[] salt = Encoding.UTF8.GetBytes(saltValue);
    62             System.Security.Cryptography.AesManaged aes = new System.Security.Cryptography.AesManaged();
    63             System.Security.Cryptography.Rfc2898DeriveBytes rfc = new System.Security.Cryptography.Rfc2898DeriveBytes(pwdValue, salt);
    64             aes.BlockSize = aes.LegalBlockSizes[0].MaxSize;
    65             aes.KeySize = aes.LegalKeySizes[0].MaxSize;
    66             aes.Key = rfc.GetBytes(aes.KeySize / 8);
    67             aes.IV = rfc.GetBytes(aes.BlockSize / 8);
    68             // 用当前的 Key 属性和初始化向量 IV 创建对称解密器对象
    69             System.Security.Cryptography.ICryptoTransform decryptTransform = aes.CreateDecryptor();
    70             // 解密后的输出流
    71             MemoryStream decryptStream = new MemoryStream();
    72             // 将解密后的目标流(decryptStream)与解密转换(decryptTransform)相连接
    73             System.Security.Cryptography.CryptoStream decryptor = new System.Security.Cryptography.CryptoStream(
    74                 decryptStream, decryptTransform, System.Security.Cryptography.CryptoStreamMode.Write);
    75             // 将一个字节序列写入当前 CryptoStream (完成解密的过程)
    76             decryptor.Write(encryptBytes, 0, encryptBytes.Length);
    77             decryptor.Close();
    78             // 将解密后所得到的流转换为字符串
    79             byte[] decryptBytes = decryptStream.ToArray();
    80             string decryptedString = UTF8Encoding.UTF8.GetString(decryptBytes, 0, decryptBytes.Length);
    81             return decryptedString;
    82         }
    83         #endregion
    84     }
  • 相关阅读:
    Servlet(九):web.xml文件和server.xml文件
    Servlet(八):ServletContext对象和ServletConfig对象
    Servlet(七):session
    Servlet(六):Cookie
    Servlet(五):请求转发和重定向
    Servlet(四):request和response对象
    Servlet(三):生命周期、常用方法、常见错误
    【php】在Windows2003下的IIS配置php5.4
    Spring AOP监控SQL运行
    算法导论—无向图的遍历(BFS+DFS,MATLAB)
  • 原文地址:https://www.cnblogs.com/zxbzl/p/2942939.html
Copyright © 2020-2023  润新知