日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

解决打不开 RSA 密钥容器 即:加密web.config中的内容

發布時間:2024/8/22 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 解决打不开 RSA 密钥容器 即:加密web.config中的内容 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

簡單的解決方法:

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中加入如下內容<configProtectedData> <providers> <clear /> <add name="ConnectionStringsKeyProvider"       type="System.Configuration.RsaProtectedConfigurationProvider, System.Configuration, Version=2.0.0.0,Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"       keyContainerName="ConnectionStringsKey"       useMachineContainer="true"/> </providers> </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頁面:加密連接字符串:界面如圖:

后臺代碼: //加密按鈕 protected void Button1_Click(object sender, EventArgs e) { //①需要加密的節點: string name = @"connectionStrings"; //②當前路徑; string appPath = "/loginContral"; Configuration config = WebConfigurationManager.OpenWebConfiguration(appPath); //③提供加密的方式:(這里使用自定義的加密方式) // string provider = "RsaProtectConfigurationProvider"; string provider = "ConnectionStringsKeyProvider"; config.GetSection(name).SectionInformation.ProtectSection(provider); //⑤保存web.config文件 try { config.Save(); } catch (Exception ex) { Response.Write(ex.Message); } if (config.GetSection(name).SectionInformation.IsProtected) { Button1.Enabled = false; Response.Write("加密成功!"); } else { Response.Write("加密失敗!"); } } //解密按鈕: protected void Button2_Click(object sender, EventArgs e) { //①需要節密的節點: string name = @"connectionStrings"; //②當前路徑; string appPath = "/loginContral"; Configuration config = WebConfigurationManager.OpenWebConfiguration(appPath); //③使用UnprotectSection方法進行解密; config.GetSection(name).SectionInformation.UnprotectSection(); //④保存web.config文件 config.Save(); if (config.GetSection(name).SectionInformation.IsProtected==false) { Button2.Enabled = false; Response.Write("解密成功!"); } else { Response.Write("解密失敗!"); } }

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

加密前的連接字符串: <connectionStrings> <add name="connection" connectionString="data source=.;database=aspnetdb;user id=sa;pwd=123;" /> </connectionStrings> ?????
加密后的連接字符串:

<connectionStrings configProtectionProvider="ConnectionStringsKeyProvider"> <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>AepogG4vVhd8K6NVhVmdO8FAGFMopOdDvnBN5vPV0mxP8NgrImnZFvflrhhvooiu56McmMr6n5cUnixzimGB/zTgCNMsIkU8Sr6YtX8iUh64U9IVujwaOAbtZp4AhLhMiH6YwkHXjmqrjYyS2ecsocquZQ0ndkKC3OMg/UcOIk0=</CipherValue> </CipherData> </EncryptedKey> </KeyInfo> <CipherData> <CipherValue>biMAH/6vwvi0FKvqijpSZzKhk+a6QNi0Aa794yxi1X+sffKdtsUR15hVcByOlborcKPRhX94MpOm2eKoBqYVyCf24PdYAkIFFAzO1sluzmUtcXFVU/lTBqn83bnJDgBgo6eVtDg4m7DSAVR6qWyEP8wySqWWuBkWSLzsMynqPOyGhVB9bTVJbSCWiUZ4ynFhvUTziGISJQA=</CipherValue> </CipherData> </EncryptedData> </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對配置文件進行解密。

?

轉載于:https://www.cnblogs.com/zhangqs008/archive/2009/09/09/2341254.html

總結

以上是生活随笔為你收集整理的解决打不开 RSA 密钥容器 即:加密web.config中的内容的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。