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 服务器上删除该文件。
@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