您的位置:首页 > 其它

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

2007-12-28 15:54 357 查看
加密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 授予帐户对计算机级别的 "MyKeys" RSA 密钥容器的访问权限,运行:

[b]aspnet_regiis -pa "ConnectionStringsKey" "NT AUTHORITY\NETWORK SERVICE"

[/b]

4 是用指定的密钥加密指定目录下的web.config文件的指定的配置节

aspnet_regiis -pef "connectionStrings" "d:\testproj\websitetest" -prov "ConnectionStringsKeyProvider"

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

5、如果访问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"

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

5、找出当前运行ASPNET的角色

打开记事本,然后将下面的代码复制到一个新文件中。

<%@ Page Language="C#" %>

<%

Response.Write(System.Security.Principal.WindowsIdentity.GetCurrent().Name);

%>

保存 my.aspx 到你的web目录 ,运行一下窗体显示 “ NT AUTHORITY\NETWORK SERVICE ”。成功!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: