如何加密Web.Config中的連接字串

來源:互聯網
上載者:User

 

如何加密 Web.Config總的連接字串

 

Asp.Net用一種無格式的文字檔儲存所有的配置資訊叫Web.Config和Machine.Config,我們儲存所有致關重要的資訊,包括資料庫連接字串,其中就有資料庫的使用者名稱,密碼,一旦在沒有任何保護的安全措施下,這種後果將不可估計的;
       順著這個線索,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的資料庫連接字串

<configuration>
    <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,

/// <summary>
    /// 加密類  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();
        }
    }

之後再添加一個對Web.Config解密的靜態方法DecrptConnectionString

/// <summary>
     ///  解密類   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>

<configuration>
    <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的翻譯,這是我第一次翻譯文章,如果有什麼
出路的話,還請諒解,謝謝!

相關文章

聯繫我們

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