Microsoft Enterprise Library 5.0 系列教程(二) Cryptography Application Block (初级)
企業庫加密應用程序模塊提供了2種方式讓用戶保護自己的數據:
使用企業庫加密應用程序模塊的優勢:
下面介紹如何使用Microsoft Enterprise Library 5.0中的加密應用程序模塊.
1.下載安裝好MicrosoftEnterprise Library 5.0,然后在運行EntLibConfig.exe
2. 選擇Blocks菜單 ,單擊?Add CryptographySettings .
下面分別樣式如何創建Hash Providers?和?Symmetric CryptographyProviders?加密策略:
(A)???Hash Providers?策略使用步驟:
(1)????點擊HashProviders?區塊右上角的加號按鈕,?Add Hash Providers, 然后點擊Add Hash Algorithm Provider,在彈出的對話框中選擇System.Core下的MD5Cng,
? 表示我們要用MD5的加密方法獲取離散值.
(2) 點擊?File?菜單,單擊?Save,保存為一個App.config文件,可以先保存到桌面,之后要用到它. 用記事本打開App.config,可以看到如下內容.
代碼(3) 要使用緩存應用程序模塊, 需要導入相應的Dll文件,在此我們要導入的是Microsoft.Practices.EnterpriseLibrary.Caching.dll?,將App.config文件添加到項目中,
并添加usingMicrosoft.Practices.EnterpriseLibrary.Security.Cryptography引用:
添加引用:
usingMicrosoft.Practices.EnterpriseLibrary.Security.Cryptography;(4) 測試:
usingSystem;using?System.Collections.Generic;
using?System.Linq;
using?System.Text;
using?Microsoft.Practices.EnterpriseLibrary.Security.Cryptography;
namespace?test
{
classProgram
{
staticvoid Main(string[]args)
{
//獲取離散碼
stringhash?=?Cryptographer.CreateHash("MD5Cng",?"SensitiveData");
//打印顯示
Console.WriteLine(hash);
Console.WriteLine("------------------------------------------------");
//驗證
boolequal?=?Cryptographer.CompareHash("MD5Cng",?"SensitiveData",hash);
//打印結果
if(equal)
{
Console.WriteLine("正確");
}
else
{
Console.WriteLine("錯誤");
}
}
}
}
運行結果:
(B)????Symmetric CryptographyProviders策略實現步驟:
(1)????點擊symmetriccryptography provider??區塊右上角的加號按鈕,然后點擊?Add Symmetric Cryptography Providers, 在此我們能看到3個選項,下面介紹一下:
- Add Custom SymmetricCrypto Provider :顧名思義,用戶自定義的加密策略,較麻煩,要自己寫相應的加密類.?
- Add DPAPI Symmetric Crypto Provider :?添加一個數據加密API生成的對稱密鑰進行加密.
- Add Sysmmetric Algorithm Provider :?較高級的對稱加密方法,需要用戶生成Key文件對數據進行保護.
? 在此我介紹的是第二種方法,因此請單擊選擇?Add DPAPI Symmetric Crypto Provider.
?
(2)????點擊?File?菜單,單擊?Save更新原有的App.config文件,打開可看到以下內容.
代碼(3)???? 測試:
using?System;using?System.Collections.Generic;
using?System.Linq;
using?System.Text;
using?Microsoft.Practices.EnterpriseLibrary.Security.Cryptography;
namespace?test
{
class?Program
{
staticvoid?Main(string[] args)
{
////獲取離散碼
//string hash = Cryptographer.CreateHash("MD5Cng", "SensitiveData");
////打印顯示
//Console.WriteLine(hash);
//Console.WriteLine("------------------------------------------------");
////驗證
//bool equal = Cryptographer.CompareHash("MD5Cng", "SensitiveData", hash);
////打印結果
//if (equal)
//{
//?Console.WriteLine("正確");
//}
//else
//{
//?Console.WriteLine("錯誤");
//}
string?Encrypt?=?Cryptographer.EncryptSymmetric("DPAPI Symmetric Crypto Provider",?"SensitiveData");
Console.WriteLine("密文:"+?Encrypt);
Console.WriteLine("------------------------------------------------");
Encrypt?=?Cryptographer.DecryptSymmetric("DPAPI Symmetric Crypto Provider", Encrypt);
Console.WriteLine("原文:"+?Encrypt);
}
}
}
運行結果:
?
本文轉自黃聰博客園博客,原文鏈接:http://www.cnblogs.com/huangcong/archive/2010/05/28/1746634.html,如需轉載請自行聯系原作者
總結
以上是生活随笔為你收集整理的Microsoft Enterprise Library 5.0 系列教程(二) Cryptography Application Block (初级)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: replicated vs global
- 下一篇: (转)shiro权限框架详解02-权限理