.net 加密错误:填充无效,无法移除

时间:2023-03-08 17:19:45

今天用System.Security.Cryptography加密。使用了AesManaged,报错:填充无效,无法移除。分析是解密失败,密文损坏,或者KEY,IV不正确。

using (AesManaged aesAlg = new AesManaged())
{
aesAlg.Key = Encoding.Unicode.GetBytes("password");
aesAlg.IV = aesAlg.Key; using (ICryptoTransform decryptor = aesAlg.CreateDecryptor())
{
return decryptor.TransformFinalBlock(cipherBytes, 0, cipherBytes.Length);
} }