我正在使用rijndeal来加密和解密一些数据!但它给了我这个错误:
Padding is invalid and cannot be removed.
我搜索了很多,但没有什么能帮我解决这个错误! 这是我的加密/解密代码:
public string Encrypt(string text)
{
mainRM = new System.Security.Cryptography.RijndaelManaged();
mainRM.BlockSize = 256;
mainRM.KeySize = 256;
memorystream = new System.IO.MemoryStream();
ICryptoTransform icrypt = mainRM.CreateEncryptor(key, iv);
CryptoStream cryptstream = new CryptoStream(memorystream, icrypt, CryptoStreamMode.Write);
cryptstream.FlushFinalBlock();
System.IO.StreamWriter sw = new System.IO.StreamWriter(cryptstream);
sw.Write(text);
return Convert.ToBase64String(memorystream.ToArray());
}
public string Decrypt(string CryptedText)
{
string custinfo;
System.IO.StreamReader streamreader;
mainRM = new RijndaelManaged();
mainRM.BlockSize = 256;
mainRM.KeySize = 256;
memorystream = new System.IO.MemoryStream(Convert.FromBase64String(CryptedText));
ICryptoTransform icrypt = mainRM.CreateDecryptor(key, iv);
memorystream.Position = 0;
CryptoStream cryptstream = new CryptoStream(memorystream, icrypt, CryptoStreamMode.Read);
cryptstream.FlushFinalBlock();
streamreader = new System.IO.StreamReader(cryptstream);
custinfo = streamreader.ReadToEnd();
return custinfo;
}
任何人都可以帮助我吗?
答案 0 :(得分:0)
将解密端设置为无填充。解密并查看您添加到明文末尾的内容。这将告诉您加密端添加的填充。更改加密端以显式设置该填充并更改解密端以明确期望相同的填充。
或者将两者都设置为PKCS7(aka PKCS5)填充。你需要在两侧都有相同的填充。