Asp.net RSA解密Web.config节
我正在尝试从外部Powershell脚本解密使用RSA加密的Web.config节。该部分内容如下:Asp.net RSA解密Web.config节,asp.net,powershell,web-config,Asp.net,Powershell,Web Config,我正在尝试从外部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#"&
<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>
它通过配置所在服务器上的远程Powershell执行。该帐户是管理员,因此本地机器密钥应该可用。我得到一个错误:
Value cannot be null. Parameter name: keyName
相同的模提供程序名称片段适用于DPAPI加密的节。密钥名称就在该部分中。我错过了什么
更新:当Web代码执行此操作时,它首先对提供程序调用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)
现在我得到了一个不同的错误:“坏数据”
更新2:尝试在该文件上搜索aspnet_regis,得到相同的“坏数据”错误。但该网站本身似乎已经启动并运行,并且具有数据库意识。可能连接字符串部分毕竟已损坏,网站将其转移到其他地方。我不确定是否通过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.
}
}
}
protectedvoid btnEncryptConnStrings\u单击(对象发送方,事件参数e)
{
//将web.config文件作为配置对象打开以获取信息。
Configuration objConfigFile=WebConfiguration Manager.OpenWebConfiguration(Request.ApplicationPath);
//与部门合作。
ConfigurationSection ConnectionString=objConfigFile.GetSection(“ConnectionString”);
if(连接字符串!=null)
{
//仅当节尚未受到保护时才对其进行加密。
如果(!ConnectionString.SectionInformation.IsProtected)
{
//使用
//DataProtectionConfigurationProvider提供程序(请参阅文件顶部的注释)。
ConnectionString.SectionInformation.ProtectSection(“RsaProtectedConfigurationProvider”);//alt:DataProtectionConfigurationProvider
objConfigFile.Save();
//其他的东西。
}
}
}
如果需要,请尝试从System.Web.Configuration liabrary执行此操作。我可以提供代码片段?此代码依赖于ASP.NET应用程序中的代码。OpenWebConfiguration()
不接受Web.config的绝对路径,它使用虚拟路径,并尝试根据当前站点解析它。我的脚本独立运行。
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.
}
}
}