5

在我的配置文件中,我有一些敏感信息我想加密以提高安全性。

这是我的代码(按预期工作):

class Program
{
    static void Main(string[] args)
    {
        System.Configuration.ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
        fileMap.ExeConfigFilename = @"D:\Web_S\Prep\test\test.exe.config";
        System.Configuration.Configuration configuration = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
        string userNameWithoutEncryption = configuration.AppSettings.Settings["username"].Value;
        EncryptAppSettings("appSettings", configuration);
    }

    protected static void EncryptAppSettings(string section, Configuration configuration)
    {    
        AppSettingsSection objAppsettings = (AppSettingsSection)configuration.GetSection(section);
        objAppsettings.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");
        objAppsettings.SectionInformation.ForceSave = true;
        configuration.Save(ConfigurationSaveMode.Modified);

    }
}

.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="username" value="a2zmenu"/>
    <add key="password" value="password"/>
  </appSettings>
</configuration>

.config 加密看起来像这样:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="customAppSettings" type="System.Configuration.NameValueSectionHandler" />
  </configSections>
  <appSettings configProtectionProvider="RsaProtectedConfigurationProvider">
    <EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element"
      xmlns="http://www.w3.org/2001/04/xmlenc#">
      <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc" />
      <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
        <EncryptedKey xmlns="http://www.w3.org/2001/04/xmlenc#">
          <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5" />
          <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
            <KeyName>Rsa Key</KeyName>
          </KeyInfo>
          <CipherData>
<CipherValue>09+Lm23xDWWnAZFOagh3NRwp5tzad+3oedvTgoeWqunQBiAfk9UGfGxriZg6snwwANUDzOANZ+wOFUb6qa0Atf
NgSd6b4FFSKTqzkfLlk+S9GtPSAVrRaLU9
/Q2Qu7oxoSbhW7NWtengJbEZrFm+GqlLlm08w8Np/y03DMExFeA=</CipherValue>
          </CipherData>
        </EncryptedKey>
      </KeyInfo>
      <CipherData>
<CipherValue>qSYRXNEKhbwNodH60c7qoWeKZ2QKVQmizPXVGCgHVZPMQ4F+XDqlZa2OyIin0kEI3j8pCjNL097RlZClgdd
gPEd61AEw6DXJc43Z98obNFHmXfK9aS67qEtO6E
T+qCWQq2ZRbfK6xZ6jlfeink35/veUmoxAmDXrkwdrbQVKv98=</CipherValue>
      </CipherData>
    </EncryptedData>
  </appSettings>
</configuration>

我有以下问题: 让诸如此类的信息安全吗?

   <EncryptedKey xmlns="http://www.w3.org/2001/04/xmlenc#">
      <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5" />

在 .config 中?

不能用这些信息解密吗?文件加密后,您能否确认我可以评论此行:

  EncryptAppSettings("appSettings", configuration);

当我尝试在使用此行加密文件后获取用户名值时:

string userNameafterEncryption = configuration.AppSettings.Settings["username"].Value;

即使我的文件现在已加密,我也会得到解密的值。我不明白为什么...

谢谢您的帮助

4

1 回答 1

21

首先,您需要了解配置加密如何以及从哪些配置真正保护您。RsaProtectedConfigurationProvider可以在两个地方存储用于实际加密的私钥。第一个是

C:\Documents and Settings\All Users\Application Data\Microsoft\Crypto\RSA\MachineKeys

这是存储机器范围密钥的文件夹。默认情况下,任何用户都可以访问此文件夹,但您需要提升权限(以管理员身份运行)才能读取此文件夹中的文件(同样,默认情况下)。

第二个可能的位置是

C:\Documents and Settings\[user name]\Application Data\Microsoft\Crypto\RSA

这是用户级别的位置 - 只有特定用户才能访问它。

默认情况下,RsaProtectedConfigurationProvider将使用机器级位置,这UseMachineContainer由此提供程序的属性控制。默认配置在机器级配置文件(位于C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\machine.config)中定义,定义如下:

<add name="RsaProtectedConfigurationProvider" type="System.Configuration.RsaProtectedConfigurationProvider,System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" description="Uses RsaCryptoServiceProvider to encrypt and decrypt" keyContainerName="NetFrameworkConfigurationKey" cspProviderName="" useMachineContainer="true" useOAEP="false"/>

如果您想使用用户级位置加密您的部分,您可以在自己的 app.config 文件中覆盖此配置(请参阅此处的更多信息)。

现在,当您知道所有这些后,您可以做出明智的决定,是否需要加密您的部分,如果是,则 - 使用哪个位置。

  1. 如果您使用机器级位置(默认) - 您的应用程序应该在提升(在管理员下)运行以对您的部分进行加密解密。如果有人可以访问您的配置文件 - 如果没有管理员权限,他将无法解密它。在某些环境(尤其是公司)中,运行提升可能是一个问题。

  2. 如果您使用用户级位置 - 您的应用程序不需要运行提升。只有加密部分的用户可以稍后解密它。如果有人以不同的用户(想象一些公司域)访问您的计算机并窃取文件 - 他将无法解密它。

您可以为特定用户\机器预加密您的部分(如果需要,可以将加密部分的密钥从一台机器导出到另一台机器)或在第一次运行时要求用户输入敏感数据(例如数据库密码) - 然后将该数据保存到 app.config 并加密部分。

至于为什么你会自动获得解密的价值——那是因为如果可能的话,它是动态解密的。您似乎默认以管理员身份运行(例如禁用您的 UAC),因此您可以访问用于加密的密钥,因此可以解密。如果您在没有管理员的情况下运行 - 当您尝试访问加密值时它将引发异常。

于 2016-05-12T18:01:57.170 回答