0

我正在使用 rijndeal 加密和解密一些数据!但它给了我这个错误:

填充无效且无法移除。

我搜索了很多,但没有任何帮助我解决这个错误!tis 是我的加密/解密代码:

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;
    }

谁能帮我?

4

1 回答 1

0

将解密端设置为无填充。解密并查看添加到明文末尾的内容。这将告诉您加密端添加了什么填充。更改加密端以明确设置该填充并更改解密端以明确期望相同的填充。

或者将两者都设置为 PKCS7(又名 PKCS5)填充。你需要让两边的填充相同。

于 2013-04-28T12:08:03.020 回答