简体   繁体   English

指定的初始化向量(IV)与此算法的块大小不匹配

[英]Specified initialization vector(IV) does not match the block size for this algorithm

I'm trying to make an encryption system with c#. 我正在尝试使用c#创建加密系统。 This is the code for the encryption. 这是加密的代码。

public static void EncryptFile(string inFile, string outFile, string @inkey)
    {
        try
        {
            UnicodeEncoding ue = new UnicodeEncoding();
            byte[] key = ue.GetBytes(inkey);
            FileStream fsEncrypt = new FileStream(outFile, FileMode.Create);

            RijndaelManaged rmCrypto = new RijndaelManaged();

            CryptoStream cs = new CryptoStream(fsEncrypt, rmCrypto.CreateEncryptor(key, key), CryptoStreamMode.Write);
            FileStream fsIn = new FileStream(inFile, FileMode.Open);

            int data;
            while((data=fsIn.ReadByte()) != 1){
                cs.WriteByte((byte)data);
            }

            fsIn.Close(); cs.Close(); fsEncrypt.Close();
        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.Message, "Fail to encrypt", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

Now, this code throws exception every time I run it, says 现在,这个代码每次运行时抛出异常,说

Specified initialization vector(IV) does not match the block size for this algorithm 指定的初始化向量(IV)与此算法的块大小不匹配

I have read on other discussion about this, saying that there is a problem with the number of bytes (my key length passed into this function is 255). 我已阅读其他有关此问题的讨论,并说字节数存在问题(传递给此函数的密钥长度为255)。 But I have tried making the key only 16 bytes and still not working. 但我已经尝试使密钥只有16个字节仍然无法正常工作。

After some troubleshooting I found out that this part: 经过一些故障排除我发现这部分:

CryptoStream cs = new CryptoStream(fsEncrypt, rmCrypto.CreateEncryptor(key, key), CryptoStreamMode.Write);

throws the exception. 抛出异常。 I have no idea why. 我不知道为什么。 Anyone can help? 有人可以帮忙吗?

You're passing the key twice to CreateEncryptor , but it needs a key and an IV ( Initialization Vector ). 您将密钥传递两次到CreateEncryptor ,但它需要一个密钥和一个IV( 初始化向量 )。 The second parameter should be an array with 128 random bits. 第二个参数应该是一个包含128个随机位的数组。 128 bits is the default block size for RijndaelManaged, but it accepts other values as well (such as 256). 128位是RijndaelManaged的默认块大小,但它也接受其他值(例如256)。 Read this for more info. 阅读本文以获取更多信息。 And as Grzegorz W pointed out in the comments, you might need to choose a different key size as well. 正如Grzegorz W在评论中指出的那样,您可能还需要选择不同的密钥大小

If you're not familiar with encryption (in which case you should stop and learn more about it before implementing your own solution, or use a ready-made one instead), the function of the IV is prevent that the same message encoded twice produce the same ciphertext. 如果您不熟悉加密(在这种情况下,您应该在实现自己的解决方案之前停止并了解更多信息,或者使用现成的解决方案), IV功能是防止相同的消息编码两次产生相同的密文。 It should be random for each message (and each use of the message), does not need to be kept secret, but you need to store it to be able to decipher the message later (ie you can not discard it after encryption). 它应该是随机的每条消息(以及消息的每次使用),不需要保密,但你需要存储它以便以后能够解密消息(即加密后你不能丢弃它)。

暂无
暂无

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

相关问题 指定的初始化向量 (IV) 与此算法的块大小不匹配 - Specified initialization vector (IV) does not match the block size for this algorithm 指定的初始化向量 (IV) 与此算法的块大小不匹配 - Specified initialization vector (IV) does not match the block size for this algorithm 指定的初始化向量(IV)与该算法的块大小不匹配 - The specified initialization vector (IV) does not match the block size for this algorithm 使用CryptoStream的“指定的初始化向量(IV)与该算法的块大小不匹配” - “Specified initialization vector (IV) does not match the block size for this algorithm” using an CryptoStream 连接 WCF Cryptography.CryptographicException:指定的初始化向量 (IV) 与此算法的块大小不匹配 - Connect WCF Cryptography.CryptographicException: Specified initialization vector (IV) does not match the block size for this algorithm 指定的初始化向量(IV)与使用AES的c#中的块大小不匹配 - Specified initialization vector (IV) does not match the block size in c# using AES AESCrypt如何处理文件格式2的初始化向量(IV)? - How does AESCrypt handle the initialization vector (IV) for file format 2? SymmetricAlgorithm密钥和初始化向量(IV)的实现 - SymmetricAlgorithm Key and initialization vector (IV) implementation C#Rijndael IV大小与块大小不匹配,但它应该 - C# Rijndael IV size doesn't match block size, while it should 收到错误:初始化AesCryptoProvider时“指定的块大小对此算法无效” - Got Error: “Specified block size is not valid for this algorithm” while initialize AesCryptoProvider
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM