配置錯誤:未能使用提供程序“RsaProtectedConfigurationProvider”進行解密。提供程序返回錯誤信息為: 打不開 RSA 密鑰容器。


http://www.cnblogs.com/jiaruistone/articles/1441634.html

 

在asp.net2.0中新增了對web.config中的部分數據進行加密的功能,可以使用RSAProtectedConfigurationProvider和DPAPIProtectedConfigurationProvider來加密,本文說明使用RSAProtectedConfigurationProvidert和計算機級別的密鑰容器進行加密的步驟。
 
1.          首先確定要進行加密的web.config中的配置節是否可以加密
 
2.          創建RSA密鑰容器
 
3.          在web.config中標識要使用的密鑰容器
 
4.          對web.config進行加密
 
5.          授予對 RSA 密鑰容器的訪問權限

 

我們如果想對web.config的數據庫連接字符串進行加密的話,那么這里提供了兩個方法。 

方法一、 
    使用“DataProtectionConfigurationProvider”形式加密,創建test.aspx文件,代碼如下: 
需要添加引用 
using System.Web.Configuration; 
using System.IO; 

//加密 
protected void Button1_Click(object sender, EventArgs e) 
    { 
        Configuration config =  WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath); 
        ConfigurationSection section = config.GetSection("connectionStrings"); 
        
        if (section != null && !section.SectionInformation.IsProtected) 
        { 
            section.SectionInformation.ProtectSection("DataProtectionConfigurationProvider"); 
            config.Save(); 
        } 

    } 
//解密 
    protected void Button2_Click(object sender, EventArgs e) 
    { 
        Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath); 
        ConfigurationSection section = config.GetSection("connectionStrings"); 

        if (section != null && section.SectionInformation.IsProtected) 
        { 
            section.SectionInformation.UnprotectSection(); 
            config.Save(); 
        } 
    } 


總結:此方法很方便,並且很簡單,但安全性沒有密鑰加密高。 

 

方法二、 
使用“RSAProtectedConfigurationProvider”形式來加密 
1.test.aspx程序文件基本如上, 
把 
section.SectionInformation.ProtectSection("DataProtectionConfigurationProvider"); 
改成 
section.SectionInformation.ProtectSection("RSAProtectedConfigurationProvider"); 
但這個時候你訪問網站的時候很有可能會出現 

2.aspnet_regiis -pe "connectionStrings" -app "/SampleApplication" -prov "RsaProtectedConfigurationProvider"

說明: 在處理向該請求提供服務所需的配置文件時出錯。請檢查下面的特定錯誤詳細信息並適當地修改配置文件。 
分析器錯誤信息: 未能使用提供程序“RsaProtectedConfigurationProvider”進行解密。提供程序返回錯誤信息為: 打不開 RSA 密鑰容器。 

這樣的錯誤,解決方法是: 


進dos運行:aspnet_regiis -pa "NetFrameworkConfigurationKey" "NT AUTHORITY\NETWORK SERVICE" 
  如果運行出錯,需要把目錄 C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727 放入環境變量path中。 
  此時就可以成功訪問網站了。 
  同樣可以通過命令行來實現“RSAProtectedConfigurationProvider”加密 (若要使用計算機配置中指定的默認 RsaProtectedConfigurationProvider,必須首先向應用程序的 Windows 標識授予對名為 NetFrameworkConfigurationKey 的計算機密鑰容器的訪問權限,該計算機密鑰容器是為該默認提供程序指定的密鑰容器。)
  aspnet_regiis -pe "connectionStrings" -app "/SampleApplication" -prov "RsaProtectedConfigurationProvider"


  注意:你也可以不運行 aspnet_regiis -pa "NetFrameworkConfigurationKey" "NT AUTHORITY\NETWORK SERVICE"命令來注冊默認的 RsaProtectedConfigurationProvider 的RSA 密鑰容器 
       

方法如下: 
        1)創建一個可導出的rsa密鑰容器,命名為Key 

aspnet_regiis -pc "Key" -exp 


2)在你要加密的信息前面指定密鑰容器,如: 

<configProtectedData> 

        <providers> 

            <clear /> 

            <add name="KeyProvider" 

       type="System.Configuration.RsaProtectedConfigurationProvider, System.Configuration, Version=2.0.0.0,Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" 

       keyContainerName="Key" 

       useMachineContainer="true"/> 

        </providers> 

</configProtectedData> 


<connectionStrings> 

        <add name="SQLConnString" connectionString="Data Source=yourIP;Initial Catalog=test;User Id=yourID;Password=yourPassword;" 

        providerName="System.Data.SqlClient" /> 


</connectionStrings> 

並且確保在configuration節的xmlns屬性有如下值: 

<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0"> 


3)對配置文件進行加密 

aspnet_regiis -pef "connectionStrings" "E:\project\Test" -prov "KeyProvider" 

參數分別為:需要加密的配置節、項目所在目錄的物理路徑、加密所使用的密鑰容器名稱 

再看web.config文件,就會發現connectionStrings節已經被加密了,但是運行程序會發現程序仍然可以正確訪問數據庫。 

此時,只需運行: 

aspnet_regiis -pdf "connectionStrings" "E:\project\Test" 

就可以對web.config文件進行解密。 

(注意,如果還是有錯誤,那可能是您沒有給生成的密匙文件足夠的權限,去到 C:\Documents and Settings\All Users\Application Data\Microsoft\Crypto\RSA\MachineKeys 目錄下, 
  找到剛生成的密匙文件,把network service用戶的讀取權限賦予給它,就可以了,直接用命令的話也可以:命令如下 aspnet_regiis -pa "Key" "NT AUTHORITY\NETWORK SERVICE" ,可能需要重新啟動iis) 


4)把密鑰容器導出為xml文件 

aspnet_regiis -px "Key" "e:\Key.xml" 

這個命令只導出公鑰,因此以后只能用於加密,而無法解密。 

aspnet_regiis -px "Key" "e:\Keys.xml" -pri 

這個則連私鑰一起導出了,所以我們要用這個。 


5)把密鑰容器刪除 

aspnet_regiis -pz "Key" 

刪除后再運行程序,會提示出錯: 

分析器錯誤信息: 未能使用提供程序“KeyProvider”進行解密。提供程序返回錯誤信息為: 打不開 RSA 密鑰容器。 

同理可以證明,在任何一台未安裝正確的密鑰容器Key的機器上,程序都無法對connectionStrings節進行解密,因此也就無法正常運行。 


6)導入key.xml文件 

aspnet_regiis -pi "Key" "e:\Keys.xml" 

此時,再運行程序會發現又可以解密了。證明加密與解密機制運行正常。 


最后說一下這個機制所提供的安全性保障可以運用在什么方面: 

對winform程序的app.config進行加密實際意義並不大,因為無論如何,客戶機都可以通過運行aspnet_regiis -pdf 來對配置文件進行解密,從而暴露敏感信息。 

對於web.config進行加密的意義也僅限於,當web.config文件不小心泄露時,不會同時泄露敏感信息,如果惡意攻擊者已經取得了在服務器上運行程序的權限,那么同app.config一樣,可以很容易通過通過運行aspnet_regiis -pdf 獲取明文了。 


還有,通過aspnet_regiis -pa "Key" "NT AUTHORITY\NETWORK SERVICE"控制對不同用戶對密鑰容器的訪問權限,應該還可以進一步獲取一些安全性,比如可以控制某些用戶即使登錄到服務器上,也無法用aspnet_regiis -pdf對配置文件進行解密。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM