1. 程式人生 > >圖解asp.net資料庫連線字串加密和aspnet_regiis

圖解asp.net資料庫連線字串加密和aspnet_regiis

一 使用aspnet_regiis加密資料庫連線字串

專案在C:\sl2\28\ShoppingCart\

加密前;


此次加密使用的命令;


加密後;資料庫名,使用者名稱,口令,都看不到了;


程式碼中照常讀取,.net會自行解密,不必自己寫程式碼解密;

二 aspnet_regiis.exe用法


C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe

aspnet_regiis.exe [-i[r] [-enable] | -u[a] | -r | -s[n] <path> | -k[n] <path> | -lv | -lk | -c | -e[a] | -?]

-i - 安裝 ASP.NET 的此版本,並更新 IIS 元資料庫根處的
指令碼對映和根以下的所有
指令碼對映。現有的低版本指令碼對映
升級到此版本。
說明:使用“aspnet_regiis.exe -i”命令可以重新安裝ASP.NET,如果ASP.NET出了什麼問題,一般用這個命令都能解決。

-ir - 安裝 ASP.NET 的此版本,僅註冊。不
更新 IIS 中的指令碼對映。

-enable - 帶 -i 或 -ir 指定 -enable 時,還將
在 IIS 安全控制檯(IIS 6.0 或更高版本)中啟用 ASP.NET。

-s <path> - 在指定的路徑以遞迴方式安裝此版本
的指令碼對映。現有的低版本指令碼對映
升級到此版本。
例如 aspnet_regiis.exe -s W3SVC/1/ROOT/SampleApp1

-sn <path> - 在指定的路徑以非遞迴方式安裝此版本的
指令碼對映。現有的低版本指令碼對映
升級到此版本。

-r - 為 IIS 元資料庫根位置的此版本
以及根以下的所有指令碼對映安裝指令碼對映。不論當前版本是什麼,
所有現有的指令碼對映都
更改為此版本。

-u - 解除安裝 ASP.NET 的此版本。到此版本的
現有指令碼對映重新對映到此計算機上安裝的
其餘的最高 ASP.NET 版本。

-ua - 解除安裝計算機上的所有 ASP.NET 版本

-k <path> - 從指定的路徑中以遞迴方式移除到任何 ASP.NET 版本的所有
指令碼對映。
例如 aspnet_regiis.exe -k W3SVC/1/ROOT/SampleApp1

-kn <path> - 從指定的路徑中以非遞迴方式移除到任何 ASP.NET 版本的所有
指令碼對映。

-lv - 列出計算機上安裝的所有
ASP.NET 版本(包括狀態和安裝路徑)。
Status: Valid[ (Root)]|Invalid

-lk - 列出包含 ASP.NET 指令碼對映的所有 IIS 元資料庫項的所有路徑
(連同版本一起)。不顯示從父項
繼承 ASP.NET 指令碼對映的項。

-c - 將客戶端指令碼的此版本安裝到
每個 IIS 站點目錄的 aspnet_client 子目錄中。
說明:如果提示缺少js指令碼檔案,就使用“aspnet_regiis.exe -c”命令。
-e - 從每個 IIS 站點目錄的 aspnet_client 子目錄中
移除客戶端指令碼的此版本。

-ea - 從每個 IIS 站點目錄的 aspnet_client 子目錄中
移除客戶端指令碼的所有版本。

-? - 列印此幫助文字。

三 利用ASP.NET加密和解密Web.config中連線字串

http://www.cnblogs.com/mmbo/archive/2010/08/25/1808138.html

介紹

這篇文章我將介紹如何利用ASP.NET來加密和解密Web.config中連線字串

背景描述

在以前的部落格中,我寫了許多關於介紹 Asp.net, Gridview, SQL Server, Ajax, JavaScript等的文章。大多數情況下,我都把資料庫的連線字串放在了web.config中。其中包含許多敏感資訊,包括連線資料庫的使用者名稱密碼等。然而我們在web.config和machine.config中以純文字的方式儲存密碼安全嗎?

如果我們的程式只是部署在內部伺服器中,這應該沒什麼問題。但如果我們的程式是執行在共享主機上面,那我們應該提高安全等級了。ASP. NET 2.0提供了一個保護配置模型來加密和解密web.config中sections資訊。RSAProtectedConfigurationProvider:預設通過RSA公鑰來加密和解密。

通過在命令列中工具執行aspnet_regiis.exe命令,可以對web.config中的連線串進行加密和解密。

第一種方式

首先,我們通過在windows命令列中執行aspnet_regiis.exe來加密與解密。

在VS中建立一個新的websit專案,開啟web.config,加入資料庫連線串,如:

然後我們按下面的步驟來加密和解密資料連線串

 <connectionStrings>
    <add name="dbconnection" connectionString="Data Source=RahulMittal;Integrated Security=true;Initial Catalog=MySampleDB"/>
  </connectionStrings >
1. 開始選單>>所有程式>>Microsoft visual studio 2008 >> Visual Studio Tools >> Visual Studio 2008 開發人員命令提示(如果是windows7,點右鍵與管理員身份執行)

2. 在命令視窗中,輸入命令 aspnet_regiis.exe -pef "connectionStrings" "C:\VisualStudio2008\Authorization"

 –pef表明程式是以檔案系統的形式建立的。第二個“connectionStrings”是你要加密的configuration 節點名字。第三個引數指名 web.config的物理路徑。

3. 成功執行命令後會顯示:加密成功。

現在,再開啟程式中的 web.config,會變成像下面這樣子了。

複製程式碼
<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>ZNUbIEnOwlZzC8qbzHj5F2GS9gLYSkWCIgCJGkrgZAX8A+8oEIssyohhxUKvAubD3jizFc5IjbLGt7HNXhoFhXNTUPYz2y6tdKJDVgDmtCgVf8Z2C990zoMRBJG+VXhmgnlo1vtHYhGx8x/bBzE1prT1+xDpep98vHF22d+LrVI=</CipherValue>
          </CipherData>
        </EncryptedKey>
      </KeyInfo>
      <CipherData>
        <CipherValue>tODWlPD0Q/B/mP14GQ/5tUxcjmhHcy9a0oPunV5osNrMQRztgi2h5V6sxJOEh+NC+G9gQNkv1huXf1s7eoZRRLy5/LDtLXzzqMUOqLSlJUs9igChvi33c9XG4rwGF15Tpn4N34bpQBt94n0rpSkQ18V9HCPzii+UO64PlA+ykDeQhc9aQr4gO3mCfUzmY2S9gsXzRbzdq0oCWBDvx8UkX2uDxaysVHC9Fo7u6IrlpU0+hOdK95Y3/A==</CipherValue>
      </CipherData>
    </EncryptedData>
  </connectionStrings>
複製程式碼
我們在程式中並不要寫任何程式碼來解密連線字串,因為.NET會自動的為我們解密。如果我們要用連線字串,可以像平常那樣呼叫.

string strconnection = ConfigurationManager.AppSettings["dbconnection"].ToString();
如果我們想解密,只需要在VS的命令視窗中,輸入aspnet_regiis.exe -pdf "connectionStrings" "C:\VisualStudio2008\Authorization"
成功執行後,會顯示解密成功。
再開啟web.config,我們可以看到解密後的字串。

現在,我們知道了如何在檔案系統中加密和解密連線字串。如果我們想加密執行在IIS上的預設網站,就像IE上展示的那樣,可以用下面的命令。

加密IIS預設網站的web.config

aspnet_regiis.exe -pe "connectionStrings" -app "/SampleWebSite"
-pe說明程式是執行在IIS上的。第二個引數指名要加密的configuration節點。-app用來指定虛擬目錄,最後一個引數就是程式部署的虛擬目錄名。

 Decrypt connectionStrings in web.config of IIS based site

解密IIS預設網站上的web.config

aspnet_regiis.exe -pd "connectionStrings" -app "/SampleWebSite"
到這裡我們知道如何用命令列工具執行aspnet_regiis.exe命令來加密和解密web.config了。下面我將介紹如何在後臺程式碼中來加密解密web.config。

第二種方式

在第二種方法中我會用RSAProtectedConfigurationProvider和DataProtectionConfgurationProvider來加密解密web.config

首先,開啟Default.aspx,新增如下程式碼:

複製程式碼
<html xmlns="http://www.w3.org/1999/xhtml">
    <head  runat="server">
      <title>Untitled Page</title>
    </head>
    <body>
      <form id="form1"  runat="server">
        <div>
          <asp:Button id="btnEncrypt" runat="server" Text="Encrypt" onclick="btnEncrypt_Click" />
          <asp:Button ID="btnDecrypt" runat="server" Text="Decrypt" onclick="btnDecrypt_Click" />
        </div>
      </form>
    </body>
  </html>
複製程式碼
開啟後臺程式碼,新增下列名稱空間:

using System;
using System.Configuration;
using System.Web.Configuration;
再新增如下程式碼

複製程式碼
string provider = "RSAProtectedConfigurationProvider";
string section = "connectionStrings";
protected void Page_Load(object sender, EventArgs e)
{
 
}
protected void btnEncrypt_Click(object sender, EventArgs e)
{
   Configuration confg = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
   ConfigurationSection configSect = confg.GetSection(section);
   if (configSect != null)
   {
      configSect.SectionInformation.ProtectSection(provider);
      confg.Save();
   }
}
 
protected void btnDecrypt_Click(object sender, EventArgs e)
{
   Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
   ConfigurationSection configSect = config.GetSection(section);
   if (configSect.SectionInformation.IsProtected)
   {
      configSect.SectionInformation.UnprotectSection();
      config.Save();
   }
}
複製程式碼
完成之後,開啟web.config,新增資料庫連線字串

 <connectionStrings>
    <add name="dbconnection" connectionString="Data Source=RahulMittal;Integrated Security=true;Initial Catalog=MySampleDB"/>
  </connectionStrings >
現在執行程式並點選加密按鈕之後,再開啟web.config,會變成下面那樣:

複製程式碼
<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>WagJ9DDjWTNc1nmYVNQXaQqXalQzXaiCHAOtUJvTWBRZiuT6UK1fBElM80PnL6dC5Umb8qvfHdkSMgoMW9CJzwOTZ0zTy17JBGZqRQmlfW2G9LacoWIil0UrxjhgmJmRXhwXHFpdGwEVl7AoQGVlJGabXuChutaTxmfGOoUbCr0=</CipherValue>
          </CipherData>
        </EncryptedKey>
      </KeyInfo>
      <CipherData>
        <CipherValue>qry5qnr3qxOgyoNPeP7OKEiHpr/PPTsaeQ2mYUsSK7cg4Kkl9uPO4RyUXgBIkgCTsjbObqLlyndcSBnYyek6bxG/IBL82G1R5J1ci8i1eyt8kIDqouzYOx5vtouErld4z1L+7WGf9Wg37QAH5RiiEfkCHndJJq3dTqjxnnXZSno6NgbxSXDfqzwE/eKDVhGV3oaTQSfjVmO8e5a9wvREYeeyasDhojx8J2mdy7/Q9rEIpv98RTiRxA==</CipherValue>
      </CipherData>
    </EncryptedData>
  </connectionStrings>
複製程式碼
如果我們想用DataProtectionConfigurationProvider來實現加密與解密,只需在程式碼中將RSAProtectedConfigurationProvider替換成DataProtectionConfigurationProvider即可。

原文:http://www.codeproject.com/Tips/304638/Encrypt-or-Decrypt-Connection-Strings-in-web-confi