简体   繁体   English

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

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

I am working on a base encryption method.我正在研究一种基本的加密方法。 I am using RijndaelManaged.我正在使用 RijndaelManaged。 I got this code from somewhere a long time ago, but can't remember where.我很久以前从某个地方得到了这个代码,但不记得在哪里。

I had my code working before, but something changed and I cannot quite figure it out.我之前让我的代码工作,但有些事情发生了变化,我无法弄清楚。

When I run my code, I get the following error;当我运行我的代码时,出现以下错误;

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

Here is my code:这是我的代码:

string textToEncrypt = "TEST STRING";

int keySize = 256;
string hashAlgorithm = "SHA1";
string passPhrase = "AH!PSB0%FGHR$";
string saltValue = "LRT%YUR#VBNL@1";
string initVector = "HR$2pIjHR$2pIj";

byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue);

byte[] plainTextBytes = Encoding.UTF8.GetBytes(textToEncrypt);

var password = new PasswordDeriveBytes(passPhrase, saltValueBytes, hashAlgorithm, 2);

byte[] keyBytes = password.GetBytes(keySize / 8);

RijndaelManaged symmetricKey = new RijndaelManaged();

symmetricKey.Mode = CipherMode.CBC;

ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes,initVectorBytes);

MemoryStream memoryStream = new MemoryStream();

var cryptoStream = new CryptoStream(memoryStream,encryptor,CryptoStreamMode.Write);
cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);

cryptoStream.FlushFinalBlock();

byte[] cipherTextBytes = memoryStream.ToArray();

memoryStream.Close();
cryptoStream.Close();

string cipherText = Convert.ToBase64String(cipherTextBytes);

Any help will be appreciated.任何帮助将不胜感激。

The problem is your initialization vector size needs to be 16 bytes.问题是您的初始化向量大小需要为 16 字节。

Your initial vector size is 14 bytes.您的初始向量大小为 14 字节。

You will need to increase the size of your initial vector by 2 bytes and your code will work.您需要将初始向量的大小增加 2 个字节,您的代码才能工作。

Example:例子:

string initVector = "HR$2pIjHR$2pIj12";

You will then get the output with your current code and the example IV (initialization vector) size provided:然后,您将获得包含当前代码和示例 IV(初始化向量)大小的输出:

hAC8hMf3N5Zb/DZhFKi6Sg== hAC8hMf3N5Zb/DZhFKi6Sg==

This article provides a good explanation on what the initialization vector is.这篇文章很好地解释了初始化向量是什么。

http://en.wikipedia.org/wiki/Initialization_vector http://en.wikipedia.org/wiki/Initialization_vector

You should be able to check how many bytes the IV needs to be using:您应该能够检查 IV 需要使用多少字节:

algorithm.BlockSize / 8

BlockSize is in bits, so 128 bits / 8 gives 16 bytes of ASCII, and you may also find Rfc2898DeriveBytes a useful class for producing keys. BlockSize 以位为单位,因此 128 位 / 8 给出 16 字节的 ASCII,您可能还会发现Rfc2898DeriveBytes是生成密钥的有用类。

algorithm.IV = rfc2898DeriveBytesForIV.GetBytes(algorithm.BlockSize / 8);

Hope it helps.希望能帮助到你。

If someone is migrating their code from .NET framework to .NET Core and starts getting this exception on RijndaelManaged.CreateEncryptor : your old cold was working due to the fact that " .NET Framework allows IVs greater than 64 bits and truncates them ".如果有人将他们的代码从 .NET Framework 迁移到 .NET Core 并开始在RijndaelManaged.CreateEncryptor上收到此异常:由于“ .NET Framework 允许大于 64 位的 IV 并截断它们”,您的旧感冒正在起作用。

To resolve see Kevin Jones comment: " simply change your IV to only the first 8 bytes "要解决,请参阅Kevin Jones评论:“ 只需将您的 IV 更改为仅前 8 个字节

So, as an example:所以,作为一个例子:

private static byte[] IV_192 =  { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18 };

would become:会成为:

// Rename field if desired.
private static byte[] IV_192 =  { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };

Also worth noting, " Rijndael class is the predecessor of the Aes algorithm. You should use the Aes algorithm instead of Rijndael. "另外值得注意的是,“ Rijndael 类是 Aes 算法的前身。你应该使用 Aes 算法而不是 Rijndael。

暂无
暂无

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