简体   繁体   English

app.config 文件是存储密码的安全位置吗?

[英]Is app.config file a secure place to store passwords?

I need to store confidential passwords within the code.我需要在代码中存储机密密码。 I cannot use Hashing techniques as the password itself is needed.我不能使用哈希技术,因为需要密码本身。 How can I store these data securely within an app.config file?如何将这些数据安全地存储在 app.config 文件中?

Are there other ways I could accomplish this securely?还有其他方法可以安全地完成此操作吗?

DPAPI and ProtectData Class is not an option because the keys are system specific eg:connection strings can't be stored this way for different end user systems. DPAPI 和 ProtectData Class 不是一个选项,因为密钥是系统特定的,例如:连接字符串不能以这种方式为不同的最终用户系统存储。

You can use DPAPI (Data protection API) to encrypt certain section of your config files.您可以使用 DPAPI(数据保护 API)来加密配置文件的某些部分。 Your code would still be using ConfigurationManager and decrypting will be taken of care by the framework.您的代码仍将使用 ConfigurationManager 并且解密将由框架处理。 For more information on the same refer to this patterns and practices document How To: Encrypt Configuration Sections in ASP.NET 2.0 Using DPAPI有关相同内容的更多信息,请参阅此模式和实践文档如何:使用 DPAPI 在 ASP.NET 2.0 中加密配置部分

Update更新

To encrypt or decrypt information from your code you could use ProtectedData.Protect & ProtectedData.Unprotect .要加密或解密代码中的信息,您可以使用ProtectedData.Protect & ProtectedData.Unprotect This can be run as a part of custom action in your installer or when the user enters the credentials when using your application.这可以作为安装程序中自定义操作的一部分运行,或者当用户在使用您的应用程序时输入凭据时运行。

Sample Code示例代码

class SecureStringManager
{
    readonly Encoding _encoding = Encoding.Unicode;

    public string Unprotect(string encryptedString)
    {
        byte[] protectedData = Convert.FromBase64String(encryptedString);
        byte[] unprotectedData = ProtectedData.Unprotect(protectedData,
            null, DataProtectionScope.CurrentUser);

        return _encoding.GetString(unprotectedData);
    }

    public string Protect(string unprotectedString)
    {
        byte[] unprotectedData = _encoding.GetBytes(unprotectedString);
        byte[] protectedData = ProtectedData.Protect(unprotectedData, 
            null, DataProtectionScope.CurrentUser);

        return Convert.ToBase64String(protectedData);
    }
}      

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM