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

查看:405
本文介绍了指定的初始化向量(IV)与该算法的块大小不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用基础加密方法。我正在使用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);

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

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

任何帮助将不胜感激。

解决方案

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



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

您将需要将初始向量的大小增加2个字节,代码将会起作用。



示例: p>

  string initVector =HR $ 2pIjHR $ 2pIj12; 

然后您将使用当前代码获取输出,并提供示例IV(初始化向量)大小:



hAC8hMf3N5Zb / DZhFKi6Sg ==



本文提供了有关初始化向量的一个很好的解释。 p / / / / Initialization_vector


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

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

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

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

这篇关于指定的初始化向量(IV)与该算法的块大小不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆