c# Rijndael IV大小与块大小不匹配,虽然它应该匹配

本文关键字:不匹配 IV Rijndael | 更新日期: 2023-09-27 18:18:37

我有以下代码:

private void EncryptFile(string inputFile, string outputFile, string pass)
    {
        try
        {
            string password = @pass;
            UnicodeEncoding UE = new UnicodeEncoding();
            byte[] key = UE.GetBytes(password);
            byte[] iv = new byte[128];
            for(int i =0; i < iv.Length; i++)
            {
                iv[i] = Convert.ToByte(true);
            }
            string cryptFile = outputFile;
            FileStream fsCrypt = new FileStream(cryptFile, FileMode.Create);
            RijndaelManaged RMCrypto = new RijndaelManaged();
            MessageBox.Show(RMCrypto.BlockSize + "'n" + iv.Length);
            CryptoStream cs = new CryptoStream(fsCrypt,
                RMCrypto.CreateEncryptor(key, iv),
                CryptoStreamMode.Write);
            FileStream fsIn = new FileStream(inputFile, FileMode.Open);
            int data;
            while ((data = fsIn.ReadByte()) != -1)
                cs.WriteByte((byte)data);

            fsIn.Close();
            cs.Close();
            fsCrypt.Close();
        }
        catch(Exception ex)
        {
            //MessageBox.Show("Encryption failed!", "Error");
            MessageBox.Show(ex.Message);
        }
    }

但是静脉注射的大小有问题。使用一个简单的消息框,我发现(可能)块大小是128。因此,我将IV设置为一个128字节的数组,其中充满了要测试的"1"值。第一个消息框确认块大小和IV数组长度都是128。然而,我得到一个异常,说Specified initialization vector (IV) does not match the block size for this algorithm.

为什么会这样?如何解决这个问题?

c# Rijndael IV大小与块大小不匹配,虽然它应该匹配

AES块大小为128 bits。不是字节。位。

AES竞赛的获胜者Rijndael支持128、192和256 的块和密钥大小,但在AES中,块大小总是128 。额外的块大小未被AES标准采用