27 c# encryption rijndael rijndaelmanaged
我正在研究基本加密方法.我正在使用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);
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激.
Cod*_*ker 59
问题是你的初始化向量大小需要是16个字节.
您的初始向量大小为14个字节.
您需要将初始向量的大小增加2个字节,您的代码才能正常工作.
例:
string initVector = "HR$2pIjHR$2pIj12";
Run Code Online (Sandbox Code Playgroud)
然后,您将获得当前代码和提供的示例IV(初始化向量)大小的输出:
hAC8hMf3N5Zb/DZhFKi6Sg ==
本文提供了有关初始化向量的详细说明.
http://en.wikipedia.org/wiki/Initialization_vector
Luk*_*ett 13
您应该能够检查IV需要使用的字节数:
algorithm.BlockSize / 8
Run Code Online (Sandbox Code Playgroud)
BlockSize是以位为单位的,因此128位/ 8给出了16个字节的ASCII,您也可以找到Rfc2898DeriveBytes一个用于生成密钥的有用类.
algorithm.IV = rfc2898DeriveBytesForIV.GetBytes(algorithm.BlockSize / 8);
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你.
如果有人将代码从 .NET 框架迁移到 .NET Core 并开始出现此异常RijndaelManaged.CreateEncryptor:您的老旧感冒正在发挥作用,因为“ .NET 框架允许 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 };
Run Code Online (Sandbox Code Playgroud)
会成为:
// Rename field if desired.
private static byte[] IV_192 = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
Run Code Online (Sandbox Code Playgroud)
另外值得注意的是,“ Rijndael 类是 Aes 算法的前身。您应该使用 Aes 算法而不是 Rijndael。 ”