【发布时间】:2016-11-14 22:02:12
【问题描述】:
我正在编写一个 ClickOnce 应用程序,该应用程序使用服务帐户凭据运行批处理文件进程。我需要存储服务帐户凭据,以便程序可以在运行进程之前将用户名/密码添加到 process.startinfo 属性。用户不知道此密码,因此不会提示他们输入密码。我相信这意味着我无法以这种方式存储哈希并验证密码,我生成的哈希值必须是可逆的,以便它可以将正确的密码添加到 startinfo 属性。我在这个网站上搜索并想出了一个有效的科学怪人类型的解决方案,但它不是很安全。目前,我使用此方法加密密码,存储加密值,然后在运行时使用解密方法获取密码(加密方法在运行时从未运行,我在调试期间在 Visual Studio 中运行它,复制值,然后在下面的解密方法中使用该值):
// used to generate decrypted acct creds
private void EncryptText(string plaintext)
{
string outsrt = null;
RijndaelManaged aesAlg = null;
try
{
// generate key from secret and salt
Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(sharedsecret, _salt);
aesAlg = new RijndaelManaged();
aesAlg.Key = key.GetBytes(aesAlg.KeySize / 8);
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
using (MemoryStream mEncrypt = new MemoryStream())
{
// prepend the IV
mEncrypt.Write(BitConverter.GetBytes(aesAlg.IV.Length), 0, sizeof(int));
mEncrypt.Write(aesAlg.IV, 0, aesAlg.IV.Length);
using (CryptoStream csEncrypt = new CryptoStream(mEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{
// write all data to the stream
swEncrypt.Write(plaintext);
}
}
outsrt = Convert.ToBase64String(mEncrypt.ToArray());
}
}
finally
{
if (aesAlg != null)
aesAlg.Clear();
}
Console.WriteLine(outsrt);
}
解密方法如下:
private string GetServiceAcctPW()
{
// Declare the RijndaelManaged object
// used to decrypt the data.
RijndaelManaged aesAlg = null;
// Declare the string used to hold
// the decrypted text.
string plaintext = null;
try
{
// generate the key from the shared secret and the salt
Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(sharedsecret, _salt);
// Create the streams used for decryption.
byte[] bytes = Convert.FromBase64String("EncryptedValueHere");
using (MemoryStream msDecrypt = new MemoryStream(bytes))
{
// Create a RijndaelManaged object
// with the specified key and IV.
aesAlg = new RijndaelManaged();
aesAlg.Key = key.GetBytes(aesAlg.KeySize / 8);
// Get the initialization vector from the encrypted stream
aesAlg.IV = ReadByteArray(msDecrypt);
// Create a decrytor to perform the stream transform.
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
// Read the decrypted bytes from the decrypting stream
// and place them in a string.
plaintext = srDecrypt.ReadToEnd();
}
}
}
catch(Exception e)
{
Console.WriteLine("Error decrypting password");
Console.WriteLine(e.StackTrace);
logger.WriteToLog(Logger.LogCodes.ERROR, "Error decrypting service account password");
MessageBox.Show("An error occurred while trying to start the installation process\nPlease contact the Service Desk for further assistance");
}
finally
{
// Clear the RijndaelManaged object.
if (aesAlg != null)
aesAlg.Clear();
}
return plaintext;
}
此代码运行良好,但是,我知道它不安全。我的代码审查员说他能够在一个小时内用 dotPeek 破解它,因为它只是增加了一层混淆。在应用程序中存储这些凭据的最佳/正确方法是什么?
【问题讨论】:
-
您不能完全保存硬编码的密钥。至少在运行时您需要获取密钥,而调试器可以简单地获取它们。忘记安全存储密钥的想法。只有使用专用服务器等才能提高安全性...
-
不要加密密码,当攻击者得到数据库时,他也会得到加密密钥。如果您必须在另一个服务器或 HSM 上进行加密,则密码在 Internet 连接的服务器上不可用。
-
我完全同意,如果将它们存储在应用程序中,它们将永远不会 100% 安全。您提到专用服务器可以提高安全性。这是否意味着将密钥存储在这些服务器上,在运行时将其作为 SecureString 获取并将其传递给 startinfo 属性?
-
@zaph ,所以我会生成 salt 和 hash 并存储它,但是,PBKDF2、Bcrype 等单向加密方法不是吗?我能否从 salt 和 hash 中获取密码并将其添加到 startinfo 属性中?我为我的无知道歉,因为这些类型的方法在大学期间从来都不是我的强项。
-
感谢@zaph 的帮助和指导,非常感谢。
标签: c# security encryption clickonce credentials