【发布时间】: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