注意:是对虚拟目录下还是物理目录下的文件加密
加密网站中的配置信息,我们不需要写任何代码,也不需要修改任何代码,只需要使用 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="RsaProtectedConfiguratio nProvider"> <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+IWZVINqTZAAunysoVPv0dliP M72D 34MJ/gX7pzvhSJNqCLiXeyjsayse 12oAuF4rlIEraa/RHiqDKjqyJtRrRCiqnwqt5PE T5LM9Q0aiT20Kpb2G2hn/0QB 7vKcWydboTdbwmUa7fXaQJhM cKaVI0mc=</CipherValue> </CipherData> </EncryptedKey> </KeyInfo> <CipherData> <CipherValue>BPws3LIOuXhD0qDlfRMWDy9X wn1jPHnMosKuVn3JVPW KmD2h7hJo2BeTIjyIOAq/2J1saLDJm JfgG85BEKfVUuNbMRg6czcgX HyOKeAHZgHzdw+d zA8qEF/t7wITzuIQEslGK2WlUXNDFg4 ZfsYDivmxy6xQh3Fvw4JOCHz LXg/ ZJrjYcHIk3I27oh/XuxtSQ0VNOl gfSsM/MTGwB4tloELcRJ6Jm5u0dJA2 fvmjpdc= </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 文件的指定配置节进行解密。
转自:http://zhidao.baidu.com/link?url=hcl34Of5n_OeoZSjRRTfvGdjiO7yEaGgRS