Microsoft Enterprise Library 5.0 系列教程(二) Cryptography Application Block (初级)


企业库加密应用程序模块提供了2种方式让用户保护自己的数据:

  1. Hashingproviders:  离散加密法, 简单来说就是把你的信息保存到内存中后用一个离散值表示并返回给程序,这样在程序中只能看到离散值而不是明文,这样就起到简单的加密效果啦.
  2. Cryptographyproviders: **加密法. 用对称加密方法对数据进行加密(尚未支持非对称加密).

使用企业库加密应用程序模块的优势:

  1. 减少了需要编写的模板代码,执行标准的任务,可以用它来解决常见的应用程序加密的问题.
  2. 有助于维持一个应用程序内和跨企业的数据传输加密.
  3. 允许管理员进行加密配置,包括使用组策略.
  4. 可扩展,支持用户自定义加密技术.

下面介绍如何使用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 (初级)代码

  (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) 测试:

Microsoft Enterprise Library 5.0 系列教程(二) Cryptography Application Block (初级)
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 (初级)

运行结果:

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 (初级)代码

  (3)     测试:

Microsoft Enterprise Library 5.0 系列教程(二) Cryptography Application Block (初级)
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);
}
}
}
Microsoft Enterprise Library 5.0 系列教程(二) Cryptography Application Block (初级)

运行结果:

 Microsoft Enterprise Library 5.0 系列教程(二) Cryptography Application Block (初级)




本文转自黄聪博客园博客,原文链接:http://www.cnblogs.com/huangcong/archive/2010/05/28/1746634.html,如需转载请自行联系原作者