天天看点

解决打不开 RSA 密钥容器 即:加密web.config中的内容

简单的解决方法:

webconfig 加解密,未能使用提供程序“rsaprotectedconfigurationprovider”进行解密。提供程序返回错误消息为: 打不开 rsa 密钥容器。

问题:未添加用于访问 rsa 密钥容器

命令:aspnet_regiis -pa "netframeworkconfigurationkey" "nt authority/network service"

注意事项:xp下:aspnet_regiis -pa "netframeworkconfigurationkey" "aspnet"

加密:aspnet_regiis -pe "appsettings" -app "/应用程序名"

解密:aspnet_regiis -pd "appsettings" -app "/应用程序名"  如(/petshop/web)

更灵活的解决方法:

1、创建一个密钥容器 

   aspnet_regiis -pc "connectionstringskey" -exp

   connectionstringskey为密钥容器的名称 

   可以使用aspnet_regiis /?查看该命令的用法

2、在web.config中加入如下内容<configprotecteddata>

<providers>

<clear />

<add name="connectionstringskeyprovider"

      type="system.configuration.rsaprotectedconfigurationprovider, system.configuration, version=2.0.0.0,culture=neutral, publickeytoken=b03f5f7f11d50a3a, processorarchitecture=msil"

      keycontainername="connectionstringskey"

      usemachinecontainer="true"/>

</providers>

</configprotecteddata> 

3、通过命令行:用指定的密钥加密指定目录下的web.config文件的指定的配置节

     aspnet_regiis -pef "connectionstrings" "d:/testproj/websitetest" -prov "connectionstringskeyprovider"

    对于子配置节用/分隔表示, 如identity配置节 需要写成 "system.web/identity"

4、如果访问web程序,页面提示 error message from the provider: the rsa key container could not be opened.

     是由于network service帐户无法访问密钥文件造成的。 找到密钥文件, 赋予network service读权限。该密钥文件位于(可按时间排序,找到自己产生的那个密钥文件)

vista: c:/programdata/microsoft/crypto/rsa/machinekeys/

xp或其他:c:/documents and settings/all users/application data/microsoft/crypto/rsa/machinekeys

至此:查看被加密的标记, 内容就已经是被加密过的了。

5.通过.aspx页面:加密连接字符串:界面如图:

解决打不开 RSA 密钥容器 即:加密web.config中的内容

后台代码: //加密按钮

protected void button1_click(object sender, eventargs e)

{

//①需要加密的节点:

string name = @"connectionstrings";

//②当前路径;

string apppath = "/logincontral";

configuration config = webconfigurationmanager.openwebconfiguration(apppath);

//③提供加密的方式:(这里使用自定义的加密方式)

// string provider = "rsaprotectconfigurationprovider";

string provider = "connectionstringskeyprovider";

config.getsection(name).sectioninformation.protectsection(provider);

//⑤保存web.config文件

try

config.save();

}

catch (exception ex)

response.write(ex.message);

if (config.getsection(name).sectioninformation.isprotected)

button1.enabled = false;

response.write("加密成功!");

else

response.write("加密失败!");

//解密按钮:

protected void button2_click(object sender, eventargs e)

//①需要节密的节点:

//③使用unprotectsection方法进行解密;

config.getsection(name).sectioninformation.unprotectsection();

//④保存web.config文件

if (config.getsection(name).sectioninformation.isprotected==false)

button2.enabled = false;

response.write("解密成功!");

response.write("解密失败!");

注意:string apppath = "/logincontral" 为当前项目路径;

解决打不开 RSA 密钥容器 即:加密web.config中的内容

加密前的连接字符串:

<connectionstrings>

<add name="connection" connectionstring="data source=.;database=aspnetdb;user id=sa;pwd=123;" />

</connectionstrings>

加密后的连接字符串:

<connectionstrings configprotectionprovider="connectionstringskeyprovider">

<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" />

<keyname>rsa key</keyname>

</keyinfo>

<cipherdata>

<ciphervalue>aepogg4vvhd8k6nvhvmdo8fagfmopoddvnbn5vpv0mxp8ngrimnzfvflrhhvooiu56mcmmr6n5cunixzimgb/ztgcnmsiku8sr6ytx8iuh64u9ivujwaoabtzp4ahlhmih6ywkhxjmqrjyys2ecsocquzq0ndkkc3omg/ucoik0=</ciphervalue>

</cipherdata>

</encryptedkey>

<ciphervalue>bimah/6vwvi0fkvqijpszzkhk+a6qni0aa794yxi1x+sffkdtsur15hvcbyolborckprhx94mpom2ekobqyvycf24pdyakiffazo1sluzmutcxfvu/ltbqn83bnjdgbgo6evtdg4m7dsavr6qwyep8wysqwwubkwslzsmynqpoyghvb9btvjbscwiuz4ynfhvutzigisjqa=</ciphervalue>

</encrypteddata>

</connectionstrings> 

其他备用操作:

1、解密web.config 

    aspnet_regiis -pdf "connectionstrings" "d:/testproj/websitetest"

2、把密钥容器导出为xml文件 

    aspnet_regiis -px "connectionstringskey" "c:/key.xml" 。这个命令只导出公钥,因此以后只能用于加密,而无法解密。

    aspnet_regiis -px "connectionstringskey" "c:/keys.xml" -pri  这个则连私钥一起导出了,所以我们要用这个。

3、把密钥容器删除  

   aspnet_regiis -pz "lixinkey"   删除后再运行程序,会提示出错: 

    分析器错误信息: 未能使用提供程序“lixinkeyprovider”进行解密。提供程序返回错误信息为: 打不开 rsa 密钥容器。 

    同理可以证明,在任何一台未安装正确的密钥容器lixinkey的机器上,程序都无法对connectionstrings节进行解密,因此也就无 法正常运行。

4、导入key.xml文件 

     aspnet_regiis -pi "lixinkey" "c:/keys.xml"

     此时,再运行程序会发现又可以解密了。证明加密与解密机制运行正常。

最后说一下这个机制所提供的安全性保障可以运用在什么方面:

1. 对winform程序的app.config进行加密实际意义并不大,因为无论如何,客户机都可以通过运行aspnet_regiis -pdf 来对配置文件进行解密,从而暴露敏感信息。

2. 对于web.config进行加密的意义也仅限于,当web.config文件不小心泄露时,不会同时泄露敏感信息,如果恶意攻击者已经取得了在服务器上运行程序的权限,那么同app.config一样,可以很容易通过通过运行aspnet_regiis -pdf 获取明文了。

3. 还有,通过aspnet_regiis -pa "key" "nt authority/network service"控制对不同用户对密钥容器的访问权限,应该还可以进一步获取一些安全性,比如可以控制某些用户即使登录到服务器上,也无法用aspnet_regiis -pdf对配置文件进行解密。