AES 算法加密(ECB模式),加密后进行base64编码
异常
Specified key is not a valid size for this algorithm
解决方法:AES加密中参数key应是32位,如果位数不对会报此错。
/// <summary>
/// AES 算法加密(ECB模式) 将明文加密,加密后进行base64编码,返回密文
/// </summary>
/// <param name="EncryptStr">明文</param>
/// <param name="Key">密钥</param>
/// <returns>加密后base64编码的密文</returns>
public static string AesEncryptor_Base64(string EncryptStr, string Key)
{
try
{
//byte[] keyArray = Encoding.UTF8.GetBytes(Key);
byte[] keyArray = Convert.FromBase64String(Key);
byte[] toEncryptArray = Encoding.UTF8.GetBytes(EncryptStr);
RijndaelManaged rDel = new RijndaelManaged();
rDel.Key = keyArray;
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);
}
catch (Exception ex)
{
return null;
}
}
/// <summary>
/// AES 算法解密(ECB模式) 将密文base64解码进行解密,返回明文
/// </summary>
/// <param name="DecryptStr">密文</param>
/// <param name="Key">密钥</param>
/// <returns>明文</returns>
public static string AesDecryptor_Base64(string DecryptStr, string Key)
{
try
{
//byte[] keyArray = Encoding.UTF8.GetBytes(Key);
byte[] keyArray = Convert.FromBase64String(Key);
byte[] toEncryptArray = Convert.FromBase64String(DecryptStr);
RijndaelManaged rDel = new RijndaelManaged();
rDel.Key = keyArray;
rDel.Mode = CipherMode.ECB;
rDel.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = rDel.CreateDecryptor();
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
return Encoding.UTF8.GetString(resultArray);// UTF8Encoding.UTF8.GetString(resultArray);
}
catch (Exception ex)
{
return null;
}
}
标签:AES,加密,keyArray,rDel,Key,byte
From: https://www.cnblogs.com/duixue/p/18086993