对web.config文件中的机密数据进行加密

加密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"

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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值