順着這個線索,Microsoft在Asp.net2.0種已經提供了在配置文件中加密一些敏感信息的方法,包括數據庫連接字符串,我們使用這個新的方法可以很簡單,很容易加密配置文件的節點,從而使你的應用程序更安全。
在Asp.Net 2.0中介紹保護配置模型中,你可以使用兩種保護配置信息的Providers,他們是:
RSAProtectedConfigurationProvider :
這是默認的Provider,可以使用這個RAS密匙和加密法則來加密和解密數據
DataRrotectionConfigurationProvider :
這個Provider 使用Windows提供的數據保護應用程序接口(DPAPI)來加密解密數據
讓我們探究一下,如何在Asp.Net 2.0中用以上兩種Providers對Web.Config中數據庫連接字符串加密和解密
Programmatic Encrption /Decrption
下面是在沒有經過加密的Web.Config的數據庫連接字符串
<appSettings/>
<connectionStrings>
<add name="AspNet_BBsConnectionString" connectionString="Data Source=.;Initial Catalog=AspNet_BBs;User ID=sa"
providerName="System.Data.SqlClient" />
</connectionStrings>
在以上的Web.Config中,可以觀察一下Web.Config中的<connectionStrings>節點,包括了數據庫的用戶名
密碼
下面我們用第一種方法來對數據庫連接字符串加密,
首先新建一個WebApplication,並且添加一個單獨的類,在這個單獨的類中,我們主要用到三個命名空間,第一個是System.configuration,在這個類中包含了處理Client 和Asp.Net Application的配置信息。第二個是System.Web.Configuration.WebConfigurationManager類,它包含了對Asp.net Web pplications的配置文件進行編輯。
然後添加一個對Web.Config加密的靜態方法EncrptConnectionString,
/// 加密類 Create : 興百放 Time : 2007-6-7-16-29
/// </summary>
public static void EncrptConnction()
{
//打開此WebApplication的Web.Config
Configuration config = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
//得到Web.Config的connectionStrings
ConfigurationSection section = config.GetSection("connectionStrings");
if(!section.SectionInformation.IsProtected)
{
section.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");
config.Save();
}
}
/// 解密類 Create : 興百放 Time : 2007-6-7-16-29
/// </summary>
public static void DecrptConnction()
{
Configuration config = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
ConfigurationSection section = config.GetSection("connectionStrings");
if (section.SectionInformation.IsProtected)
{
section.SectionInformation.UnprotectSection(); //更新Web.config
config.Save();
}
}
最後新建一個Web頁,在這個頁上拖兩個Button,一個命名爲Web.Config Encrpt.它執行的是EncrptConnectionString;另一個爲Web.Config Decrpt ,執行的是DecrptConnection最後
執行,可以看看效果了
上面就是經過加密之後的Web.Config中的數據庫連接字符串
但是,我們不能對Web.Config中的所有的節點用以上的方法加密,有些節點,我們需要一些額外的步驟對他們加密
<processModel>
<appSettings/>
<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>fgIDeCHVR8nxK3rppwqc8pJPh3E3anEoqCSAdDFfe2Oysq28i++11Y3fIx7PH/CsoetCRw2Tx9qCw2ATFZ6GVPwQ+/938A8g8DsfaPG2S9s89RL0Yz4szQpILvhkTZlyuB8C7kH/9TjQhWb3Q5/XSrkqVe3ggvKBIf+QsqfCKbw=</CipherValue>
</CipherData>
</EncryptedKey>
</KeyInfo>
<CipherData>
<CipherValue>lGZRXAwKFXGO36CyqqfKG6PXUsegnkm9ZmKMBoFN3wxVDPC9WkVr9yEySo2hScbrMzcWgJiy9dx7mjUDEa2QevpRe6nI3Gx5QKRLy0rTboBI2ID49XrWYMsrBLOVrqs8bMJNSFHe5aKmQJCdAGqrDeB9PKf6Syuupc2gv89m9Vq5hXy2k7Lw20vTghdK/YJyoJcwt/dPUaShwhoYGrI5P0cGqtI/c15jo6tiwt5M+ZvKtogcAfy15SfMvrERojWhUQeo98rl19XmxQwrNxn7Cw==</CipherValue>
</CipherData>
</EncryptedData>
</connectionStrings>
<runtime>
<mscorlib>
<startup>
<system.runtime.remoting>
<configProtectedData>
<satelliteassemblies>
<cryptographySettings>
<cryptoNameMapping>
<cryptoClasses>
爲了加密這些configruation section ,你必須加密這些節點的值,並且在註冊表中保存
Encryption/Decryption using aspnet_regiis.exe command line tool
在這裏我們介紹另一種對Web.Config加密的方法,就是使用Ms提供的命令行工具(aspnet_regiis.exe)進行操作,你可以在<WINDOWSDIR>/Microsoft.Net/Framework/你的版本,下找到aspnet_regiis。
我們使用一下的命令對Web.Config進行加密
aspnet_regiis.exe -pe "connectionStrings" -app "/你的站點名稱" “prov "DataProtectionConfigurationProvider"
如果想要解密的話,使用一下的命令
aspnet_regiis.exe -pd "connectionStrings" -app "/你的站點的名稱"
對於第二種方法我沒有進行測試,如果有什麼不對的地方,還請諒解!
雖然,Asp.Net對所有以.config後綴名的Http請求拒絕,但是如果一些惡意的攻擊者,獲得了對服務器文
件系統操作權限,那麼我們保存在Web.Config中的一些敏感信息將會泄露。幸運的是,Asp.Net 2.0提供了對
配置文件加密的加密術來避免這種問題,你可以從Programmatically 和 aspnet_regiis.exe工具任選一個來對Web.Config和Machine.Config進行加密和解密
以上是我對Encrypting Connection Strings in web.config file的翻譯,這是我第一次翻譯文章,如果有什麼
出路的話,還請諒解,謝謝!