加密网站配置文件中的信息
加密网站中的配置信息,我们不需要写任何代码,也不需要修改任何代码,只需要使用 aspnet_regiis 工具修改配置文件即可.
比如我们有下面一个配置文件需要加密:
<configuration>
<connectionstrings>
<add name=”sqlservices” connectionstring=”data source=localhost;
integrated security=sspi;initial catalog=northwind;” />
</connectionstrings>
</configuration>
假设这个配置文件在 myapplication 目录下。
加密命令
aspnet_regiis -pe “connectionstrings” -app “/myapplication”
aspnet_regiis 命令在你安装的 .net framework 目录下, 默认在:
c:\windows\microsoft.net\framework\v2.0.*
加密后的效果:
<configuration>
<connectionstrings configprotectionprovider=”rsaprotectedconfigurationprovider”>
<encrypteddata type=”http://www.w3.org/2001/04/xmlenc#element”
xmlns=”http://www.w3.org/2001/04/xmlenc#”>
<encryptionmethod algorithm=”http://www.w3.org/2001/04/xmlenc#tripledes-cbc” />
<keyinfo xmlns=”http://www.w3.org/2000/09/xmldsig#”>
<encryptedkey xmlns=”http://www.w3.org/2001/04/xmlenc#”>
<encryptionmethod algorithm=”http://www.w3.org/2001/04/xmlenc#rsa-1_5″ />
<keyinfo xmlns=”http://www.w3.org/2000/09/xmldsig#”>
<keyname>rsa key</keyname>
</keyinfo>
<cipherdata> <ciphervalue>0ru0xfrexc6alfyzm+f+iwzvinqtzaaunysovpv0dlipm72d34mj/gx7pzvhsjnqclixeyjsayse
12oauf4rlieraa/rhiqdkjqyjtrrrciqnwqt5pet5lm9q0ait20kpb2g2hn/0qb7vkcwydbotdbwmua7fxaqjhmckavi0mc=</ciphervalue>
</cipherdata>
</encryptedkey>
</keyinfo>
<cipherdata> <ciphervalue>bpws3liouxhd0qdlfrmwdy9xwn1jphnmoskuvn3jvpwkmd2h7hjo2betijyioaq/2j1saldjm
jfgg85bekfvuunbmrg6czcgxhyokeahzghzdw+d
za8qef/t7witzuiqeslgk2wluxndfg4zfsydivmxy6xqh3fvw4jochzlxg/zjrjychik3i27oh/xuxtsq0vnol
gfssm/mtgwb4tloelcrj6jm5u0dja2fvmjpdc=
</ciphervalue>
</cipherdata>
</encrypteddata>
</connectionstrings>
</configuration>
注意:为了避免一行太长,我这里把加密后信息加了几个回车符。
asp.net 在处理 web.config 文件时会自动对该文件的内容进行解密。因此,
不需要任何附加步骤即可对已加密的配置设置进行解密,供其他 asp.net 功能使用或用于访问代码中的值。
如果你想修改这些配置信息,就需要解密这个文件,然后再加密。解密用 aspnet_regiis.exe 命令的 -pd 选项。
参考命令如下:
aspnet_regiis -pd “connectionstrings” -app “/myapplication”
上面给的范例是 针对 iis 的站点,如果你的站点是使用vs2005 的 asp.net development server
则需要用 -pef 参数,当然 iis 站点也可以这么用
aspnet_regiis.exe -pef “connectionstrings” “d:\my2005codes\webtestcode\testwebsite”
说明:
-pef 对指定物理(非虚拟)目录中的 web.config 文件的指定配置节进行加密。
对应的这个解密则是
-pdf 参数 对指定物理(非虚拟)目录中的 web.config 文件的指定配置节进行解密。
