加密web.config中的内容

来源:互联网 发布:学吹笛子软件 编辑:程序博客网 时间:2024/05/22 03:10

加密web.config中的内容


1、Create an RSA keypair in ContainerName, -exp means the key is exportable(创建一个密钥容器)

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

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


其他备用操作


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"

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