起源
最近负责的一个项目, 对接的东西刚好有需要用到AES加密, 就顺便学习记录一下
Aes是什么
在密码学中又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准, 是对称密钥加密中最流行的算法之一
Aes加密
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
public static string AesEncrypt(string text, string password, string iv) {
byte[] pwdBytes = Encoding.UTF8.GetBytes(password); byte[] keyBytes = new byte[16]; int len = pwdBytes.Length; if (len > keyBytes.Length) len = keyBytes.Length; Array.Copy(pwdBytes, keyBytes, len); RijndaelManaged rhinelandCipher = new RijndaelManaged() { Mode = CipherMode.CBC, Padding = PaddingMode.Zeros, KeySize = 128, BlockSize = 128, Key = keyBytes, IV = Encoding.UTF8.GetBytes(iv) };
ICryptoTransform transform = rhinelandCipher.CreateEncryptor(); byte[] plainText = Encoding.UTF8.GetBytes(text); byte[] cipherBytes = transform.TransformFinalBlock(plainText, 0, plainText.Length);
return Convert.ToBase64String(cipherBytes);
}
|
Aes解密
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
public static string AesDecrypt(string text, string password, string iv) { byte[] encryptedData = Convert.FromBase64String(text); byte[] pwdBytes = Encoding.UTF8.GetBytes(password); byte[] keyBytes = new byte[16]; int len = pwdBytes.Length; if (len > keyBytes.Length) len = keyBytes.Length; Array.Copy(pwdBytes, keyBytes, len); RijndaelManaged rhinelandCipher = new RijndaelManaged() { Mode = CipherMode.CBC, Padding = PaddingMode.Zeros, KeySize = 128, BlockSize = 128, Key = keyBytes, IV = Encoding.UTF8.GetBytes(iv) };
ICryptoTransform transform = rhinelandCipher.CreateDecryptor(); byte[] plainText = transform.TransformFinalBlock(encryptedData, 0, encryptedData.Length); return Encoding.UTF8.GetString(plainText); }
|
调用
1 2 3 4 5 6 7 8 9 10 11
| static void Main(string[] args) { string password = "1234567890123456"; string iv = "1234567887654321"; string message = AesEncrypt("abcdefghigklmnopqrstuvwxyz0123456789", password, iv); Console.WriteLine(message); message = AesDecrypt("S8AclhU3eZDc0zV+upMD1dnm3ZwhoH+lNIA4U6+6O1w8QFy55oYo3fwgYUQdNNn0", password, iv); Console.WriteLine(message); }
|