ASP.NET 下的Web.config 提供了很方便的系統參數設定方式,但是太方便的東西有時後卻得為它另外做一些額外的工作來處理,比如說你可能會在Web.config裡儲存資料庫的連線字串同時又把帳號密碼寫了進去,雖然一般人並不能直接在瀏覽器上輸入網址來偷取Web.confg檔但是心裡總是毛毛的,而且在大部份機構的IT部門都會有一條這樣的規定。

「所有帳號密碼及老闆的情婦身份資料都不能以明碼方式儲存」

所以你想要讓他們掏錢買你做的系統就得要有辦法加密Web.config裡的資料,否則要是大老闆被捉姦在床絕對會認為是你出賣他。

ASP.NET 2.0中有兩個Provider可以幫助你完成保護資料的需求。
他們分別是:
  • RsaProtectedConfigurationProvider
  • DataProtectionConfigurationProvider
現在就讓我們來看看到底要用什麼方法來加解密資料。




使用aspnet_regiis.exe加解密

aspnet_regiis.exe是一個強大的IIS管理工具,如果你已經安裝好.Net framework 2.0的話,他就在 %windir% \Microsoft.Net\Framework\v2.0.50727目錄下,直接在命令提示字元視窗輸入下面指令。
1 aspnet_regiis -pe "connectionStrings" -app "/applicationName" 
看不清楚 | 列印 | 複製

connectionStrings代表你要加密的區段,本文以connectionStrings為範例你也可以視情況加密appSettings等其它區段資料。
applicationName代表你的Web應用程式的虛擬目錄名稱。

下完指令回去看看Web.config是否有些變化,順利的話connectionStrings區段應該會看起來像是這個樣子。
1 <connectionStrings configProtectionProvider="RsaProtectedConfigurationProvider">  
2   <EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element" 
3    xmlns="http://www.w3.org/2001/04/xmlenc#">  
4    <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc" /> 
5    <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">  
6     <EncryptedKey xmlns="http://www.w3.org/2001/04/xmlenc#">  
7      <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5" /> 
8      <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">  
9       <KeyName>Rsa Key</KeyName> 
10      </KeyInfo> 
11      <CipherData> 
12       <CipherValue> 
13 d28OjqVJ/8SbhT7/kS52ShU05JWvq1LsTCfXDUYYqu2Q18iJv3Y/KX7  
14 CrZOvNR7t2aZlUddutBbpsxqO4JpPgE/pv3kplR2TI5xhL4NZk+Mc6E  
15 vZnKQ7UAXveLUCeWNNtImb7c9/30ZVJPwAsR0XirdJBVEzOfU8yQD  
16 j50Tvmfo=</CipherValue> 
17      </CipherData> 
18     </EncryptedKey> 
19    </KeyInfo> 
20    <CipherData> 
21     <CipherValue>GYFFLF5IX2KJZSSyszQoVlXGRm+GJ2x2fUV/wldfId8=</CipherValue> 
22    </CipherData> 
23   </EncryptedData> 
24  </connectionStrings> 
看不清楚 | 列印 | 複製

太好了,真是亂成一團!另外要解密還原原來的字串也非常簡單,只需要下面的指令。
1 aspnet_regiis -pd "connectionStrings" -app "/applicationName" 
看不清楚 | 列印 | 複製

如果你夠聰明的話應該會發現他跟加密時的指令只差在-pd的參數而已,如果想知道aspnet_regiis的詳細使用說明請輸入aspnet_regiis /? 或是到MSDN上去翻一翻吧。

「等等,我不能叫使用者去下什麼狗屁指令啊,他除了打電話來煩我之外什麼都不會做。」
嗯,雖然aspnet_regiis非常好用但是使用者就是不會用,所以我們還是得弄出更簡的方法讓使用者可以自己玩才行。




使用程式碼加解密

開始動手吧,先做一個操作用的網頁並且在上面放兩個按鈕,一個用來加密字串一個用來解密字串,然後在按鈕的Click事件中寫入下面的程式。

1 protected void Encryption(object sender, EventArgs e)  
2 {  
3  
4     Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);  
5     ConfigurationSection section = config.ConnectionStrings;  
6       
7     if (!section.SectionInformation.IsProtected)  
8     {  
9         section.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");  
10         section.SectionInformation.ForceSave = true;  
11         config.Save(ConfigurationSaveMode.Modified);  
12     }  
13 }  
14  
15 protected void Decrypting(object sender, EventArgs e)  
16 {  
17     Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);  
18     ConfigurationSection section = config.ConnectionStrings;  
19       
20     if (section.SectionInformation.IsProtected)  
21     {  
22         section.SectionInformation.UnprotectSection();  
23         section.SectionInformation.ForceSave = true;  
24         config.Save(ConfigurationSaveMode.Modified);  
25     }  
26 }  
看不清楚 | 列印 | 複製

現在使用者只要按個按鈕就能完成加解密的動作了,這下他可沒話說了吧。