邪恶八进制信息安全团队技术讨论组's Archiver

无双坏坏 2006-2-13 09:17

[转载]加密网站配置文件中的信息

文章作者:蝈蝈俊.net
信息来源:博客堂

加密网站中的配置信息,我们不需要写任何代码,也不需要修改任何代码,只需要使用 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="[url]http://www.w3.org/2001/04/xmlenc#Element[/url]"
    xmlns="[url]http://www.w3.org/2001/04/xmlenc#[/url]">
    <EncryptionMethod Algorithm="[url]http://www.w3.org/2001/04/xmlenc#tripledes-cbc[/url]" />
    <KeyInfo xmlns="[url]http://www.w3.org/2000/09/xmldsig#[/url]">
      <EncryptedKey xmlns="[url]http://www.w3.org/2001/04/xmlenc#[/url]">
       <EncryptionMethod Algorithm="[url]http://www.w3.org/2001/04/xmlenc#rsa-1_5[/url]" />
       <KeyInfo xmlns="[url]http://www.w3.org/2000/09/xmldsig#[/url]">
        <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 文件的指定配置节进行解密。

参考资料:
[url]http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbda/html/bdasamppet4.asp[/url]

页: [1]
© 1999-2008 EvilOctal Security Team