首页 > 代码库 > C#简单的加密类

C#简单的加密类

1.加密

 1 public class EncryptHepler { 2         // 验值  3         static string saltValue = http://www.mamicode.com/"XXXX"; 4         // 密码值  5         static string pwdValue = http://www.mamicode.com/"XXXX"; 6   7         /// <summary> 8         /// 加密 9         /// </summary>10         public static string Encrypt( string input ) {11             byte[ ] data =http://www.mamicode.com/ System.Text.UTF8Encoding.UTF8.GetBytes( input );12             byte[ ] salt = System.Text.UTF8Encoding.UTF8.GetBytes( saltValue );13  14             // AesManaged - 高级加密标准(AES) 对称算法的管理类 15             System.Security.Cryptography.AesManaged aes = new System.Security.Cryptography.AesManaged( );16             // Rfc2898DeriveBytes - 通过使用基于 HMACSHA1 的伪随机数生成器,实现基于密码的密钥派生功能 (PBKDF2 - 一种基于密码的密钥派生函数) 17             // 通过 密码 和 salt 派生密钥 18             System.Security.Cryptography.Rfc2898DeriveBytes rfc = new System.Security.Cryptography.Rfc2898DeriveBytes( pwdValue, salt );19  20             aes.BlockSize = aes.LegalBlockSizes[0].MaxSize;21             aes.KeySize = aes.LegalKeySizes[0].MaxSize;22             aes.Key = rfc.GetBytes( aes.KeySize / 8 );23             aes.IV = rfc.GetBytes( aes.BlockSize / 8 );24  25             // 用当前的 Key 属性和初始化向量 IV 创建对称加密器对象 26             System.Security.Cryptography.ICryptoTransform encryptTransform = aes.CreateEncryptor( );27             // 加密后的输出流 28             System.IO.MemoryStream encryptStream = new System.IO.MemoryStream( );29             // 将加密后的目标流(encryptStream)与加密转换(encryptTransform)相连接 30             System.Security.Cryptography.CryptoStream encryptor = new System.Security.Cryptography.CryptoStream31                 ( encryptStream, encryptTransform, System.Security.Cryptography.CryptoStreamMode.Write );32  33             // 将一个字节序列写入当前 CryptoStream (完成加密的过程)34             encryptor.Write( data, 0, data.Length );35             encryptor.Close( );36             // 将加密后所得到的流转换成字节数组,再用Base64编码将其转换为字符串 37             string encryptedString = Convert.ToBase64String( encryptStream.ToArray( ) );38             return encryptedString;39         }
View Code

2.解密

 1 /// <summary> 2         /// 解密 3         /// </summary> 4         public static string Decrypt( string input ) { 5             byte[ ] encryptBytes = Convert.FromBase64String( input ); 6             byte[ ] salt = Encoding.UTF8.GetBytes( saltValue ); 7             System.Security.Cryptography.AesManaged aes = new System.Security.Cryptography.AesManaged( ); 8             System.Security.Cryptography.Rfc2898DeriveBytes rfc = new System.Security.Cryptography.Rfc2898DeriveBytes( pwdValue, salt ); 9  10             aes.BlockSize = aes.LegalBlockSizes[0].MaxSize;11             aes.KeySize = aes.LegalKeySizes[0].MaxSize;12             aes.Key = rfc.GetBytes( aes.KeySize / 8 );13             aes.IV = rfc.GetBytes( aes.BlockSize / 8 );14  15             // 用当前的 Key 属性和初始化向量 IV 创建对称解密器对象 16             System.Security.Cryptography.ICryptoTransform decryptTransform = aes.CreateDecryptor( );17             // 解密后的输出流 18             System.IO.MemoryStream decryptStream = new System.IO.MemoryStream( );19  20             // 将解密后的目标流(decryptStream)与解密转换(decryptTransform)相连接 21             System.Security.Cryptography.CryptoStream decryptor = new System.Security.Cryptography.CryptoStream(22                 decryptStream, decryptTransform, System.Security.Cryptography.CryptoStreamMode.Write );23             // 将一个字节序列写入当前 CryptoStream (完成解密的过程) 24             decryptor.Write( encryptBytes, 0, encryptBytes.Length );25             decryptor.Close( );26  27             // 将解密后所得到的流转换为字符串 28             byte[ ] decryptBytes = decryptStream.ToArray( );29             string decryptedString = UTF8Encoding.UTF8.GetString( decryptBytes, 0, decryptBytes.Length );30             return decryptedString;31         }32     }//class end
View Code