简体   繁体   English

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

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

   public static string GenerateKey()
   {
        AesCryptoServiceProvider aesCrypto = (AesCryptoServiceProvider)AesCryptoServiceProvider.Create();

        // Use the Automatically generated key for Encryption. 
        return ASCIIEncoding.ASCII.GetString(aesCrypto.Key);
    }

    static void EncryptFile(string sInputFilename, string sOutputFilename, string sKey)
    {
        FileStream fsInput = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read);

        FileStream fsEncrypted = new FileStream(sOutputFilename, FileMode.Create, FileAccess.Write);
        AesCryptoServiceProvider AES = new AesCryptoServiceProvider();
        // Sets the appropriate block size for the AES Encryption Method
        AES.BlockSize = 128;
        AES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
        AES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
        ICryptoTransform aesencrypt = AES.CreateEncryptor();
        CryptoStream cryptostream = new CryptoStream(fsEncrypted, aesencrypt, CryptoStreamMode.Write);

        byte[] bytearrayinput = new byte[fsInput.Length];
        fsInput.Read(bytearrayinput, 0, bytearrayinput.Length);
        cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length);
        cryptostream.Close();
        fsInput.Close();
        fsEncrypted.Close();
    }

    static void DecryptFile(string sInputFilename, string sOutputFilename, string sKey)
    {
        AesCryptoServiceProvider AES = new AesCryptoServiceProvider();
        //A 64 bit key and IV is required for this provider.
        //Set secret key For DES algorithm.
        AES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
        //Set initialization vector.
        AES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);

        //Create a file stream to read the encrypted file back.
        FileStream fsread = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read);
        //Create a DES decryptor from the DES instance.
        ICryptoTransform aesdecrypt = AES.CreateDecryptor();
        //Create crypto stream set to read and do a 
        //DES decryption transform on incoming bytes.
        CryptoStream cryptostreamDecr = new CryptoStream(fsread, aesdecrypt, CryptoStreamMode.Read);
        //Print the contents of the decrypted file.
        StreamWriter fsDecrypted = new StreamWriter(sOutputFilename);
        fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd());
        fsDecrypted.Flush();
        fsDecrypted.Close();
    }

I'm getting the exception listed in the title at this line in the EncryptFile method.我在 EncryptFile 方法的这一行中得到了标题中列出的异常。

AES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);

I set the BlockSize directly before that line, and I'm sure AES is suppose to use a 16 byte block size, so what do I do to get this working?我直接在该行之前设置了 BlockSize,并且我确定 AES 应该使用 16 字节的块大小,那么我该怎么做才能让它工作呢? I'm unsure as to why the code is still having problems with the block size.我不确定为什么代码仍然存在块大小问题。

Note: I'm simply trying some examples I found online, this isn't meant to be a lock-tight AES implementation, just something I'd like to get working so I can continue to learn about the algorithm.注意:我只是在尝试我在网上找到的一些示例,这并不意味着是一个锁紧的 AES 实现,只是我想开始工作,以便我可以继续了解该算法。

Thanks for any help.谢谢你的帮助。

The IV must be exactly the same size as the block size, which in the case of AES is 16 bytes. IV 必须与块大小完全相同,在 AES 的情况下为 16 个字节。

暂无
暂无

声明:本站的技术帖子网页,遵循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