如何使用 RsaProtectedConfigurationProvider 加密/解密配置文件部分

How to encrypt / decrypt a configuration file section with RsaProtectedConfigurationProvider

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

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

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;

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

感谢您的帮助

首先,您需要了解加密如何以及从哪些配置中真正保护您。 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 文件中覆盖此配置(查看更多 here)。

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

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

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

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

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