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.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.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.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.gif
{
InBlock.gif    
if ((message != null&& (message.Trim().Length > 0))
InBlock.gif
ExpandedSubBlockStart.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系列文章












本文转自lihuijun51CTO博客,原文链接:  http://blog.51cto.com/terrylee/67645 ,如需转载请自行联系原作者


相关文章
|
3月前
|
移动开发 JavaScript 安全
Vue 应用程序性能优化:代码压缩、加密和混淆配置详解
Vue 应用程序性能优化:代码压缩、加密和混淆配置详解
38 0
|
4月前
|
JavaScript 安全 开发工具
​Vue 应用程序性能优化:代码压缩、加密和混淆配置详解
简介在 Vue 应用程序的开发中,代码压缩、加密和混淆是优化应用程序性能和提高安全性的重要步骤。 Vue CLI 是一个功能强大的开发工具,它提供了方便的配置选项来实现这些功能。本文将介绍如何使用 Vue CLI 配置代码压缩、加密和混淆功能,以提高应用程序的性能和安全性。
|
5月前
|
开发工具 数据安全/隐私保护 git
【Devchat 插件】创建一个GUI应用程序,使用Python进行加密和解密
【Devchat 插件】创建一个GUI应用程序,使用Python进行加密和解密
76 0
|
11月前
|
Go 数据安全/隐私保护
Go实现aes加密,并带你手撸一个命令行应用程序
Go实现aes加密,并带你手撸一个命令行应用程序
129 0
|
Java 数据安全/隐私保护
Java实现最电话号码的简单加密源码
Java实现最电话号码的简单加密源码
17 0
|
2月前
|
存储 安全 算法
【接口加密】Java中的接口加密实践
【接口加密】Java中的接口加密实践
|
2月前
|
算法 安全 Java
Java 实现 RSA 非对称加密算法-加解密和签名验签
Java 实现 RSA 非对称加密算法-加解密和签名验签
|
Java 数据安全/隐私保护
java实现加密电话号码,有具体的加密流程注释
java实现加密电话号码,有具体的加密流程注释
19 0