【发布时间】:2017-09-22 15:05:22
【问题描述】:
我在使用 AES 和 Rfc2898DeriveBytes 时遇到了困惑。这是我找到的代码......
public static string Decrypt(string encryptionKey, string cipherValue)
{
byte[] cipherBytes = Convert.FromBase64String(cipherValue);
using (var encryptor = Aes.Create())
{
var pdb = new Rfc2898DeriveBytes(encryptionKey, new byte[] { (13 element byte array) });
if (encryptor != null)
{
encryptor.Key = pdb.GetBytes(32);
encryptor.IV = pdb.GetBytes(16);
using (var ms = new MemoryStream())
{
using (var cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(cipherBytes, 0, cipherBytes.Length);
cs.Close();
}
cipherValue = Encoding.Unicode.GetString(ms.ToArray());
}
}
}
return cipherValue;
}
所以,“cipherValue”是一个加密字符串......以及“encryptionKey”。如何使用 AES 和 Rfc2898Derive 字节的其他示例似乎不适合此代码。我见过的其他示例使用纯文本代替上面的“encryptionKey”参数,但这些示例通常演示的是加密而不是解密。
此代码用于解密我的应用程序配置文件中的密码。加密已经完成,我没有可用资源告诉我它是如何完成的。我假设密码是使用指定的“encryptionKey”和盐值加密的,以及默认的 1000 次迭代和最大大小的密钥和 IV。
我主要对“encryptionKey”参数如何影响事物感到好奇。 “cipherValue”是被解密的内容,并给了我正确的输出。这里采用了什么方法,与我见过的其他示例相比,它有什么优势(如果有的话)?
加密和安全性还不是我的强项...如果我遗漏了任何可能更清楚地说明这一点的重要内容,请告诉我。提前致谢!
【问题讨论】:
-
必须随机选择盐(Rfc2898DeriveBytes 的第二个参数)。不要使用静态盐,因为这会使密码具有确定性,因此在语义上不安全。观察密文的攻击者可以确定之前发送相同消息前缀的时间。盐不是秘密的,因此您可以将其与密文一起发送。通常,它只是简单地添加到密文中,并在解密之前被切掉。
标签: c# encryption cryptography aes rfc2898