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

发布于 2024-07-22 20:26:39 字数 1375 浏览 7 评论 0原文

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

我的代码之前可以工作,但是有些事情发生了变化,我无法完全弄清楚。

当我运行我的代码时,出现以下错误;

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

这是我的代码:

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

任何帮助将不胜感激。

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.

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

演多会厌 2024-07-29 20:26:39

问题是您的初始化向量大小需要为 16 字节。

您的初始向量大小为 14 字节。

您需要将初始向量的大小增加 2 个字节,您的代码才能正常工作。

示例:

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

然后,您将获得包含当前代码和提供的示例 IV(初始化向量)大小的输出:

hAC8hMf3N5Zb/DZhFKi6Sg==

本文很好地解释了初始化向量是什么。

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

The problem is your initialization vector size needs to be 16 bytes.

Your initial vector size is 14 bytes.

You will need to increase the size of your initial vector by 2 bytes and your code will work.

Example:

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

You will then get the output with your current code and the example IV (initialization vector) size provided:

hAC8hMf3N5Zb/DZhFKi6Sg==

This article provides a good explanation on what the initialization vector is.

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

红尘作伴 2024-07-29 20:26:39

您应该能够检查 IV 需要使用多少字节:

algorithm.BlockSize / 8

BlockSize 以位为单位,因此 128 位 / 8 给出 16 个字节的 ASCII,您还可能会发现 Rfc2898DeriveBytes 是一个用于生成有用的类键。

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

希望能帮助到你。

You should be able to check how many bytes the IV needs to be using:

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.

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

Hope it helps.

水晶透心 2024-07-29 20:26:39

如果有人将代码从 .NET 框架迁移到 .NET Core 并开始在 RijndaelManaged.CreateEncryptor 上收到此异常:您的旧感冒正在发挥作用,因为“.NET Framework 允许 IV 大于 64 位并截断它们"。

要解决此问题,请参阅 Kevin Jones 评论:“只需将 IV 更改为仅前 8 个字节"

因此,作为示例:

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

将变为:

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

另外值得注意的是,“Rijndael 类是 Aes 算法的前身。您应该使用 Aes 算法而不是 Rijndael。

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".

To resolve see Kevin Jones comment: "simply change your IV to only the first 8 bytes"

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."

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文