我在使用 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”是被解密的内容,并给了我正确的输出。这里使用的是什么方法,与我见过的其他示例相比,这有什么优势(如果有的话)?
加密和安全性还不是我的强项……让我知道我是否遗漏了任何可能更清楚地说明这一点的重要内容。提前致谢!