Enterprise Library 2.0 Hands On Lab 翻译(14):加密应用程序块(一)

练习1:加解密字符串

通过本练习将学习通过加密来保护信息,在这里创建一个类似于IM的聊天应用程序,加密通信过程中的信息。

 

第一步

BugSmak.sln项目,默认的安装路径应该为C:\Program Files\Microsoft Enterprise Library January 2006\labs\cs\Cryptography\exercises\ex01\begin,并编译。

 

第二步 回顾应用程序

1.在解决方案管理器选中Chat.cs文件,选择View | Code菜单命令。Chat窗体用来接收和发送信息,上面的灰色TextBox用来显示聊天信息,底部白色的TextBox用来发送新的消息。

2.选择Debug | Start Without Debugging命令运行应用程序,聊天窗口将被打开,分别叫做SamToby,消息可以在这两个窗口之间传递,在Toby的消息文本框中输入一些字符,并单击Send按钮,在Sam窗体中作重复做一次。可以看到交流信息显示在了聊天窗体中。还有一个控制台应用程序显示,它用来监视聊天的过程,所有的消息都将在这里显示。

3.关闭所有窗体并关闭应用程序。

 

第三步 添加加解密

1.选择Project | Add Reference菜单命令,添加对如下程序集的引用,它默认的安装位置应该在C:\Program Files\Microsoft Enterprise Library January 2006\bin目录下。

Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.dll

2.打开Chat.cs文件,添加如下命名空间:

None.gif using  Microsoft.Practices.EnterpriseLibrary.Security.Cryptography;

3.在Chat类中添加如下代码:

None.gif public  partial  class  Chat : Form
None.gif
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
InBlock.gif
// TODO: Configuration symmetric algorithm provider name
InBlock.gif

InBlock.gif
private const string symmProvider = "ChatProvider";
InBlock.gif
InBlock.gif
// dot.gifdot.gif
InBlock.gif

ExpandedBlockEnd.gif}

None.gif

4.修改SendMessage方法,使用Cryptographer加密消息。

None.gif private   void  SendMessage( string  message)
None.gif
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
InBlock.gif    
// TODO: Encrypt message
InBlock.gif

InBlock.gif    
string encrypted = Cryptographer.EncryptSymmetric(symmProvider, message);
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif    
// Fire SendingMessage Event
InBlock.gif

InBlock.gif    
if (this.SendingMessage != null)
InBlock.gif
InBlock.gif        
this.SendingMessage(new MessageEventArgs(this._name, encrypted));
InBlock.gif
ExpandedBlockEnd.gif}

5.修改MessageReceived方法,使用Cryptographer解密消息。

None.gif private   void  MessageReceived(MessageEventArgs args)
None.gif
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
InBlock.gif    
string message = args.Message;
InBlock.gif
InBlock.gif    
// TODO: Decrypt message
InBlock.gif

InBlock.gif    
string plainText = Cryptographer.DecryptSymmetric(symmProvider, message);
InBlock.gif
InBlock.gif    
this.txtMessages.AppendText(
InBlock.gif
InBlock.gif        args.Sender 
+ " says: " + plainText + Environment.NewLine);
InBlock.gif
ExpandedBlockEnd.gif}

 

第四步 企业库配置工具

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对话框中,设置Program name指向EntLibConfig.exe文件,默认的路径为C:\Program Files\Microsoft Enterprise Library January 2006\bin,设置Friendly nameEnterprise Library Configuration,单击OK按钮。

Visual Studio会把配置文件(App.config)作为一个命令行参数传递给EntLibConfig.exe

5.在Open With对话框中,选中Enterprise Library Configuration并单击OK按钮。

 

第五步 配置应用程序使用对称密钥加密

1.在应用程序上点右键选择New | Cryptography Application Block

2.选中Cryptography Application Block | Symmetric Providers节点,选择Action | New | Symmetric Algorithm Provider菜单命令。

3.将会显示出Type Selector对话框,选择RijndaelManaged并单击OK按钮。

4.密钥向导将会开始,选择Create a new key选择,并单击Next按钮。

通过该向导将创建一个密钥。

5.单击Generate按钮生成一个新的密钥,并单击Next按钮。

6.单击Ellipsis并选择密钥文件存放位置,在该实验中,文件将保存在Windows桌面。

注意密钥将不再保存在配置文件中,每一个密钥都使用DPAPI保护保存在一个单独的文件中。

7.选择User mode或者Machine mode,并单击Finish按钮。

当创建一个密钥的时候,需要选择是用户模式或者机器模式来限制访问密钥文件的权限。在下列情形下适用机器模式:

应用程序运行在专有的服务器上,再没有别的应用程序运行。

有多个应用程序运行在相同的服务器上,想在这些应用程序之间共享这些敏感信息。

8.选中Cryptography Application Block | Symmetric Providers | RijndaelManaged节点,并设置如下属性Name = ChatProvider

9.保存对应用程序的配置。

 

第五步 运行应用程序

1.选择Debug | Start Without Debugging菜单命令,运行应用程序。

SamToby之间传递消息,可以看到,在传递过程中消息是加密的,注意观察控制台窗口,在接收到消息后是解密的。

2.关闭应用程序。

 

第六步 添加错误处理

Chat.cs文件中的SendMessage方法添加如下代码。

None.gif private   void  SendMessage( string  message)
None.gif
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
if ((message != null&& (message.Trim().Length > 0))
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
// TODO: Encrypt message
InBlock.gif

InBlock.gif        
string encrypted = Cryptographer.EncryptSymmetric(symmProvider, message);
InBlock.gif
InBlock.gif        
// Fire SendingMessage Event
InBlock.gif

InBlock.gif        
if (this.SendingMessage != null)
InBlock.gif
InBlock.gif            
this.SendingMessage(new MessageEventArgs(this._name, encrypted));
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedBlockEnd.gif}

 

更多Enterprise Library的文章请参考《Enterprise Library系列文章

转载于:https://www.cnblogs.com/Terrylee/archive/2006/10/16/Cryptography_Application_Block_HandsOnLab_Part1.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值