【发布时间】:2012-12-11 02:59:53
【问题描述】:
所以我一直在尝试用 c# 研究 AES 加密。不过,我对 IV 有点坚持,只是我很难理解加密,我希望其他人的更多解释可以让我越过这堵墙。
无论如何,我发现了一些关于 IV 的内容以及如何将其与加密消息一起发送到我的网络服务(因为这是我使用 AES 的主要目的)。
这是我找到的文章
http://old.nabble.com/AES-decryption-with-different-IV-td31004365.html
它说你可以在传递到解密它的目的地时将你的 IV 附加到消息中。
话虽如此,我的问题是,它们是否与从用于加密消息的 IV 转换而来的普通字符串有关?或者您是以字节为单位附加 IV 的?
这是我的主要解密
public string DecryptString(string encryptedString, string key)
{
if (encryptedString == null || encryptedString.Length <= 0)
throw new ApplicationException("No string to decrypt.");
if (key == null || key.Length <= 0)
throw new ApplicationException("No key.");
byte[] cipherText = Convert.FromBase64String(encryptedString);
Rijndael aes = new RijndaelManaged();
aes.Key = StringToByte(key);
aes.IV = GetIV(cipherText);
ICryptoTransform decryptor = aes.CreateDecryptor();
throw new NotImplementedException();
}
我的 GetIV() 是
static public byte[] GetIV(string cipherText)
{
// IV will be attached to the beginning of the encrypted String
// needs the length of the IV
int ivStringLength = 16;
string ivString = cipherText.Substring(0, ivStringLength);
byte[] ivByte = StringToByte(ivString);
return ivByte;
}
现在,基于我阅读的另一个线程
您似乎从加密中获得了 IV,但它不是前 N 个字符,而是前 N 个字节。这让我很困惑!
还有一件事,IV 不应该用于使用密钥加密消息。上一篇文章,他/她是用KEY和IV加密,然后在消息前面再次附加IV,然后将其转换为字符串?
编辑:抱歉,我忘了说我是创建 Web 服务的人,这是我正在尝试实施的加密。
【问题讨论】:
-
您不能只是将 IV 发送到任何您喜欢的地方和任何方式,并期望 Web 服务知道在哪里可以找到它。您必须以与网络服务兼容的方式发送。向网络服务所有者询问规范。
-
我是创建 Web 服务的人。这就是我想要实现的。我需要客户要求在消息上附加 IV。但我正试图从我的角度弄清楚如何正确加密和解密它。
标签: c# encryption aes