【问题标题】:Where to store secret key of the c# applicationc#应用程序的密钥存储在哪里
【发布时间】:2012-11-27 07:12:08
【问题描述】:

有类似的问题 How to Manage Key in a Symmetric Algorithm Where to store a secret key to use in a SHA-1 hash?

我的问题是一样的,但我想问的不同

我有 C# 应用程序。我正在加密应用程序中的一些数据。对于加密,我使用密钥或密码。解密也需要同样的东西。

在哪里/如何在应用程序中存储此密钥或密码?它很容易从反射中查看字符串密码。我可能会使用一些组合来生成密码,但一些聪明的人可以通过一些努力猜到。

是否有任何安全的方式来存储或管理应用程序中用于加密数据的秘密密码?

【问题讨论】:

  • 我不确定,但可能是我们使用 app.config 应用密钥并对其进行加密

标签: c# encryption


【解决方案1】:

我怀疑是否有任何安全的方式来存储密钥。最终,您的程序必须获得密钥的访问权,而破解者可以通过逆向工程轻松计算出这是如何发生的,并将该字符串重定向到他们想要的任何地方。

您最好的选择是:

  • 尽可能混淆密钥。这使得访问“密钥”变得更加困难,但并不意味着不可能(见上文)。与其将其存储为字符串,不如使用函数生成它,或者使用种子并将其传递给函数以获取秘密字符串。

  • 如果您的用例允许,请使用公钥/私钥对。只有当您希望您的应用程序加密数据,将其发送到您的服务器,然后您想要解密它时,它才有效。在这种情况下,您将公钥嵌入到应用程序中(无论破解者是否发现),并将私钥留给您自己或您的服务器。

【讨论】:

  • 不完全是 BlokeTech 的答案,而是肯定的指导。感谢您的回答。我认为如果加密和解密都在同一台机器上,那么智能破解者无论如何都可以破解它。
  • 那么你将如何存储私钥呢?
【解决方案2】:

如果您将密钥存储为应用程序设置,并加密应用程序设置,那么我认为您很省钱。

您可以使用以下代码来加密 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;
    }
}

并像这样使用它(例如在您的 Main() 方法中):

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

【讨论】:

  • 欢迎提供更多信息:为什么没有密钥,它是如何工作的,是否修改了原始配置文件,何时必须运行(每次使用时,在开始之前), ,, 仅代码答案并不容易
猜你喜欢
  • 1970-01-01
  • 2017-05-30
  • 2011-04-30
  • 1970-01-01
  • 1970-01-01
  • 2010-11-10
  • 2012-09-06
  • 2013-02-19
相关资源
最近更新 更多