Skip to main content
 首页 » 编程设计

c#之Rijndael 填充或长度无效

2024年02月23日33qq78292959

我正在尝试使用 Rijndael 或 Aes 以及下面的代码来加密/解密字符串。

public class Crypto 
{ 
    private const string defaultVector = "asdfg123456789"; 
    private const CipherMode cipherMode = CipherMode.CBC; 
    //Have tried PaddingMode.ISO10126, PaddingMode.None, and PaddingMode.PKCS7 
    private const PaddingMode paddingMode = PaddingMode.ISO10126; 
    private const int iterations = 2; 
    private static Rijndael GetCrypto(string passphrase) 
    { 
        var crypt = Rijndael.Create(); 
        crypt.Mode = cipherMode; 
        crypt.Padding = paddingMode; 
        crypt.BlockSize = 256; 
        crypt.KeySize = 256; 
        crypt.Key = 
            new Rfc2898DeriveBytes(passphrase, Encoding.Unicode.GetBytes(defaultVector), iterations).GetBytes(32); 
        crypt.IV = new Rfc2898DeriveBytes(passphrase, Encoding.Unicode.GetBytes(defaultVector), iterations).GetBytes(32); 
        return crypt; 
    } 
    public static string Encrypt(string plainText, string passphrase) 
    { 
        byte[] clearData = Encoding.Unicode.GetBytes(plainText); 
        byte[] encryptedData; 
        var crypt = GetCrypto(passphrase); 
        using (var ms = new MemoryStream()) 
        { 
            using (var cs = new CryptoStream(ms, crypt.CreateEncryptor(), CryptoStreamMode.Write)) 
            { 
                cs.Write(clearData, 0, clearData.Length); 
                //cs.FlushFinalBlock(); //Have tried this active and commented with no change. 
            } 
            encryptedData = ms.ToArray(); 
        } 
        //Changed per Xint0's answer. 
        return Convert.ToBase64String(encryptedData); 
    } 
    public static string Decrypt(string cipherText, string passphrase) 
    { 
        //Changed per Xint0's answer. 
        byte[] encryptedData = Convert.FromBase64String(cipherText); 
        byte[] clearData; 
        var crypt = GetCrypto(passphrase); 
        using (var ms = new MemoryStream()) 
        { 
                using (var cs = new CryptoStream(ms, crypt.CreateDecryptor(), CryptoStreamMode.Write)) 
                { 
                    cs.Write(encryptedData, 0, encryptedData.Length); 
                    //I have tried adding a cs.FlushFinalBlock(); here as well. 
                } 
                clearData = ms.ToArray(); 
        } 
        return Encoding.Unicode.GetString(clearData); 
    } 
} 

//编辑:我已经根据下面 Xint0 的回答更改了对 Convert.ToBase64String 的 Unicode 调用。

在解密方法中的 cs.Write 中,我收到“填充无效且无法删除”的错误。

我已尝试将填充设置为 PaddingMode.None,但我得到“要加密的数据的长度无效”。在加密方法中的 cs.Write 上。

我看过这些,但他们所说的似乎都不起作用。

Padding is invalid and cannot be removed

Padding is invalid and cannot be removed?

堆栈跟踪显示 System.Security.CryptographicException 来自 RijndaelManagedTransform.DecryptData(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount, Byte[]& outputBuffer, Int32 outputOffset, PaddingMode paddingMode, Boolean fLast)。

请您参考如下方法:

我花了很多时间寻找导致 CryptographicException 的原因,我也在谷歌上搜索,包括 Stackoverflow。
这是一个愚蠢的错误(在使用复制粘贴编程时经常出现)如下:

它从 CryptoStream 的实例中调用方法 FlushFinalBlock()。

查看错误代码:

CryptoStream cs = new CryptoStream(ms, rj.CreateDecryptor(rj.Key, rj.IV), CryptoStreamMode.Write); 

我用它来加密,所以你可以看到 CryptoStreamMode.Write 但在同一指令中我创建了解密器而不是加密器(请参阅构造函数中的第二个参数)。

小心检查,以免浪费您宝贵的时间;)

问候
布朗克