RijndaelManaged已经过时,升级一下即可,下面是例子
internal class Program
{
static void Main(string[] args)
{
var key = "1234567890123456";
var iv = "1234567890123456";
var input = "你好啊";
var jiami = EncryptByAESNew(input, key, iv);
Console.WriteLine(jiami);
Console.WriteLine(DecryptByAESNew(jiami, key, iv));
}
#region aes加密解密,RijndaelManaged
/// <summary>
/// AES加密算法
/// </summary>
/// <param name="input">明文字符串</param>
/// <param name="key">密钥 长度为:16</param>
/// <param name="iv">偏移 长度为:16</param>
/// <returns>字符串</returns>
public static string EncryptByAES(string input, string key, string iv)
{
if (string.IsNullOrWhiteSpace(input))
{
return input;
}
using (RijndaelManaged rijndaelManaged = new RijndaelManaged())
{
rijndaelManaged.Mode = CipherMode.CBC;
rijndaelManaged.Padding = PaddingMode.PKCS7;
rijndaelManaged.FeedbackSize = 128;
rijndaelManaged.Key = Encoding.UTF8.GetBytes(key);
rijndaelManaged.IV = Encoding.UTF8.GetBytes(iv);
ICryptoTransform encryptor = rijndaelManaged.CreateEncryptor(rijndaelManaged.Key, rijndaelManaged.IV);
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{
swEncrypt.Write(input);
}
byte[] bytes = msEncrypt.ToArray();
//return BitConverter.ToString(bytes).Replace("-", "").ToLower();//返回hex
return Convert.ToBase64String(bytes);
}
}
}
}
/// <summary>
/// AES解密
/// </summary>
/// <param name="input">密文字节数组</param>
/// <param name="key">密钥 长度为:16</param>
/// <param name="iv">偏移 长度为:16</param>
/// <returns>返回解密后的字符串</returns>
public static string DecryptByAES(string input, string key, string iv)
{
if (string.IsNullOrWhiteSpace(input))
{
return input;
}
var buffer = Convert.FromBase64String(input);
//buffer = hexStringToByteArray(input);//如果是hex则用hex方式处理
using (RijndaelManaged rijndaelManaged = new RijndaelManaged())
{
rijndaelManaged.Mode = CipherMode.CBC;
rijndaelManaged.Padding = PaddingMode.PKCS7;
rijndaelManaged.FeedbackSize = 128;
rijndaelManaged.Key = Encoding.UTF8.GetBytes(key);
rijndaelManaged.IV = Encoding.UTF8.GetBytes(iv);
ICryptoTransform decryptor = rijndaelManaged.CreateDecryptor(rijndaelManaged.Key, rijndaelManaged.IV);
using (MemoryStream msEncrypt = new MemoryStream(buffer))
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srEncrypt = new StreamReader(csEncrypt))
{
return srEncrypt.ReadToEnd();
}
}
}
}
}
#endregion
#region aes加密解密 Aes
/// <summary>
/// AES加密算法
/// </summary>
/// <param name="input">明文字符串</param>
/// <param name="key">密钥 长度为:16</param>
/// <param name="iv">偏移 长度为:16</param>
/// <returns>字符串</returns>
public static string EncryptByAESNew(string input, string key, string iv)
{
if (string.IsNullOrWhiteSpace(input))
{
return input;
}
using (var rijndaelManaged = Aes.Create())
{
rijndaelManaged.Mode = CipherMode.CBC;
rijndaelManaged.Padding = PaddingMode.PKCS7;
rijndaelManaged.FeedbackSize = 128;
rijndaelManaged.Key = Encoding.UTF8.GetBytes(key);
rijndaelManaged.IV = Encoding.UTF8.GetBytes(iv);
ICryptoTransform encryptor = rijndaelManaged.CreateEncryptor(rijndaelManaged.Key, rijndaelManaged.IV);
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{
swEncrypt.Write(input);
}
byte[] bytes = msEncrypt.ToArray();
//return BitConverter.ToString(bytes).Replace("-", "").ToLower();//返回hex
return Convert.ToBase64String(bytes);
}
}
}
}
/// <summary>
/// AES解密
/// </summary>
/// <param name="input">密文字节数组</param>
/// <param name="key">密钥 长度为:16、24、32</param>
/// <param name="iv">偏移 长度为:16</param>
/// <returns>返回解密后的字符串</returns>
public static string DecryptByAESNew(string input, string key, string iv)
{
if (string.IsNullOrWhiteSpace(input))
{
return input;
}
var buffer = Convert.FromBase64String(input);
//buffer = hexStringToByteArray(input);//如果是hex则用hex方式处理
using (var rijndaelManaged = Aes.Create())
{
rijndaelManaged.Mode = CipherMode.CBC;
rijndaelManaged.Padding = PaddingMode.PKCS7;
rijndaelManaged.FeedbackSize = 128;
rijndaelManaged.Key = Encoding.UTF8.GetBytes(key);
rijndaelManaged.IV = Encoding.UTF8.GetBytes(iv);
ICryptoTransform decryptor = rijndaelManaged.CreateDecryptor(rijndaelManaged.Key, rijndaelManaged.IV);
using (MemoryStream msEncrypt = new MemoryStream(buffer))
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srEncrypt = new StreamReader(csEncrypt))
{
return srEncrypt.ReadToEnd();
}
}
}
}
}
#endregion
}