简体   繁体   English

在哪里存储c#应用程序的密钥

[英]Where to store secret key of the c# application

There are similar questions How to Manage Key in a Symmetric Algorithm Where to store a secret key to use in a SHA-1 hash? 类似的问题如何在对称算法中管理密钥在 哪里存储要在SHA-1哈希中使用的密钥?

My question is same, But I want to ask it differently 我的问题是一样的,但我想以不同的方式提出这个问题

I have C# application. 我有C#应用程序。 I am encrypting some data in the application. 我正在加密应用程序中的一些数据。 For encryption I am using secret key or password. 对于加密,我使用密钥或密码。 This same thing is needed for decryption. 解密需要同样的事情。

Where/how to store this secret key or password in application? 在应用程序中存储/如何存储此密钥或密码的位置? its easy to view string password from reflection. 它很容易从反射中查看字符串密码。 I may use some combination to generate password, but some smart guys can guess that with some efforts. 我可能会使用一些组合来生成密码,但有些聪明人可以通过一些努力来猜测。

Is there any secured way to store or manage secret password which is used in application to encrypt data? 是否有任何安全的方法来存储或管理在应用程序中用于加密数据的密码?

I doubt there is any guaranteed secure way to store the key. 我怀疑有任何保证安全的方式来存储密钥。 Ultimately your program has to get access to the key, and a cracker could easily work out how that is happening via reverse engineering and redirect that string to wherever they want to. 最终,您的程序必须能够访问密钥,并且一个破解者可以通过逆向工程轻松地解决这种情况,并将该字符串重定向到他们想要的任何地方。

Your best options are to: 您最好的选择是:

  • Obfuscate the key as much as possible. 尽可能地模糊密钥。 This makes it more difficult to access the "secret key" but does not make it impossible (see above). 这使得访问“秘密密钥”变得更加困难,但却无法实现(见上文)。 Rather than storing it as a string, generate it using a function, or use a seed and pass that through a function to get the secret string. 而不是将其存储为字符串,使用函数生成它,或使用种子并通过函数传递它以获取秘密字符串。

  • If your use case allows it, use a public/private key pair. 如果您的用例允许,请使用公钥/私钥对。 It only works if you want your application to encrypt the data, send it to your servers, and then you want to decrypt it. 它只适用于您希望应用程序加密数据,将其发送到您的服务器,然后您想要解密它。 In this case, you embed the public key into the application (doesn't matter if crackers discover that), and keep the private key to yourself or your server. 在这种情况下,您将公钥嵌入应用程序(如果破解者发现这一点并不重要),并将私钥保留给您自己或您的服务器。

If you store the key as an app-setting, and encrypt the app-settings, then I think you're pretty save. 如果您将密钥存储为应用程序设置,并加密应用程序设置,那么我认为您很节省。

You can use the following code to encrypt sections of the app.config. 您可以使用以下代码加密app.config的各个部分。

using System;
using System.Configuration;

public static class ConfigurationEncryptor {
    [Flags]
    public enum ConfigurationSectionType {
        ConnectionStrings = 1,
        ApplicationSettings = 2
    }

    /// <summary>
    /// Encrypts the given sections in the current configuration.
    /// </summary>
    /// <returns>True is the configuration file was encrypted</returns>
    public static bool Encrypt(ConfigurationSectionType section) {
        bool result = false;

        Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        if (config == null)
            throw new Exception("Cannot open the configuration file.");

        if (section.HasFlag(ConfigurationSectionType.ConnectionStrings)) {
            result = result || EncryptSection(config, "connectionStrings");
        }

        if (section.HasFlag(ConfigurationSectionType.ApplicationSettings)) {
            result = result || EncryptSection(config, "appSettings");
        }

        return result;
    }

    /// <summary>
    /// Encrypts the specified section.
    /// </summary>
    /// <param name="config">The config.</param>
    /// <param name="section">The section.</param>
    private static bool EncryptSection(Configuration config, string section) {
        ConfigurationSection currentSection = config.GetSection(section);
        if (currentSection == null)
            throw new Exception("Cannot find " + section + " section in configuration file.");
        if (!currentSection.SectionInformation.IsProtected) {
            currentSection.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
            config.Save();

            // Refresh configuration
            ConfigurationManager.RefreshSection(section);

            return true;
        }
        return false;
    }
}

And use it like this (eg in your Main() method): 并像这样使用它(例如在你的Main()方法中):

ConfigurationEncryptor.Encrypt(
    ConfigurationEncryptor.ConfigurationSectionType.ApplicationSettings |
    ConfigurationEncryptor.ConfigurationSectionType.ConnectionStrings
);

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

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