C# 中的 Rijndael 解密
我需要使用 Rijndael 和这些值解密字符串:
密钥大小 - 192
块大小 - 128
密钥 - cmdAj45F37I5ud2134FDg2fF
当我使用下面的代码时,出现错误:字符串大小不正确,任何人都可以帮助我吗?
public static string DecryptRijndael(string value, string encryptionKey)
{
var key = Encoding.UTF8.GetBytes(encryptionKey); //must be 16 chars
var rijndael = new RijndaelManaged
{
BlockSize = 128,
IV = key,
KeySize = 192,
Key = key
};
var buffer = Convert.FromBase64String(value);
var transform = rijndael.CreateDecryptor();
string decrypted;
using (var ms = new MemoryStream())
{
using (var cs = new CryptoStream(ms, transform, CryptoStreamMode.Write))
{
cs.Write(buffer, 0, buffer.Length);
cs.FlushFinalBlock();
decrypted = Encoding.UTF8.GetString(ms.ToArray());
cs.Close();
}
ms.Close();
}
return decrypted;
}
I need to decrypt a string using Rijndael and those values:
key size - 192
block size - 128
key - cmdAj45F37I5ud2134FDg2fF
When I'm using the code below I get an error : string size illigle, can anyone help me?
public static string DecryptRijndael(string value, string encryptionKey)
{
var key = Encoding.UTF8.GetBytes(encryptionKey); //must be 16 chars
var rijndael = new RijndaelManaged
{
BlockSize = 128,
IV = key,
KeySize = 192,
Key = key
};
var buffer = Convert.FromBase64String(value);
var transform = rijndael.CreateDecryptor();
string decrypted;
using (var ms = new MemoryStream())
{
using (var cs = new CryptoStream(ms, transform, CryptoStreamMode.Write))
{
cs.Write(buffer, 0, buffer.Length);
cs.FlushFinalBlock();
decrypted = Encoding.UTF8.GetString(ms.ToArray());
cs.Close();
}
ms.Close();
}
return decrypted;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一个(大)问题是使用
UTF8.GetBytes()
从字符串中获取 byte[]。字节数很难控制,也不是很安全。使用
Rfc2898DeriveBytes.GetBytes()< /code>
相反。然后您可以指定所需的长度。
但当然,您在加密时也必须这样做。
我同意卢克关于 IV 的评论
One (big) problem is in using
UTF8.GetBytes()
to get the byte[] from string. It is hard to control the number of bytes and it is not very safe.Use
Rfc2898DeriveBytes.GetBytes()
instead. And then you can specify the desired length.But of course you have to do that while encrypting as well.
And I agrre with Luke's remarks about the IV
您能看到代码中的注释吗?“必须是 16 个字符”?对我来说,你的密钥看起来更像是 24 个字符!
在这种情况下,您将重新使用密钥作为 IV(无论如何都不是推荐的最佳实践),但 IV 的大小必须与块大小匹配,块大小设置为 128 位/16 字节。
话虽如此,我刚才描述的问题应该会给您错误“指定的初始化向量 (IV) 与该算法的块大小不匹配”,而不是“字符串大小不匹配” em>,所以这可能是一个转移注意力的事情。
Can you see the comment in your code that says the key "must be 16 chars"? Your key looks more like 24 characters to me!
In this case you're re-using the key as the IV -- not recommended best practice anyway -- but the size of the IV must match the block size, which is set to 128 bits/16 bytes.
Having said that, the problem I just described should give you the error "Specified initialization vector (IV) does not match the block size for this algorithm", not "string size illigle", so this might be a red herring.
错误是因为输入是 64 位编码的。
IV和key不一样。 IV用于腌制。无论如何,您收到的错误是因为输入是 64 位编码的。所以这样做,错误就会消失。
var解码加密密钥= Base64Decode(加密密钥);
var key = Encoding.UTF8.GetBytes(decodedEncryptionKey);
这是完整的代码:
Error is because of the input being 64 bit encoded.
IV and key is not the same. IV is for salting. Anyway the error you are getting is because the input is 64bit encoded. so do this and the error will go.
var decodedEncryptionKey= Base64Decode(encryptionKey);
var key = Encoding.UTF8.GetBytes(decodedEncryptionKey);
here is the full code: