当我尝试使用PaddingMode.PKCS7解密字符串时,我得到'填充无效且无法删除'异常,但是当我尝试使用PaddingMode.Zeros时,我没有得到任何异常。
P.S我使用PaddingMode.PKCS7加密了脚本,所以我必须使用相同的PaddingMode解密脚本。
if (encrypted == null || encrypted.Length == 0)
throw new ArgumentNullException("ecrypted");
if (keyArr == null || keyArr.Length == 0)
throw new ArgumentNullException("keyArr");
if (iv == null || iv.Length == 0)
throw new ArgumentNullException("iv");
using (RijndaelManaged rij = new RijndaelManaged())
{
rij.Padding = PaddingMode.PKCS7;
rij.Mode = CipherMode.CBC;
rij.KeySize = 128;
rij.BlockSize = 128;
rij.Key = keyArr;
rij.IV = iv;
ICryptoTransform decryptor = rij.CreateDecryptor();
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Write))
{
cs.Write(encrypted, 0, encrypted.Length);
}
plainText = Encoding.UTF8.GetString(ms.ToArray());
}
}
提前致谢
答案 0 :(得分:1)
通常填充错误并不意味着存在填充错误,而是解密错误。当解密失败时,输出基本上是随机字节,填充当然是不正确的。
确保: