对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的角色
打开记事本,然后将下面的代码复制到一个新文件中。
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 ”。成功!
相关文章推荐
- 对web.config文件中的机密数据进行加密
- 对web.config文件中的机密数据进行加密
- 在spring-config文件中对jdbc数据配置进行加密处理
- 支持加密 Web.config 文件的节 以及如何部署
- 对WinForm的App.config文件进行加密
- 微软提供的web.config文件的加密方式,掩耳盗铃!
- 对Webconfig中的连接字符串进行加密
- web.config文件加密-ASP.NET
- ASP.NET 2.0加密解密Web.config配置文件
- 如何对web.config进行加密和解密
- 对WinForm的App.config文件进行加密
- 我们如果想对web.config的数据库连接字符串进行加密的话,那么这里提供了两个方法。
- ASP.NET 2.0加密Web.config 配置文件
- Asp.Net 2.0加密Web.config配置文件
- 对web.config的数据库连接字符串进行加密
- ASP.NET读取设置Web.config文件--数据库
- 用C语言对数据或文件内容进行加密
- 对WinForm的App.config文件进行加密
- 对WinForm的App.config文件进行加密
- ASP.NET 2.0加密Web.config 配置文件