【转载】C#之AES256位加密解密
文章转载自http://jingyan.baidu.com/article/20b68a8856821b796cec6280.html
密码学中的高级加密标准(Advanced Encryption Standard,AES),又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准。这个标准用来替代原先的DES,已经被多方分析且广为全世界所使用。
AES加密代码
/// <summary> /// AES加密 /// </summary> /// <param name="encryptStr">明文</param> /// <param name="key">密钥</param> /// <returns></returns> public static string Encrypt(string encryptStr,string key) { byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key); byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(encryptStr); RijndaelManaged rDel = new RijndaelManaged(); rDel.Key = keyArray; rDel.Mode = CipherMode.ECB; rDel.Padding = PaddingMode.PKCS7; ICryptoTransform cTransform = rDel.CreateEncryptor(); byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); return Convert.ToBase64String(resultArray, 0, resultArray.Length); }
AES解密代码
/// <summary> /// AES解密 /// </summary> /// <param name="decryptStr">密文</param> /// <param name="key">密钥</param> /// <returns></returns> public static string Decrypt(string decryptStr,string key) { byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key); byte[] toEncryptArray = Convert.FromBase64String(decryptStr); RijndaelManaged rDel = new RijndaelManaged(); rDel.Key = keyArray; rDel.Mode = CipherMode.ECB; rDel.Padding = PaddingMode.PKCS7; ICryptoTransform cTransform = rDel.CreateDecryptor(); byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); return UTF8Encoding.UTF8.GetString(resultArray); }
主程序入口,测试代码
static void Main(string[] args) { string encryptStr,decryptStr, key; key = "ae125efkk4454eeff444ferfkny6oxi8"; encryptStr = "C#AES加密字符串"; Console.WriteLine("============== 256位AES加密 ============\n"); Console.WriteLine("加密前的字符串:"+encryptStr); Console.WriteLine("密钥:" + key); Console.WriteLine(); decryptStr = Program.Encrypt(encryptStr,key); Console.WriteLine("加密后的字符串:" + decryptStr); encryptStr = Program.Decrypt(decryptStr, key); Console.WriteLine("解密后的字符串:" + encryptStr); Console.ReadKey(); }
注意事项
密钥必须是32个字符(非中文),换句话说每个字符只能占1Byte。
基于向量的AES加密解密方法,采用CBC/PKCS5Padding加密
秘钥规则如下:
1、 algorithm采用“AES”。
2、 秘钥长度采用128byte,即秘钥固定16位字节。
3、 偏移量位固定值 。
4、 编码格式:UTF-8
5、 AES加密结果使用base64编码
/// <summary> /// AES加密 /// </summary> /// <param name="text">明文字符串</param> /// <param name="key">秘钥</param> /// <param name="iv">加密辅助向量</param> /// <returns>密文</returns> public static string AESEncrypt(string text, string key, string iv) { RijndaelManaged rijndaelCipher = new RijndaelManaged(); rijndaelCipher.Mode = CipherMode.CBC; rijndaelCipher.Padding = PaddingMode.PKCS7; rijndaelCipher.KeySize = 128; rijndaelCipher.BlockSize = 128; byte[] pwdBytes = System.Text.Encoding.UTF8.GetBytes(key); byte[] keyBytes = new byte[16]; int len = pwdBytes.Length; if (len > keyBytes.Length) len = keyBytes.Length; System.Array.Copy(pwdBytes, keyBytes, len); rijndaelCipher.Key = keyBytes; byte[] ivBytes = System.Text.Encoding.UTF8.GetBytes(iv); rijndaelCipher.IV = ivBytes; ICryptoTransform transform = rijndaelCipher.CreateEncryptor(); byte[] plainText = Encoding.UTF8.GetBytes(text); byte[] cipherBytes = transform.TransformFinalBlock(plainText, 0, plainText.Length); return Convert.ToBase64String(cipherBytes); } /// <summary> /// AES解密 /// </summary> /// <param name="text">加密字符串</param> /// <param name="key">秘钥</param> /// <param name="iv">加密辅助向量</param> /// <returns>明文</returns> public static string AESDecrypt(string text, string key, string iv) { RijndaelManaged rijndaelCipher = new RijndaelManaged(); rijndaelCipher.Mode = CipherMode.CBC; rijndaelCipher.Padding = PaddingMode.PKCS7; rijndaelCipher.KeySize = 128; rijndaelCipher.BlockSize = 128; byte[] encryptedData = Convert.FromBase64String(text); byte[] pwdBytes = System.Text.Encoding.UTF8.GetBytes(key); byte[] keyBytes = new byte[16]; int len = pwdBytes.Length; if (len > keyBytes.Length) len = keyBytes.Length; System.Array.Copy(pwdBytes, keyBytes, len); rijndaelCipher.Key = keyBytes; byte[] ivBytes = System.Text.Encoding.UTF8.GetBytes(iv); rijndaelCipher.IV = ivBytes; ICryptoTransform transform = rijndaelCipher.CreateDecryptor(); byte[] plainText = transform.TransformFinalBlock(encryptedData, 0, encryptedData.Length); return Encoding.UTF8.GetString(plainText); }