您的位置:首页 > 理论基础 > 计算机网络

加密和解密配置节(asp2.0以后)-http://www.cnitblog.com/lby91772/archive/2008/03/04/40436.html

2008-10-26 11:06 246 查看
asp.net2.0中新增了对web.config中的部分数据进行加密的功能,可以使用RSAProtectedConfigurationProvider和DPAPIProtectedConfigurationProvider来加密,本文说明使用RSAProtectedConfigurationProvidert和计算机级别的密钥容器进行加密的步骤。

1.首先确定要进行加密的web.config中的配置节是否可以加密

2.创建RSA密钥容器

3.在web.config中标识要使用的密钥容器

4.对web.config进行加密

5.授予对 RSA 密钥容器的访问权限

Step 1:首先确定要进行加密的web.config中的配置节是否可以加密

ASP.NET 2.0支持对Web.config的部分配置节进行加密,以下配置节中的数据是不能进行加密的:

<processModel>

<runtime>

<mscorlib>

<startup>

<system.runtime.remoting>

<configProtectedData>

<satelliteassemblies>

<cryptographySettings>

<cryptoNameMapping>

<cryptoClasses>

Step2:创建 RSA 密钥容器

若要创建 RSA 密钥容器,请使用 ASP.NET IIS 注册工具 (Aspnet_regiis.exe) 及 –pc 开关。必须为密钥容器指定一个名称,该名称标识应用程序的 Web.config 文件的 configProtectedData 节中指定的 RsaProtectedConfigurationProvider 所使用的密钥容器。为确保可以导出新创建的 RSA 密钥容器,必须包括 -exp 选项。

例如,下面的命令创建一个名为 ABeenKeys 的 RSA 密钥容器,该容器是可导出的计算机级密钥容器。

aspnet_regiis -pc "ABeenKeys"–exp

Step 3: Modify web.config to identify the key container

编辑Web.config文件以标识要使用的密钥容器

在web.config中加以<configProtectedData>来配置密钥容器, 使用名为 ABeenKeys 的计算机级 RSA 密钥容器的在<configuration>中加入xmlns属性<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">使用名为 ABeenKeys 的计算机级 RSA 密钥容器的 saProtectedConfigurationProvider。

<configProtectedData > <providers> <add name="ABeenProvider" type="System.Configuration.RsaProtectedConfigurationProvider, System.Configuration, Version=2.0.0.0,Culture=neutral, processorArchitecture=MSIL" keyContainerName="ABeenKeys"/> </providers> </configProtectedData>

Step 4: Encrypt the <connectionStrings> section of your web.config file

加密你的web.config文件中的配置节

> aspnet_regiis -pe "connectionStrings" -app "/connectionTest" 

Step 5:授予对 RSA 密钥容器的访问权限

可以通过以下代码确定应该给哪个用户权限

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

默认情况下,RSA 密钥容器受到所在服务器上的 NTFS 访问控制列表 (ACL) 的严密保护。这样能够限制可以访问加密密钥的人员,从而增强加密信息的安全性。必须首先向 ASP.NET 应用程序的进程标识授予对该 RSA 密钥容器的读取访问权限,然后 ASP.NET 才能使用 RSA 密钥容器。可以使用 Aspnet_regiis.exe 工具及 -pa 开关,向 ASP.NET 应用程序的标识授予读取 RSA 密钥容器的权限。例如,下面的命令向 Windows Server 2003 NETWORK SERVICE 帐户授予对名为 ABeenKeys 的计算机级 RSA 密钥容器的读取访问权限:

aspnet_regiis -pa "ABeenKeys" "NT AUTHORITY\NETWORK SERVICE"

注意:

如果 RSA 密钥容器是用户级容器,必须以其 Windows 配置文件存储了密钥的用户的身份登录,并且必须包括 -pku 选项以授予对该用户级 RSA 密钥容器的访问权限。

若要使用计算机配置中指定的默认 RsaProtectedConfigurationProvider,必须首先向应用程序的 Windows 标识授予对名为 NetFrameworkConfigurationKey 的计算机密钥容器的访问权限,该计算机密钥容器是为该默认提供程序指定的密钥容器。例如,下面的命令向 NETWORK SERVICE 帐户授予对默认 RsaProtectedConfigurationProvider 所使用的 RSA 密钥容器的访问权限。

aspnet_regiis -pa "NetFrameworkConfigurationKey" "NT AUTHORITY\NETWORK SERVICE"

NetFrameworkConfigurationKey RSA 密钥容器是 Aspnet_regiis.exe 工具所发出的命令的默认密钥容器。因此上述命令也可以按以下方式发出:

aspnet_regiis -pa "NT AUTHORITY\NETWORK SERVICE"
Encrypt.bat

@echo off

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis -pc "ABeenKeys" -exp

PAUSE

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis -pe "connectionStrings" -app "/connectionTest"

PAUSE

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis -pa "NetFrameworkConfigurationKey" "WJDZ-68F7317D1C\ASPNET"

PAUSE

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis -pa "ABeenKeys" "WJDZ-68F7317D1C\ASPNET"

PAUSE

Decrypt.bat

@echo off

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis -pd "connectionStrings" -app "/connectionTest"

PAUSE

Web.config

<connectionStrings>

<add name="myconnection" connectionString="server=192.168.0.28;uid=sa;pwd=;database=testdb" />

</connectionStrings>

<!--<add name="myconnection" connectionString="server=192.168.0.28;uid=sa;pwd=;database=testdb"/>-->

<configProtectedData >

<providers>

<add name="ABeenProvider"

type="System.Configuration.RsaProtectedConfigurationProvider, System.Configuration, Version=2.0.0.0,Culture=neutral, processorArchitecture=MSIL"

keyContainerName="ABeenKeys"/>

</providers>

</configProtectedData>

其它補充:

用aspnet_regiis -pe来加密.config文件中的节,拿到另一台机器上部署时却发现在读取加密节的时候会发生异常,查了下MSDN发现:如果您打算在多台服务器(如网络场)上使用同一个加密配置文件,则只有使用 RsaProtectedConfigurationProvider 才能导出用于对数据进行加密的加密密钥,并在另一台服务器上导入它们。

总结了一下命令:

aspnet_regiis -pc "MyKeys" -exp (创建容器)

aspnet_regiis -pa "MyKeys" "NT AUTHORITY\NETWORK SERVICE" (授权访问)

aspnet_regiis -pe "customSettings" -app "/Site" -prov "MyProvider" (加密)

aspnet_regiis -pd "customSettings" -app "/Site" (解密)

aspnet_regiis -px "MyKeys" c:\keys.xml -pri (导出密钥)

aspnet_regiis -pz "MyKeys" (删除密钥)

aspnet_regiis -pi "MyKeys" c:\keys.xml (导入密钥)

开发机器上执行:

aspnet_regiis -pc "MyKeys" -exp (创建容器)

aspnet_regiis -pa "MyKeys" "NT AUTHORITY\NETWORK SERVICE" (授权访问)

aspnet_regiis -px "MyKeys" c:\keys.xml -pri (导出密钥)

部署机器上执行:

aspnet_regiis -pi "MyKeys" c:\keys.xml (导入密钥)

aspnet_regiis -pa "MyKeys" "NT AUTHORITY\NETWORK SERVICE" (授权访问)

然后要重启IIS

最有还有一个问题很郁闷,在IIS6.0的机器上没有问题,但是到了IIS5.1上的机器怎么试都不行,是5.1不支持?

MSDN上另外提示要注意的安全问题:为了确保没有人可以对已经用 RSA 密钥容器加密的 Web.config 文件进行解密,将 RSA 密钥容器导出到 .xml 文件后,请将 .xml 文件复制到 Web 服务器的外部位置,然后从 Web 服务器上删除该文件。

////clear temp files

@echo off

color 0a

echo 开始执行清理……

reg query "HKCU\software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v

Cache>%temp%\cleantmp.txt

reg query "HKCU\software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v

Cookies>>%temp%\cleantmp.txt

reg query "HKCU\software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v

History>>%temp%\cleantmp.txt

reg query "HKCU\software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v

NetHood>>%temp%\cleantmp.txt

reg query "HKCU\software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v

Recent>>%temp%\cleantmp.txt

for /f "tokens=3*" %%a in (%temp%\cleantmp.txt) do (

for /d %%i in ("%%a %%b\*.*") do rd /s /q "%%i"

del /a /f /s /q "%%a %%b\*.*"

)

del /a /f /s /q "%userprofile%\Locals~1\Tempor~1\*.*"

del /a /f /s /q "%userprofile%\Locals~1\Temp\*.*"

del /a /f /s /q "%userprofile%\recent\*.*"

del /a /f /s /q "%Temp%\*.*"

del /a /f /s /q "%Tmp%\*.*"

del /a /f /s /q "%HomePath%\..\IconCache.db"

del /a /f /s /q "%systemdrive%\*._mp"

del /a /f /s /q "%systemdrive%\*.log"

del /a /f /s /q "%systemdrive%\*.dmp"

del /a /f /s /q "%systemdrive%\*.gid"

del /a /f /s /q "%systemdrive%\*.old"

del /a /f /s /q "%systemdrive%\*.tmp"

del /a /f /s /q "%systemdrive%\recycled\*.*"

del /a /f /s /q "%SystemRoot%\*.bak"

del /a /f /s /q "%SystemRoot%\*.query"

rd /s /q "%SystemRoot%\Downloaded Program Files"

rd /s /q "%SystemRoot%\Offline Web Pages"

rd /s /q "%systemroot%\Connection Wizard"

rd /s /q "%SystemRoot%\SoftwareDistribution\Download"

rd /s /q "%SystemRoot%\Assembly"

rd /s /q "%SystemRoot%\Help"

rd /s /q "%SystemRoot%\system32\ReinstallBackups"

del /a /s /q "%SystemRoot%\inf\*.pnf"

del /a /f /s /q "%SystemRoot%\inf\InfCache.1"

dir %SystemRoot%\inf\*.* /ad/b >%SystemRoot%\vTmp.txt

for /f %%a in (%SystemRoot%\vTmp.txt) do rd /s /q "%SystemRoot%\inf\%%a"

del /a /f /s /q "%SystemRoot%\Driver Cache\*.pnf"

del /a /f /s /q "%SystemRoot%\Driver Cache\InfCache.1"

del /a /f /s /q "%SystemRoot%\system32\drivers\*.pnf"

del /a /f /s /q "%SystemRoot%\system32\drivers\InfCache.1"

rd /s /q "%SystemRoot%\temp" & md "%SystemRoot%\temp"

del /a /f /s /q "%SystemRoot%\Prefetch\*.*"

del /a /f /s /q "%SystemRoot%\minidump\*.*"

del /a /f /q "%SystemDrive%\*.chk"

dir %SystemDrive%\found.??? /ad/b >%SystemRoot%\vTmp.txt

for /f %%a in (%SystemRoot%\vTmp.txt) do rd /s /q "%SystemDrive%\%%a"

dir %SystemRoot%\$*$ /ad/b >%SystemRoot%\vTmp.txt

for /f %%a in (%SystemRoot%\vTmp.txt) do rd /s /q "%SystemRoot%\%%a"

rd /s /q "%ProgramFiles%\InstallShield Installation Information"

Ren "%ProgramFiles%\Common~1\Real\Update_OB\realsched.exe" realsched.ex_

Del "%ProgramFiles%\Common~1\Real\Update_OB\realsched.exe"

Reg Delete "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run" /v TkBellExe /f

rd /s /q "%ProgramFiles%\Tencent\QQGame\Download"

taskkill /f /im "TIMPlatform.exe" /t

del /a /f /s /q "%ProgramFiles%\Kaspersky Lab\*.tmp"

del %SystemRoot%\vTmp.txt

exit
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐