laudy的博客

人变聪明容易,但想装糊涂可真难
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

加密(Asp.Net配置文件的)配置节(转载)

Posted on 2007-03-28 18:54  laudy  阅读(579)  评论(0)    收藏  举报
怎样加密配置文件?

ASP.NET提供了两种方法来加密和解密配置文件

1、在命令提示行中,通过ASPNET_REGIIS.EXE工具
2、通过 configuration management 类

加密与解密通过两个受保护的提供者实现
RsaProtectedConfigurationProvider
DataProtectionConfigurationProvider

存储在可以在你的机器里的machine.config文件中找到这两个提供者。RsaProtectedConfigurationProvider是默认的提供者。下面的部分是machine.config中的相关标记

<configProtectedData
defaultProvider="RsaProtectedConfigurationProvider">
<providers>
<add name="RsaProtectedConfigurationProvider"
type="System.Configuration.RsaProtectedConfigurationProvider,
System.Configuration, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a"
description="Uses RsaCryptoServiceProvider to encrypt and decrypt"
keyContainerName="NetFrameworkConfigurationKey"
cspProviderName="" useMachineContainer="true" useOAEP="false" />
<add name="DataProtectionConfigurationProvider"
type="System.Configuration.DpapiProtectedConfigurationProvider,
System.Configuration, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a"
description="Uses CryptProtectData and CryptUnProtectData
Windows APIs to encrypt and decrypt" useMachineProtection="true"
keyEntropy="" />
</providers>
</configProtectedData>

使用ASPNET_REGIIS.EXE加密一个配置节

让我们来看看如何使用ASPNET_REGIIS.EXE工具来加密<connectionStrings>配置节。

用VS.Net建立一个网站叫做EncryptTest,加入一个web.config文件。然后加入如下的<connectionStrings>配置节:

<connectionStrings>
<add name="connstr" connectionString=
"data source=.\sqlexpress;initial catalog=
northwind;integrated security=true" />
</connectionStrings>

我们简单的加入了一个联接字符串,指向了Northwind数据库。然后打开default.aspx文件,拖拽一个GridView控件,并在Page_Load中输入如下代码:

protected void Page_Load(object sender, EventArgs e)
{
string strConn = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
SqlDataAdapter da = new SqlDataAdapter("select * from customers", strConn);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
} 

这段代码里,我们用ConfigurationManager类从<connectionStrings>配置节读取了联接字符串。这里用们有意的用了手动的方法代替了SqlDataSource空间,就是为了证明内置的类ConfigurationManager自动的机密了加密版本的联机字符串。

 现在打开Visual Studio 2005命令提示,执行下面的命令:
aspnet_regiis -pe "connectionStrings" -app "/encrypttest"

-pe开关用来指定web.config中需要加密的节(我们的例子里是connectionStrings)。
-app开关用来指定IIS里面的虚拟路径

执行以后,如果你再查看一下web.config文件,你会看到这样的情形:

<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>NW4gFUtlA3XkbKu42FQ3kYV8EKmwzy9r53vrI2rjV
ZFqjsr00/MwS6TWqjnsguN09kWvNIrbfWu5+Gi+DLFhYnGm2NcuaCy
Vic8f5e0Q8u3E7zk2MegZmiri5bSELE1fZneWz4oFb+MHqA94ZO3Be
XBlocou6ydtmJPXZCqLsDQ=</CipherValue>
</CipherData>
</EncryptedKey>
</KeyInfo>
<CipherData>
<CipherValue>T2fswq6Rt7g7VSNUnoe4kNFGSluWCBReQf3DRbXao/
sWaWs4mrJAI6Xy0zNDHY5pKXUUF9Kep2wG84rMVx0QtLIUjBaUKCnrm
Eb+53oYNPjN4Kf5zcPyWoaWwcus4LnJYNtg3oGJUvavZ0tjGXH9+5gB
w/xMrtfDcYAIom9l/IMcO92BKrvimjn/k4Mr8VXxGpvdMkAC3+e6dtW
JeUgQGpepO+RNpWymB5kWj38LjMQ=</CipherValue>
</CipherData>
</EncryptedData>
</connectionStrings>


这个工具已经用RsaProtectedConfigurationProvider(默认的提供者)加密了connectionStrings节点,
并且将加密过的标签返回给配置文件。你还可以指定具体的Protected Configuration Provider(加密保护配置提供者),
使用-prov开关来指定。很简单,不是么?
现在,浏览你的web页面。这个web页面将会在GridView标记里正确的显示Customers表的内容,这说明ConfigurationManager类
在读取连接字符串的时候自动解密了其中的信息。
使用ASPNET_REGIIS.EXE解密配置节
如果你需要在开发过程中对连接串进行些改动要怎么办?不用担心,ASPNET_REGIIS.EXE工具同样支持解密节点,仅仅需要执行
下面的命令就会恢复到你原来的未加密的版本。
aspnet_regiis -pd "connectionStrings" -app "/encrypttest"
唯一的区别就是,我们用-pd开关代替了-pa开关。

通过代码加密与解密 

加密:
protected void Button1_Click(object sender, EventArgs e)
{
Configuration config = WebConfigurationManager.
OpenWebConfiguration(Request.ApplicationPath);
ConfigurationSection section = config.ConnectionStrings;
if (!section.SectionInformation.IsProtected)
{
section.SectionInformation.ProtectSection
("RsaProtectedConfigurationProvider");
section.SectionInformation.ForceSave = true;
config.Save(ConfigurationSaveMode.Modified);
}
}
解密配置节的工作和加密类似:
protected void Button2_Click(object sender, EventArgs e)
{
Configuration config = WebConfigurationManager.
OpenWebConfiguration(Request.ApplicationPath);
ConfigurationSection section = config.ConnectionStrings;
if (section.SectionInformation.IsProtected)
{
section.SectionInformation.UnprotectSection();
section.SectionInformation.ForceSave = true;
config.Save(ConfigurationSaveMode.Modified);
}
}