繁体   English   中英

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

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

我有以下代码:

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

但是IV尺寸有问题。 使用一个简单的消息框,我发现(可能)块大小为128.因此,我将IV设置为一个128字节的数组,其中包含“1”值以进行测试。 第一个消息框确认块大小,IV阵列长度都是128.但是,我得到一个例外,说明Specified initialization vector (IV) does not match the block size for this algorithm.

这是为什么以及如何解决这个问题?

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

AES竞赛的获胜者Rijndael支持128,192和256 位的块和密钥大小,但在AES中,块大小始终为128 AES标准未采用额外的块大小

暂无
暂无

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

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