[轉(zhuǎn)]對(duì)WinForm的App.config文件進(jìn)行加密最近在做一個(gè)WinForm的項(xiàng)目,由于采用的是在客戶端直接連接數(shù)據(jù)庫(kù)的方式,需要在客戶端部署App.config,由于使用了Enterprise Library,需要對(duì)App.config文件里的connectionStrings片斷進(jìn)行加密處理,搜索MSDN,發(fā)現(xiàn)已經(jīng)有了現(xiàn)成的工具ASP.NET IIS 注冊(cè)工具 (Aspnet_regiis.exe),可是它只能針對(duì)ASP.NET的Web.config文件,難道我們就沒有辦法了嗎?答案當(dāng)然是否定的。配置選項(xiàng)
-pdf 和-pef 參數(shù)是對(duì)指定的物理目錄里的Web.config文件進(jìn)行加密,我們可以先將App.config文件改名為Web.config,通過(guò)這兩個(gè)參數(shù)便可以“騙”過(guò)系統(tǒng),讓它將指定的配置節(jié)進(jìn)行加密,我們只需要將加密后的文件名改回App.config即可,我們來(lái)實(shí)驗(yàn)一下: 1<?xml version="1.0" encoding="utf-8"?>
2<configuration> 3 <configSections> 4 <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" /> 5 </configSections> 6 <dataConfiguration defaultDatabase="Connection String" /> 7 <connectionStrings> 8 <add name="Connection String" connectionString="Database=LocomotiveStat;Server=10.167.61.49;User ID=sa;Password=sa;" 9 providerName="System.Data.SqlClient" /> 10 </connectionStrings> 11</configuration> 輸入命令:aspnet_regiis -pef "connectionStrings" "E:\開發(fā)目錄",加密后的config文件內(nèi)容如下: 1<?xml version="1.0" encoding="utf-8"?>
2<configuration> 3 <configSections> 4 <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" /> 5 </configSections> 6 <dataConfiguration defaultDatabase="Connection String" /> 7 <connectionStrings configProtectionProvider="RsaProtectedConfigurationProvider"> 8 <EncryptedData Type="http://www./2001/04/xmlenc#Element" 9 xmlns="http://www./2001/04/xmlenc#"> 10 <EncryptionMethod Algorithm="http://www./2001/04/xmlenc#tripledes-cbc" /> 11 <KeyInfo xmlns="http://www./2000/09/xmldsig#"> 12 <EncryptedKey xmlns="http://www./2001/04/xmlenc#"> 13 <EncryptionMethod Algorithm="http://www./2001/04/xmlenc#rsa-1_5" /> 14 <KeyInfo xmlns="http://www./2000/09/xmldsig#"> 15 <KeyName>Rsa Key</KeyName> 16 </KeyInfo> 17 <CipherData> 18 <CipherValue>g2QFQqbHU1L6WUPYqjADqFAvHcdq/7dqCd1U9GlQFEi/nHDVHjqsWvjNywOZtQQg7Q/yW7g8xlRCo0h2+yYd/tQTNoVMu/RKdJmSjZMnmnwpWq+S2VEWK4U106JQwLCfBR/bAF4DHvG47B9KB0JbRfXBt5V2wJVaAI9u3kzuj50=</CipherValue> 19 </CipherData> 20 </EncryptedKey> 21 </KeyInfo> 22 <CipherData> 23 <CipherValue>blwV/ZW1izFZL80YL5RkcjrIjWkQ0L1gJhgZbxEzzTgOcT24ihrAnv3/rDCG+WIZ7TL5D/rMm7dQwkIsij1Sh3befg6F3+pxcW4oe1w/bovIKuzjs3tokUpBvTTj+fsCs2W/MWUhQaWMKQWkHfS2Ajt6gL6MTYtb3pfQUp0pdHbeRxoqdiAksQ1Zzsi1FtRTi7gTT7hnpF0pJs+W9mxTVDMO/qSZXfXLOEMIs/A5ExcfvR5GjpaPuDeLuSsCN3XtjaiXzaDQ3It7j+r66+L2C0xvEhbT9SsG</CipherValue> 24 </CipherData> 25 </EncryptedData> 26 </connectionStrings> 27</configuration> 由此可見,我們已經(jīng)完成了任務(wù),現(xiàn)在只需要將Web.config文件名改回App.config即可,在應(yīng)用程序項(xiàng)目中無(wú)需對(duì)該文件進(jìn)行解密操作,.NET框架會(huì)自動(dòng)替我們完成,如果想解密該文件也很簡(jiǎn)單,在SDK命令提示里輸入aspnet_regiis -pdf "配置節(jié)" "目錄"即可。 |
|
來(lái)自: Alex847 > 《.NET相關(guān)》