【问题标题】:Encrypting externally stored App blocks with exportable Key Provider使用可导出的密钥提供程序加密外部存储的应用程序块
【发布时间】:2008-12-19 11:46:43
【问题描述】:

我已经尝试了很长时间来弄清楚如何加密存储在名为 dev_entlib.config 的外部文件中的应用程序块

我可以在 entlib (4.1) 中看到可以使用默认保护提供程序来加密块,但是我确实需要将此应用程序部署在不同的服务器上,因此我需要导出用于加密应用程序的 keyProvider阻止这些服务器。

到目前为止,我所做的是将自定义受保护的配置提供程序添加到 .net v2.0* 任何文件夹(以及所有目标服务器)中的 machine.config 文件中

自定义提供者是这样的

<add name="MyCompanyProvider" 
    type="System.Configuration.RsaProtectedConfigurationProvider, 
          System.Configuration, Version=2.0.0.0, Culture=neutral, 
          PublicKeyToken=b03f5f7f11d50a3a,
         processorArchitecture=MSIL"
    keyContainerName="MyKey" 
    useMachineContainer="true" />

它很好地位于其他默认提供程序旁边,甚至在 Entlib 配置工具中具有设计时支持。然后,我为要加密的每个块选择保护提供程序。

查看 dev_entlib.config,显示该块确实是使用我的提供程序加密的。我的提供者使用我的密钥容器。因此,应该使用我的密钥容器对块进行加密。然后我使用以下命令将“MyKey”导出到 xml 文件:

c:\Windows\Microsoft.NET\Framework\v2.0.50727>aspnet_regiis.exe -px "MyKey" "C:\keys.xml" -pri
Exporting RSA Keys to file...
Succeeded!

然后这个密钥文件被复制到我的 sysTest 服务器上,它被导入并被授予“NT Authority\Network Services”和“ASPNET”的访问权限

然后我复制我的加密 web.config 和 dev_entlib.config 并尝试在一个小页面中显示连接字符串,该页面使用 .net ConfigurationManager 获取 ConnectionStrings 集合并将它们显示在页面上。此页面在 IIS 下运行,进程标识为“NT Authority\Network Services”。

问题是,它不起作用!存在错误数据错误或“无法使用提供程序 MyCompanyProvider 解密”。

这种方法对我来说似乎合乎逻辑,但仍然失败。

有人有其他建议吗?

【问题讨论】:

    标签: encryption enterprise-library aspnet-regiis.exe


    【解决方案1】:

    使用企业库配置工具使用您的自定义 RSA 密钥容器加密外部企业库配置文件。

    • EntLib (4.1) 使用默认保护提供程序 RsaProtectedConfigurationProvider。但是可以在您的配置文件中删除此提供程序,并将其替换为您自己的同名提供程序,然后可以指向您的自定义密钥提供程序:“MyKey”。
    • 您应该将这个 configProtectedData 部分添加到具有您要加密的区域的配置文件中(例如,您的外部文件:*dev_entlib.config*)。您根本不需要修改 machine.config 文件。
    • 然后您可以从企业库配置应用程序中为 数据访问应用程序块 ProtectionProvider 选择 RsaProtectedConfigurationProvider
    • 如果您使用的是 Vista、Windows 7、Windows 2008,则必须使用 以管理员身份运行 打开此 EntLibConfig.exe
      • 否则会报错:
        • Failed to encrypt the section 'connectionStrings' using provider 'RsaProtectedConfigurationProvider'. Error message from the provider: Object already exists.
    • 然后您可以将此加密的 *dev_entlib.config* 连同 web.config 配置文件复制到您的 sysTest 服务器。使用该 sysTest 服务器上的企业库配置工具打开 web.config 文件应该得到错误:
      • Failed to decrypt using provider 'RsaProtectedConfigurationProvider'. Error message from the provider: Bad Data.

    web.config

    这个文件几乎是空的,只是指向外部数据配置文件:

    <!-- web.config -->
    <configuration>
      <configSections>
        <section name="enterpriseLibrary.ConfigurationSource" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourceSection, Microsoft.Practices.EnterpriseLibrary.Common, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
      </configSections>
      <enterpriseLibrary.ConfigurationSource selectedSource="External Data Configuration File Source">
        <sources>
          <add name="External Data Configuration File Source" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
            filePath="dev_entlib.config" />
        </sources>
      </enterpriseLibrary.ConfigurationSource>
    </configuration>
    

    dev_entlib.config

    此文件包含连接字符串和加密时应使用的保护提供程序:

    <!-- dev_entlib.config -->
    <configuration>
        <configSections>
            <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
     </configSections>
        <dataConfiguration defaultDatabase="MyConnectionStringName" />
     <connectionStrings>
      <add name="cnHnicMediaLibrary" connectionString="Server=MyDbServer; Database=MyDbName; Integrated Security=SSPI"
       providerName="System.Data.SqlClient" />
     </connectionStrings>
      <configProtectedData>
        <providers>
          <remove name="RsaProtectedConfigurationProvider" />
          <add    name="RsaProtectedConfigurationProvider"
            keyContainerName="MyKey"
            useMachineContainer="true"
            description="Uses our own encryption key container so that it will work in a Web Farm setting. We need to trick Enterprise Library, which wants to use the default RsaCryptoServiceProvider to encrypt and decrypt, by replacing this default provider with our own while this configuration is processed!"
            type="System.Configuration.RsaProtectedConfigurationProvider, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        </providers>
      </configProtectedData>
    </configuration>
    

    基于:

    我希望这描述了您收到的错误消息以及如何修复它。

    【讨论】:

      【解决方案2】:

      这似乎还不可能。我的解决方案是将块加密为 web.config 的一部分,然后将这些块复制并粘贴到外部 entLib.config 文件中。这些块应该能够在目标服务器上使用导出的密钥解密。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-07-23
        • 2023-02-01
        • 1970-01-01
        • 2016-04-03
        • 2021-01-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多