AES加密(128位):

         /// <summary>
/// 有密码的AES加密
/// </summary>
internal static string Encrypt(string toEncrypt)
{
var keyArray = Encoding.GetEncoding(Charset).GetBytes(key);;
var toEncryptArray = Encoding.GetEncoding(Charset).GetBytes(toEncrypt);
using (var acsp = new AesCryptoServiceProvider { KeySize = , BlockSize = })
{
acsp.GenerateIV();
using (var aes = new AesCryptoServiceProvider { Key = keyArray, IV = acsp.IV, Mode = CipherMode.ECB, Padding = PaddingMode.PKCS7 })
{
byte[] resultArray;
using (var cTransform = aes.CreateEncryptor())
{
resultArray = cTransform.TransformFinalBlock(toEncryptArray, , toEncryptArray.Length);
}
return Convert.ToBase64String(resultArray);
}
}
}

AES解密(128位):

         /// <summary>
/// AES解密
/// </summary>
internal static string Decrypt(string toDecrypt, string key)
{
var keyArray = Encoding.GetEncoding(Charset).GetBytes(key);
var toDecryptArray = Convert.FromBase64String(toDecrypt);
using (var acsp = new AesCryptoServiceProvider { KeySize = , BlockSize = })
{
acsp.GenerateIV();
using (var aes = new AesCryptoServiceProvider { Key = keyArray, IV = acsp.IV, Mode = CipherMode.ECB, Padding = PaddingMode.PKCS7 })
{
byte[] resultArray;
using (var cTransform = aes.CreateDecryptor())
{
resultArray = cTransform.TransformFinalBlock(toDecryptArray, , toDecryptArray.Length);
}
return Encoding.GetEncoding(Charset).GetString(resultArray);
}
}
}
04-30 12:17