1. 如何为配置文件加密
在web.config或app.config文件里我们经常会存储一些敏感信息,比如connectionStrings或者appSettings,比如像下面的文件。
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<connectionStrings>
<add name="MyNwConnectionString" connectionString="Server=myServerAddress;Database=myDataBase;User Id=myUsername; Password=myPassword;"/>
</connectionStrings>
<appSettings>
<add key="User" value="myUsername"/>
<add key="Password" value="myPassword"/>
</appSettings>
</configuration>
using System;
using System.Configuration;
namespace WebConfigEncryptTest
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string user = ConfigurationManager.AppSettings.Get("User");
string password = ConfigurationManager.AppSettings.Get("Password");
string connectionString = ConfigurationManager.ConnectionStrings["MyNwConnectionString"].ConnectionString;
}
}
}
(一)加密文件可以使用的Provider
.NET为我们提供了一个工具aspnet_regiis.exe来对web.config文件中的敏感信息进行加密(app.config文件可以先改名为web.config,加密后再改回app.config)。你可以使用两个provider中的一个来进行加密:
System.Configuration.:在System.Configuration.dll中,使用Windows DPAPI(Data Protection API)来进行加密,密钥存在Windows Local Security Authority(LSA)中。注意:当使用时,加密文件所使用的帐号需要与运行web application的帐号相同,否则web application无法解密加密的内容。
System.Configuration.:在System.Configuration.dll中,使用RSA算法来进行加密(RSA算法是非对称加密,参见《对称加密与非对称加密 》),公钥存放在config文件当中,只有加密的计算机有密钥。通常是默认的缺省provider。
(二)加密文件的命令
加密web.config文件可以使用:
aspnet_regiis -pef section web-app-physical-dir
Encrypt the configuration section. Optional arguments:
[-prov provider] Use this provider to encrypt.
比如运行下面的命令就会分别对connectionStrings和appSettings中的信息进行加密:
aspnet_regiis.exe -pef "connectionStrings" "C:\myweb\HelloService"
aspnet_regiis.exe -pef "appSettings" "C:\myweb\HelloService"
加密后的web.config文件变成:
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation targetFramework="4.0" />
</system.web>
<connectionStrings configProtectionProvider="">
<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>E2fO9C0TJVxImLYQZza+fCQdDbTpNh/kOKLRsK6zcFjkgtUCl6SnMViuu/2G1NVTxqXyEWYwyK6AiCZA+feeG/+f2EIimP7LJI+JRZVerI4MU6Ke3wxm2S/ATc73/W6eg9808f4//JB0kso0kGJ9i+==</CipherValue>
</CipherData>
</EncryptedKey>
</KeyInfo>
<CipherData>
<CipherValue>I1DWG11Iz/rq+NC9C/21B3Q22J9+//JW1oCvAGs5tHrZU5+vgvm0yCmSuCWZbXva+iv9J35EQqs58pq+hwVo1hg1dffpGCBykaXGl5VX3TIGc=</CipherValue>
</CipherData>
</EncryptedData>
</connectionStrings>
<appSettings configProtectionProvider="">
<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>//SuBvV3D2kxhHaYGFaPuvYgsyOLf3+aYR3O/uh/+/iSANzAWoC+==</CipherValue>
</CipherData>
</EncryptedKey>
</KeyInfo>
<CipherData>
<CipherValue>5W2KhG/oETLUDptobcOM52x1qD/g9A0By/wcGXI+///w=</CipherValue>
</CipherData>
</EncryptedData>
</appSettings>
</configuration>
是默认的缺省provider,如果想使用,可以用-prov参数指明:
aspnet_regiis.exe -pef "connectionStrings" "C:\myweb\HelloService" -prov ""
aspnet_regiis.exe -pef "appSettings" "C:\myweb\HelloService" -prov ""
加密配置文件后,源程序不需要做任何改动。如果要修改或添加新的配置信息,需要先解密配置文件。不论使用哪种Provider,都只能在进行加密的计算机上对配置文件进行解密。