加密webconfig中的連接字串,利用RSA非對稱式加密,利用windows儲存密鑰容器

來源:互聯網
上載者:User

標籤:encrypted   服務   apr   margin   rsa   利用   ext   介面   資訊   

簡單的解決方案:

WebConfig 加解密,未能使用提供者“RsaProtectedConfigurationProvider”進行解密。提供者返回錯誤訊息為: 打不開 RSA 密鑰容器。
問題:未添加用於訪問 RSA 密鑰容器
命令:aspnet_regiis -pa "NetFrameworkConfigurationKey" "NT AUTHORITY/NETWORK SERVICE"
注意事項:XP下:aspnet_regiis -pa "NetFrameworkConfigurationKey" "aspnet"
加密:aspnet_regiis -pe "appSettings" -app "/應用程式名稱"
解密:aspnet_regiis -pd "appSettings" -app "/應用程式名稱"  如(/PetShop/web)

更靈活的解決方案:
1、建立一個密鑰容器 
   aspnet_regiis -pc "ConnectionStringsKey" -exp
   ConnectionStringsKey為密鑰容器的名稱 
   可以使用aspnet_regiis /?查看該命令的用法

2、在web.config中加入如下內容

[c-sharp] view plain copy print?
  1. <configProtectedData>   
  2.         <providers>   
  3.             <clear />   
  4.             <add name="ConnectionStringsKeyProvider"  
  5.         type="System.Configuration.RsaProtectedConfigurationProvider, System.Configuration, Version=2.0.0.0,Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"   
  6.         keyContainerName="ConnectionStringsKey"   
  7.         useMachineContainer="true"/>   
  8.         </providers>   
  9. </configProtectedData>  

 
3、通過命令列:用指定的祕密金鑰加密指定目錄下的web.config檔案的指定的配置節
     aspnet_regiis -pef "connectionStrings" "d:/testproj/websitetest" -prov "ConnectionStringsKeyProvider"
    對於子配置節用/分隔表示, 如identity配置節 需要寫成 "system.web/identity"
4、如果訪問web程式,頁面提示 Error message from the provider: The RSA key Container could not be opened.
     是由於network service帳戶無法存取金鑰檔案造成的。 找到密鑰檔案, 賦予network service讀許可權。該密鑰檔案位於(可按時間排序,找到自己產生的那個密鑰檔案)
vista: c:/ProgramData/Microsoft/Crypto/RSA/MachineKeys/
xp或其他:C:/Documents and Settings/All Users/Application Data/Microsoft/Crypto/RSA/MachineKeys

至此:查看被加密的標記, 內容就已經是被加密過的了。

5.通過.aspx頁面:加密連接字串:介面

後台代碼:

[c-sharp] view plain copy print?
  1. //加密按鈕  
  2. protected void Button1_Click(object sender, EventArgs e)   
  3. {   
  4.    //①需要加密的節點:   
  5.     string name = @"connectionStrings";   
  6.    //②當前路徑;   
  7.     string appPath = "/loginContral";   
  8.     Configuration config = WebConfigurationManager.OpenWebConfiguration(appPath);   
  9.     //③提供加密的方式:(這裡使用自訂的加密方式)   
  10.     // string provider = "RsaProtectConfigurationProvider";   
  11.     string provider = "ConnectionStringsKeyProvider";    
  12.     config.GetSection(name).SectionInformation.ProtectSection(provider);   
  13.   
  14.     //⑤儲存web.config檔案   
  15.     try   
  16.     {   
  17.         config.Save();   
  18.     }   
  19.     catch (Exception ex)   
  20.     {   
  21.         Response.Write(ex.Message);   
  22.     }   
  23.     if (config.GetSection(name).SectionInformation.IsProtected)   
  24.     {   
  25.         Button1.Enabled = false;   
  26.         Response.Write("加密成功!");   
  27.     }   
  28.     else   
  29.     {   
  30.         Response.Write("加密失敗!");   
  31.     }   
  32. }  
  33.   
  34. //解密按鈕:  
  35.   
  36. protected void Button2_Click(object sender, EventArgs e)   
  37. {   
  38.      //①需要節密的節點:   
  39.      string name = @"connectionStrings";   
  40.   
  41.     //②當前路徑;   
  42.      string appPath = "/loginContral";   
  43.      Configuration config = WebConfigurationManager.OpenWebConfiguration(appPath);   
  44.   
  45.     //③使用UnprotectSection方法進行解密;        
  46.   
  47.      config.GetSection(name).SectionInformation.UnprotectSection();   
  48.   
  49.      //④儲存web.config檔案        
  50.      config.Save();   
  51.   
  52.      if (config.GetSection(name).SectionInformation.IsProtected==false)   
  53.      {   
  54.          Button2.Enabled = false;   
  55.          Response.Write("解密成功!");   
  56.      }   
  57.      else   
  58.      {   
  59.          Response.Write("解密失敗!");   
  60.      }   
  61. }  

注意:string appPath = "/loginContral" 為當前項目路徑;

加密前的連接字串:

[c-sharp] view plain copy print?
  1. <connectionStrings>  
  2.     <add name="connection" connectionString="data source=.;database=aspnetdb;user id=sa;pwd=123;" />  
  3. </connectionStrings>  

     
加密後的連接字串:

 

[c-sharp] view plain copy print?
  1. <connectionStrings configProtectionProvider="ConnectionStringsKeyProvider">  
  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>AepogG4vVhd8K6NVhVmdO8FAGFMopOdDvnBN5vPV0mxP8NgrImnZFvflrhhvooiu56McmMr6n5cUnixzimGB/zTgCNMsIkU8Sr6YtX8iUh64U9IVujwaOAbtZp4AhLhMiH6YwkHXjmqrjYyS2ecsocquZQ0ndkKC3OMg/UcOIk0=</CipherValue>  
  13.                    </CipherData>  
  14.                </EncryptedKey>  
  15.            </KeyInfo>  
  16.            <CipherData>  
  17.                <CipherValue>biMAH/6vwvi0FKvqijpSZzKhk+a6QNi0Aa794yxi1X+sffKdtsUR15hVcByOlborcKPRhX94MpOm2eKoBqYVyCf24PdYAkIFFAzO1sluzmUtcXFVU/lTBqn83bnJDgBgo6eVtDg4m7DSAVR6qWyEP8wySqWWuBkWSLzsMynqPOyGhVB9bTVJbSCWiUZ4ynFhvUTziGISJQA=</CipherValue>  
  18.            </CipherData>  
  19.        </EncryptedData>  
  20.    </connectionStrings>  

 
其他備用操作:
1、解密web.config 
    aspnet_regiis -pdf "connectionStrings" "d:/testproj/websitetest"
2、把密鑰容器匯出為xml檔案 
    aspnet_regiis -px "ConnectionStringsKey" "c:/Key.xml" 。這個命令只匯出公開金鑰,因此以後只能用於加密,而無法解密。
    aspnet_regiis -px "ConnectionStringsKey" "c:/Keys.xml" -pri  這個則連私密金鑰一起匯出了,所以我們要用這個。
3、把密鑰容器刪除  
   aspnet_regiis -pz "LixinKey"   刪除後再運行程式,會提示出錯: 
    分析器錯誤資訊: 未能使用提供者“LixinKeyProvider”進行解密。提供者返回錯誤資訊為: 打不開 RSA 密鑰容器。 
    同理可以證明,在任何一台未安裝正確的密鑰容器LixinKey的機器上,程式都無法對connectionStrings節進行解密,因此也就無 法正常運行。
4、匯入key.xml檔案 
     aspnet_regiis -pi "LixinKey" "c:/Keys.xml"

     此時,再運行程式會發現又可以解密了。證明加密與解密機制運行正常。
最後說一下這個機制所提供的安全性保障可以運用在什麼方面:
1. 對winform程式的app.config進行加密實際意義並不大,因為無論如何,客戶機都可以通過運行aspnet_regiis -pdf 來對設定檔進行解密,從而暴露敏感資訊。
2. 對於web.config進行加密的意義也僅限於,當web.config檔案不小心泄露時,不會同時泄露敏感資訊,如果惡意攻擊者已經取得了在伺服器上運行程式的許可權,那麼同app.config一樣,可以很容易通過通過運行aspnet_regiis -pdf 擷取明文了。
3. 還有,通過aspnet_regiis -pa "Key" "NT AUTHORITY/NETWORK SERVICE"控制對不同使用者對密鑰容器的存取權限,應該還可以進一步擷取一些安全性,比如可以控制某些使用者即使登入到伺服器上,也無法用aspnet_regiis -pdf對設定檔進行解密。

加密webconfig中的連接字串,利用RSA非對稱式加密,利用windows儲存密鑰容器

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.