對web.config的ConnectionString加密

來源:互聯網
上載者:User

標籤:des   style   blog   http   io   ar   color   os   使用   

原文:對web.config的ConnectionString加密

      本文參考了wayshan的部落格,原文地址:http://www.cnblogs.com/wayshan/archive/2012/04/09/web_config.html

     

      web.config設定檔中,有一些敏感性資料希望被保護起來,例如資料庫的串連串ConnectionString,預設情況下是明文顯示的,例如:

1 <appSettings>2     <!--資料庫連結-->3     <add key="ConnectionString" value="Data Source=127.0.0.1;Initial Catalog=Demo2012;User ID=sa;password=123456"/>4   </appSettings>

  經過加密之後,配置資訊變為:

 1 <appSettings 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>FSEEyg/SUlE0SqTip30MSc9a/tUYJSDiJ3nDLkE1iGSYU3vo02KRhsTnbLWRSRLffGGWP65fUJJuj5+qiPNQNX0THjCajF43PrP28u9V1fkXuX/T5BxI4Rg2rjw6mSPLuZ1SaQOjJjXi8ArCYs+83qpCxvjml1UN74RlOpLs6qs=</CipherValue>13           </CipherData>14         </EncryptedKey>15       </KeyInfo>16       <CipherData>17         <CipherValue>0Qp07JkMSsyTmHADATQ+rkGCok21XBFH2zvtj4diSSsTNJhi3CzBYw6QfasdpyuG/zf+4RWh4aNJ/IrGavzeSyRa929zAv/RsYpR3GQROWt20zr6d7UJUfDgbm85KyZrQNXLwhcfEiUX5VMJg+g3OUczhiDS8YvQ+JjOCDQQPpv99sj8AT2eNCyJJpKWJpGSf0iBpJmkpNP6MOTYHEEmVQ==</CipherValue>18       </CipherData>19     </EncryptedData>20   </appSettings>

  

  這麼長一串,一定不會有人認出來你的資料庫地址和登陸資訊了   :)

 

      操作過程如下(RsaProtectedConfigurationProvider加密):

      1:運行cmd,並定位到C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727(可以直接運行vs2005的命令提示工具,但是貌似vs2010預設指向的framework3.5裡沒有aspnet_regiis命令,為了避免有些人找不到,所以還是直接cmd比較好。)

      2:運行

1 aspnet_regiis -pef "appSettings" "D:\demo"   //顯示"正在加密配置節,成功!"

          這裡要注意一下,D:\demo是工程的路徑,可以是絕對路徑,也可以是網站的相對路徑,具體方法可以參考aspnet_regiis的協助。如果你把web.config放在d盤根目錄下,那麼路徑就是"D:\\"。appSettings是要加密的節點名稱,也可以針對某個節點的子節點進行加密,例如

1 aspnet_regiis -pef "system.web/authentication" "D:\demo"

      加密完成後,除了視覺上變為複雜的字串之外,使用上與加密前是完全相同的。解密時運行參數-pdf,例如:

1 aspnet_regiis -pdf "appSettings" "D:\demo"  //顯示"正在解密配置節,成功!"

     

      經過加密的字串,雖然可以在本機使用,但是在另外一台電腦上(客戶現場或伺服器)是無法直接使用的。需要進行一些配置。

      1:建立秘鑰容器(pc-操作容器 -exp,可匯出)

1 aspnet_regiis -pc "MyKey" -exp

      2:匯出容器中的秘鑰到xml中(px操作容器檔案,注意D:\\後面有一個空格,否則你找不到檔案的  ^ ^)

1 aspnet_regiis -px "MyKey" "D:\\ keys.xml" -pri

      3:拷貝你的keys.xml檔案到伺服器上,並且匯入秘鑰。(-pi匯入容器裡的秘鑰)

1 aspnet_regiis -pi "MyKey" “D:\keys.xml”

      4:賦許可權(ASPNET許可權)

1 aspnet_regiis -pa "MyKey" "ASPNET"

      5:修改web.config檔案,增加:

1  <configuration>2  <configProtectData defaultProvider="MyProtectedConfigurationProvider">3  <providers>4  <add name="MyProtectedConfigurationProvider" type="System.Configuration.RsaProtectedConfigurationProvider" cspProviderName="" useMachineContainer="true" useOAEP="false" keyContainerName="SharedKeys"/>5  </providers>6  </configProtectedData>7  </configuration>

      完成!

 

      說了這麼多,我累了你也累了。有沒有簡便一些的辦法?有。開發時用明文,部署的時候在客戶現場運行aspnet_regiis加密就可以了。(省略符號)

對web.config的ConnectionString加密

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.