在clickonce应用程序中安全地存储服务帐户凭据

我正在编写一个ClickOnce应用程序,它运行带有服务帐户凭据的批处理文件进程。 我需要存储服务帐户凭据,以便程序可以在运行进程之前将用户名/密码添加到process.startinfo属性中。 用户不知道此密码,因此没有提示他们输入密码。 我相信这意味着我无法存储哈希并以这种方式validation密码,我生成的哈希值必须是可逆的,以便它可以将正确的密码添加到startinfo属性。 我在这个网站上搜索并提出了一个可行的Frankenstein类型的解决方案,但它不是很安全。 目前,我使用此方法加密密码,存储加密值,然后使用decrypt方法在运行时获取密码(加密方法在运行时从不运行,我在调试期间在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破解它,因为它只是添加了一层混淆。 在应用程序中存储这些凭据的最佳/正确方法是什么?

加密密钥位于专用服务器上。

密码将与要加密的ID和为数据库存储返回的加密密码一起发送到服务器。

当需要密码时,向具有id的专用服务器发出请求,并返回解密的密码。

密码永远不会保存到磁盘,密钥永远不会在专用服务器上提供。

专用服务器有点像穷人HSM。

这是加密,而不是散列。 加密密钥是秘密的,随机IV与专用服务器上的id一起保存。 密钥不可用且与密码无关,因此没有比对加密密钥powershell攻击更好的攻击,而加密密钥基本上是强大的攻击强暴。

服务器需要非常安全,只需要两个因子登录,并且不可用于Internet。