Enterprise Library 2.0 Hands On Lab 翻譯(14):加密應用程序塊(一)
練習1:加解密字符串
通過本練習將學習通過加密來保護信息,在這里創(chuàng)建一個類似于IM的聊天應用程序,加密通信過程中的信息。
第一步
打BugSmak.sln項目,默認的安裝路徑應該為C:\Program Files\Microsoft Enterprise Library January 2006\labs\cs\Cryptography\exercises\ex01\begin,并編譯。
第二步 回顧應用程序
1.在解決方案管理器選中Chat.cs文件,選擇View | Code菜單命令。Chat窗體用來接收和發(fā)送信息,上面的灰色TextBox用來顯示聊天信息,底部白色的TextBox用來發(fā)送新的消息。

2.選擇Debug | Start Without Debugging命令運行應用程序,聊天窗口將被打開,分別叫做Sam和Toby,消息可以在這兩個窗口之間傳遞,在Toby的消息文本框中輸入一些字符,并單擊Send按鈕,在Sam窗體中作重復做一次。可以看到交流信息顯示在了聊天窗體中。還有一個控制臺應用程序顯示,它用來監(jiān)視聊天的過程,所有的消息都將在這里顯示。

3.關(guān)閉所有窗體并關(guān)閉應用程序。
第三步 添加加解密
1.選擇Project | Add Reference菜單命令,添加對如下程序集的引用,它默認的安裝位置應該在C:\Program Files\Microsoft Enterprise Library January 2006\bin目錄下。
Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.dll
2.打開Chat.cs文件,添加如下命名空間:
using Microsoft.Practices.EnterpriseLibrary.Security.Cryptography;3.在Chat類中添加如下代碼:
public partial class Chat : Form
{
// TODO: Configuration symmetric algorithm provider name
private const string symmProvider = "ChatProvider";
// 


}
4.修改SendMessage方法,使用Cryptographer加密消息。
private void SendMessage(string message)
{
// TODO: Encrypt message
string encrypted = Cryptographer.EncryptSymmetric(symmProvider, message);

// Fire SendingMessage Event
if (this.SendingMessage != null)
this.SendingMessage(new MessageEventArgs(this._name, encrypted));
}5.修改MessageReceived方法,使用Cryptographer解密消息。
private void MessageReceived(MessageEventArgs args)
{
string message = args.Message;
// TODO: Decrypt message
string plainText = Cryptographer.DecryptSymmetric(symmProvider, message);
this.txtMessages.AppendText(
args.Sender + " says: " + plainText + Environment.NewLine);
}
第四步 企業(yè)庫配置工具
1.在項目CustomerManagement中添加一個應用程序配置文件(App.config),單擊CustomerManagement項目,選擇Project| Add New Item…菜單命令,在彈出的對話框中選擇Application configuration file,保留名稱為App.config。

2.使用Enterprise Library配置工具配置應用程序,可以通過開始菜單打開該配置工具,選擇所有程序| Microsoft patterns and practices | Enterprise Library | Enterprise Library Configuration,并打開App.config文件。或者直接在Visual Studio中使用該工具打開配置文件。
3.在解決方案管理器中選中App.config文件,在View菜單或者在右鍵菜單中選擇Open With…,將打開OpenWith對話框,單擊Add按鈕。

4.在Add Program對話框中,設(shè)置Program name指向EntLibConfig.exe文件,默認的路徑為C:\Program Files\Microsoft Enterprise Library January 2006\bin,設(shè)置Friendly name為Enterprise Library Configuration,單擊OK按鈕。

Visual Studio會把配置文件(App.config)作為一個命令行參數(shù)傳遞給EntLibConfig.exe。
5.在Open With對話框中,選中Enterprise Library Configuration并單擊OK按鈕。

第五步 配置應用程序使用對稱密鑰加密
1.在應用程序上點右鍵選擇New | Cryptography Application Block。

2.選中Cryptography Application Block | Symmetric Providers節(jié)點,選擇Action | New | Symmetric Algorithm Provider菜單命令。

3.將會顯示出Type Selector對話框,選擇RijndaelManaged并單擊OK按鈕。

4.密鑰向?qū)_始,選擇Create a new key選擇,并單擊Next按鈕。

通過該向?qū)?chuàng)建一個密鑰。
5.單擊Generate按鈕生成一個新的密鑰,并單擊Next按鈕。

6.單擊Ellipsis并選擇密鑰文件存放位置,在該實驗中,文件將保存在Windows桌面。

注意密鑰將不再保存在配置文件中,每一個密鑰都使用DPAPI保護保存在一個單獨的文件中。
7.選擇User mode或者Machine mode,并單擊Finish按鈕。

當創(chuàng)建一個密鑰的時候,需要選擇是用戶模式或者機器模式來限制訪問密鑰文件的權(quán)限。在下列情形下適用機器模式:
應用程序運行在專有的服務(wù)器上,再沒有別的應用程序運行。
有多個應用程序運行在相同的服務(wù)器上,想在這些應用程序之間共享這些敏感信息。
8.選中Cryptography Application Block | Symmetric Providers | RijndaelManaged節(jié)點,并設(shè)置如下屬性Name = ChatProvider。

9.保存對應用程序的配置。
第五步 運行應用程序
1.選擇Debug | Start Without Debugging菜單命令,運行應用程序。
在Sam和Toby之間傳遞消息,可以看到,在傳遞過程中消息是加密的,注意觀察控制臺窗口,在接收到消息后是解密的。

2.關(guān)閉應用程序。
第六步 添加錯誤處理
在Chat.cs文件中的SendMessage方法添加如下代碼。
private void SendMessage(string message)
{
if ((message != null) && (message.Trim().Length > 0))
{
// TODO: Encrypt message
string encrypted = Cryptographer.EncryptSymmetric(symmProvider, message);
// Fire SendingMessage Event
if (this.SendingMessage != null)
this.SendingMessage(new MessageEventArgs(this._name, encrypted));
}
}
更多Enterprise Library的文章請參考《Enterprise Library系列文章》
Worktile,新一代簡單好用、體驗極致的團隊協(xié)同、項目管理工具,讓你和你的團隊隨時隨地一起工作。完全免費,現(xiàn)在就去了解一下吧。
https://worktile.com


浙公網(wǎng)安備 33010602011771號