對web.config文件中的機密數據進行加密

導讀:
  加密web.config中的內容
  1、Create an RSA keypair in ContainerName, -exp means the key is exportable(創建一個密鑰容器)
  aspnet_regiis -pc "ConnectionStringsKey" -exp
  ConnectionStringsKey爲密鑰容器的名稱
  可以使用aspnet_regiis /?查看該命令的用法
  2、在web.config中加入如下內容
  
  
  
    type="System.Configuration.RsaProtectedConfigurationProvider, System.Configuration, Version=2.0.0.0,Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"
  keyContainerName="ConnectionStringsKey"
  useMachineContainer="true"/>
  
  
  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
  至此:查看被加密的標記, 內容就已經是被加密過的了。
  其他備用操作
  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"
  此時,再運行程序會發現又可以解密了。證明加密與解密機制運行正常。

本文轉自
http://zhangxiaoyu0312.blog.sohu.com/71633822.html
發佈了332 篇原創文章 · 獲贊 3 · 訪問量 47萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章