Aes Base64加密
/// <summary>
/// 加密
/// </summary>
/// <param name="data"></param>
/// <param name="key"></param>
/// <returns></returns>
public static string Encryption(string data, string key)
{
byte[] keyArr = Encoding.UTF8.GetBytes(key);
byte[] dataArr = Encoding.UTF8.GetBytes(data);
using var aes = Aes.Create();
aes.Key = keyArr;
aes.Mode = CipherMode.ECB;
aes.Padding = PaddingMode.PKCS7;
using var cryptoTransform = aes.CreateEncryptor();
byte[] result = cryptoTransform.TransformFinalBlock(dataArr, 0, dataArr.Length);
return Convert.ToBase64String(result);
}
Aes Base64解密
/// <summary>
/// 解密
/// </summary>
/// <param name="data"></param>
/// <param name="key"></param>
/// <returns></returns>
public static string Decryption(string data, string key)
{
byte[] keyArr = Encoding.UTF8.GetBytes(key);
byte[] dataArr = Convert.FromBase64String(data);
using var aes = Aes.Create();
aes.Key = keyArr;
aes.Mode = CipherMode.ECB;
aes.Padding = PaddingMode.PKCS7;
using var cryptoTransform = aes.CreateDecryptor();
byte[] result = cryptoTransform.TransformFinalBlock(dataArr, 0, dataArr.Length);
return Convert.ToBase64String(result);
}
标签:Aes,加密,string,C#,aes,dataArr,result,byte
From: https://www.cnblogs.com/kele-cc/p/17141153.html