【问题标题】:RSA decrypting a Web.config sectionRSA 解密 Web.config 部分
【发布时间】:2018-07-16 23:53:09
【问题描述】:

我正在尝试从外部 Powershell 脚本中解密使用 RSA 加密的 Web.config 部分。该部分内容为:

<connectionStrings 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>.......</CipherValue>
          </CipherData>
        </EncryptedKey>
      </KeyInfo>
      <CipherData>
        <CipherValue>.......</CipherValue>
      </CipherData>
    </EncryptedData>
</connectionStrings>

代码如下:

[xml]$x = Get-Content "$Path\Web.config"
$Prov = New-Object System.Configuration.RsaProtectedConfigurationProvider
$Prov.Decrypt($x.configuration.connectionStrings.EncryptedData)

它是通过配置所在服务器上的远程 Powershell 执行的。该帐户是管理员,因此应该可以使用本地机器密钥。我收到一个错误:

Value cannot be null. Parameter name: keyName

相同的模提供程序名称片段适用于 DPAPI 加密部分。键名就在该部分中。我在这里错过了什么?

更新:当 Web 代码执行此操作时,它首先在提供程序上调用 Initialize()。我已经模仿了 Initialize 调用的参数。它们来自机器。配置。

$nv = New-Object System.Collections.Specialized.NameValueCollection
$nv.Add("description", "Uses RsaCryptoServiceProvider to encrypt and decrypt")
$nv.Add("keyContainerName", "NetFrameworkConfigurationKey")
$nv.Add("cspProviderName", "")
$nv.Add("useMachineContainer", "true")
$nv.Add("useOAEP", "false")
$Prov.Initialize("RsaProtectedConfigurationProvider", $nv)

现在我得到一个不同的错误:“Bad data”。

更新 2:尝试在该文件上删除 aspnet_regiis,得到相同的“错误数据”错误。但是该站点本身似乎已启动并正在运行并且可以识别数据库。也许connectionString部分毕竟损坏了,网站把它带到别处了。

【问题讨论】:

  • 如果您愿意,请尝试从 System.Web.Configuration 库中进行操作,我可以提供 sn-p 吗?

标签: asp.net powershell web-config


【解决方案1】:

我不确定是否通过 Powershell 执行此操作,但这是我通过网页上的代码隐藏手动执行的操作。此处可能有线索。如果没有帮助,我可以删除此答案。

protected void btnEncryptConnStrings_Click(object sender, EventArgs e)
{
    // Open web.config file as a configuration object to get information.
    Configuration objConfigFile = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);

    // Work with the <connectionStrings> section.
    ConfigurationSection connectionStrings = objConfigFile.GetSection("connectionStrings");

    if(connectionStrings != null)
    {
        // Only encrypt the section if it is not already protected.
        if(!connectionStrings.SectionInformation.IsProtected)
        {
            // Encrypt the <connectionStrings> section using the
            // DataProtectionConfigurationProvider provider (see notes at top of file).
            connectionStrings.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider"); // alt: DataProtectionConfigurationProvider

            objConfigFile.Save();

            // other stuff.
        }
    }
}

【讨论】:

  • 代码依赖于在 ASP.NET 应用程序中。 OpenWebConfiguration() 不接受 Web.config 的绝对路径,它需要一个虚拟路径并尝试针对当前站点解析它。我的脚本独立运行。
猜你喜欢
  • 1970-01-01
  • 2023-04-05
  • 1970-01-01
  • 2012-08-31
  • 1970-01-01
  • 2015-03-16
  • 1970-01-01
  • 2012-03-13
  • 2015-02-27
相关资源
最近更新 更多