简体   繁体   English

C#Rijndael IV大小与块大小不匹配,但它应该

[英]C# Rijndael IV size doesn't match block size, while it should

I have the following code: 我有以下代码:

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

But have a problem with the IV size. 但是IV尺寸有问题。 Using a simple message box I found that (probably) the block size is 128. So, I set the IV to a 128 bytes array full of "1" values to test. 使用一个简单的消息框,我发现(可能)块大小为128.因此,我将IV设置为一个128字节的数组,其中包含“1”值以进行测试。 The first message box confirms the blocksize and the IV array length are both 128. However, I get an exception saying Specified initialization vector (IV) does not match the block size for this algorithm. 第一个消息框确认块大小,IV阵列长度都是128.但是,我得到一个例外,说明Specified initialization vector (IV) does not match the block size for this algorithm.

Why is this and how to fix the issue? 这是为什么以及如何解决这个问题?

AES block size is 128 bits . AES 块大小为128 Not bytes. 不是字节。 Bits. 位。

The winner of the AES contest, Rijndael, supports block and key sizes of 128, 192, and 256 bits , but in AES the block size is always 128 bits . AES竞赛的获胜者Rijndael支持128,192和256 位的块和密钥大小,但在AES中,块大小始终为128 The extra block sizes were not adopted by the AES standard AES标准未采用额外的块大小

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM