天天看點

黃聰:Microsoft Enterprise Library 5.0 系列教程(二) Cryptography Application Block (初級)

企業庫加密應用程式子產品提供了2種方式讓使用者保護自己的資料:

Hashingproviders:  離散加密法, 簡單來說就是把你的資訊儲存到記憶體中後用一個離散值表示并傳回給程式,這樣在程式中隻能看到離散值而不是明文,這樣就起到簡單的加密效果啦.

Cryptographyproviders: 密鑰加密法. 用對稱加密方法對資料進行加密(尚未支援非對稱加密).

使用企業庫加密應用程式子產品的優勢:

減少了需要編寫的模闆代碼,執行标準的任務,可以用它來解決常見的應用程式加密的問題.

有助于維持一個應用程式内和跨企業的資料傳輸加密.

允許管理者進行加密配置,包括使用組政策.

可擴充,支援使用者自定義加密技術.

下面介紹如何使用Microsoft Enterprise Library 5.0中的加密應用程式子產品.

1.下載下傳安裝好MicrosoftEnterprise Library 5.0,然後在運作EntLibConfig.exe

黃聰:Microsoft Enterprise Library 5.0 系列教程(二) Cryptography Application Block (初級)
黃聰:Microsoft Enterprise Library 5.0 系列教程(二) Cryptography Application Block (初級)

2. 選擇Blocks菜單 ,單擊 Add CryptographySettings .

黃聰:Microsoft Enterprise Library 5.0 系列教程(二) Cryptography Application Block (初級)

下面分别樣式如何建立Hash Providers 和 Symmetric CryptographyProviders 加密政策:

(A)   Hash Providers 政策使用步驟:

  (1)    點選HashProviders 區塊右上角的加号按鈕, Add Hash Providers, 然後點選Add Hash Algorithm Provider,在彈出的對話框中選擇System.Core下的MD5Cng,

      表示我們要用MD5的加密方法擷取離散值.

黃聰:Microsoft Enterprise Library 5.0 系列教程(二) Cryptography Application Block (初級)
黃聰:Microsoft Enterprise Library 5.0 系列教程(二) Cryptography Application Block (初級)

  (2) 點選 File 菜單,單擊 Save,儲存為一個App.config檔案,可以先儲存到桌面,之後要用到它. 用記事本打開App.config,可以看到如下内容.

黃聰:Microsoft Enterprise Library 5.0 系列教程(二) Cryptography Application Block (初級)
黃聰:Microsoft Enterprise Library 5.0 系列教程(二) Cryptography Application Block (初級)

代碼

<configuration>

<configSections>

<section name="securityCryptographyConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.Configuration.CryptographySettings,Microsoft.Practices.EnterpriseLibrary.Security.Cryptography, Version=5.0.414.0,Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true"/>

</configSections>

<securityCryptographyConfiguration>

<hashProviders>

<add name="MD5Cng" type="Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.HashAlgorithmProvider,Microsoft.Practices.EnterpriseLibrary.Security.Cryptography, Version=5.0.414.0,Culture=neutral, PublicKeyToken=31bf3856ad364e35"

algorithmType="System.Security.Cryptography.MD5Cng,System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"

saltEnabled="true"/>

</hashProviders>

</securityCryptographyConfiguration>

</configuration>

  (3) 要使用緩存應用程式子產品, 需要導入相應的Dll檔案,在此我們要導入的是Microsoft.Practices.EnterpriseLibrary.Caching.dll ,将App.config檔案添加到項目中,

     并添加usingMicrosoft.Practices.EnterpriseLibrary.Security.Cryptography引用:

黃聰:Microsoft Enterprise Library 5.0 系列教程(二) Cryptography Application Block (初級)
黃聰:Microsoft Enterprise Library 5.0 系列教程(二) Cryptography Application Block (初級)

  添加引用:

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("錯誤");

運作結果:

黃聰:Microsoft Enterprise Library 5.0 系列教程(二) Cryptography Application Block (初級)

(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.

   

黃聰:Microsoft Enterprise Library 5.0 系列教程(二) Cryptography Application Block (初級)
黃聰:Microsoft Enterprise Library 5.0 系列教程(二) Cryptography Application Block (初級)

  (2)    點選 File 菜單,單擊 Save更新原有的App.config檔案,打開可看到以下内容.

黃聰:Microsoft Enterprise Library 5.0 系列教程(二) Cryptography Application Block (初級)
黃聰:Microsoft Enterprise Library 5.0 系列教程(二) Cryptography Application Block (初級)

<securityCryptographyConfiguration defaultHashInstance="MD5Cng">

<symmetricCryptoProviders>

<add type="Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.DpapiSymmetricCryptoProvider,Microsoft.Practices.EnterpriseLibrary.Security.Cryptography, Version=5.0.414.0,Culture=neutral, PublicKeyToken=31bf3856ad364e35"

name="DPAPISymmetric Crypto Provider"/>

</symmetricCryptoProviders>

  (3)     測試:

using System;

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);

黃聰:Microsoft Enterprise Library 5.0 系列教程(二) Cryptography Application Block (初級)

繼續閱讀