指定的初始化向量 (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);
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题是您的初始化向量大小需要为 16 字节。
您的初始向量大小为 14 字节。
您需要将初始向量的大小增加 2 个字节,您的代码才能正常工作。
示例:
然后,您将获得包含当前代码和提供的示例 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:
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 需要使用多少字节:
BlockSize 以位为单位,因此 128 位 / 8 给出 16 个字节的 ASCII,您还可能会发现
Rfc2898DeriveBytes
是一个用于生成有用的类键。希望能帮助到你。
You should be able to check how many bytes the IV needs to be using:
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.Hope it helps.
如果有人将代码从 .NET 框架迁移到 .NET Core 并开始在
RijndaelManaged.CreateEncryptor
上收到此异常:您的旧感冒正在发挥作用,因为“.NET Framework 允许 IV 大于 64 位并截断它们"。要解决此问题,请参阅 Kevin Jones 评论:“只需将 IV 更改为仅前 8 个字节"
因此,作为示例:
将变为:
另外值得注意的是,“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:
would become:
Also worth noting, "Rijndael class is the predecessor of the Aes algorithm. You should use the Aes algorithm instead of Rijndael."