AES_ECB_PKCS7 加解密
2024-09-28
17
AES加密,对应java中的 aes/ECB/pkcs5padding 模式的算法
配置iv
static byte[] ivbyte = new byte[16] { 49, 50, 51, 52, 53, 54, 55, 56, 49, 50, 51, 52, 53, 54, 55, 56 };
加密
/// <summary>
/// AES加密 对应java中的 aes/ECB/pkcs5padding 模式的算法
/// </summary>
/// <param name="text">待加密的字符串</param>
/// <param name="key">密钥</param>
/// <returns></returns>
public static string Aes_ECB_PKCS7_Encrypt(string text, string key) {
byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key);
byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(text);
var rDel = Aes.Create();
rDel.Key = keyArray;
rDel.IV = ivbyte;
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);
}
解密
/// <summary>
/// AES解密 对应java中的 aes/ECB/pkcs5padding 模式的算法
/// </summary>
/// <param name="str">待解密的字符串</param>
/// <param name="key">密钥</param>
public static string Aes_ECB_PKCS7_Decrypt(string str, string key) {
byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key);
byte[] toEncryptArray = Convert.FromBase64String(str);
var rDel = Aes.Create();
rDel.Key = keyArray;
rDel.IV = ivbyte;//ivArray;
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);
}
更新于:2个月前赞一波!1
相关文章
文章评论
评论问答