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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/944833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 03:56:03  来源:igfitidea点击:

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

c#encryptionrijndaelmanagedrijndael

提问by

I am working on a base encryption method. I am using RijndaelManaged. I got this code from somewhere a long time ago, but can't remember where.

我正在研究一种基本的加密方法。我正在使用 RijndaelManaged。我很久以前从某个地方得到了这个代码,但不记得在哪里。

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 = "HRpIjHRpIj";

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.

任何帮助将不胜感激。

采纳答案by CodeLikeBeaker

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 = "HRpIjHRpIj12";

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

回答by Luke Puplett

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 Rfc2898DeriveBytesa useful class for producing keys.

BlockSize 以位为单位,因此 128 位 / 8 给出 16 个字节的 ASCII,您还可以找到Rfc2898DeriveBytes一个有用的类来生成密钥。

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

Hope it helps.

希望能帮助到你。