【发布时间】:2021-05-08 09:25:54
【问题描述】:
我需要使用 128 位 CBC 的 AES 加密字符串
为此我需要将块大小设置为什么?
var iv = "0000000000000000000000000000000000000000000000000000000000000000".ToByteArray();
using (Aes myAes = Aes.Create())
{
myAes.Mode = CipherMode.CBC;
// Encrypt the string to an array of bytes.
byte[] encrypted = EncryptStringToBytes_Aes(xml, myAes.Key, iv);
// Decrypt the bytes to a string.
string roundtrip = DecryptStringFromBytes_Aes(encrypted, myAes.Key, iv);
//Display the original data and the decrypted data.
Console.WriteLine("Original: {0}", xml);
Console.WriteLine("Round Trip: {0}", roundtrip);
}
奇怪的是,我的规格似乎不适合 IV
反正我还没有告诉对方 IV 是什么所以我想我必须使用一个 0 的字符串,我认为它有 64 个字符长所以我使用了上面的代码
有人可以帮忙吗?
【问题讨论】:
-
IV是密钥,在ecrypt和decrypt方法上必须使用相同的密钥。密钥不能为空。此外,您不会随消息发送密钥。密钥必须以安全方式在加密位置和解密位置之间发送。发送带有消息的钥匙就像给小偷钥匙一样。
-
ToByteArray()是扩展方法的糟糕名称,因为它不会告诉读者它如何解释字符串,或者即使它执行常规解码(从十六进制或基数 64)或字符编码(是的,GetBytes也很糟糕,微软,你从atoi开始就没有学过)。
标签: c# encryption aes