原文:黄聪: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

2. 选择Blocks菜单 ,单击 Add CryptographySettings .

下面分别样式如何创建Hash Providers 和 Symmetric CryptographyProviders 加密策略:

(A)   Hash Providers 策略使用步骤:

  (1)    点击HashProviders 区块右上角的加号按钮, Add Hash Providers, 然后点击Add Hash Algorithm Provider,在弹出的对话框中选择System.Core下的MD5Cng,

      表示我们要用MD5的加密方法获取离散值.

  (2) 点击 File 菜单,单击 Save,保存为一个App.config文件,可以先保存到桌面,之后要用到它. 用记事本打开App.config,可以看到如下内容.

代码

<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引用:

  添加引用:

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("错误");            }        }    }}

运行结果:

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

  

  (2)    点击 File 菜单,单击 Save更新原有的App.config文件,打开可看到以下内容.

代码

<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 defaultHashInstance="MD5Cng">

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

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

</securityCryptographyConfiguration>

</configuration>

  (3)     测试:

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 (初级)的更多相关文章

  1. 黄聪:Microsoft Enterprise Library 5.0 系列教程(二) Cryptography Application Block (高级)

    原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(二) Cryptography Application Block (高级) 本章介绍的是企业库加密应用程序模块 ...

  2. 黄聪:Microsoft Enterprise Library 5.0 系列教程(三) Validation Application Block (初级)

    原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(三) Validation Application Block (初级) 企业库提供了一个很强大的验证应用程序模 ...

  3. 黄聪:Microsoft Enterprise Library 5.0 系列教程(一) : Caching Application Block (初级)

    原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(一) : Caching Application Block (初级) 本篇文章具体官方解释请参照以下链接: h ...

  4. 黄聪:Microsoft Enterprise Library 5.0 系列教程(六) Security Application Block

    原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(六) Security Application Block 开发人员经常编写需要安全功能的应用程序.这些应用程序 ...

  5. 黄聪:Microsoft Enterprise Library 5.0 系列教程(四) Logging Application Block

    原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(四) Logging Application Block 企业库日志应用程序模块工作原理图:   从上图我们可以 ...

  6. 黄聪:Microsoft Enterprise Library 5.0 系列教程(三) Validation Application Block (高级)

    原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(三) Validation Application Block (高级) 企业库验证应用程序模块之配置文件模式: ...

  7. 黄聪:Microsoft Enterprise Library 5.0 系列教程(一) Caching Application Block (高级)

    原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(一) Caching Application Block (高级) Caching Application Bl ...

  8. 黄聪:Microsoft Enterprise Library 5.0 系列教程(十) Configuration Application Block

    原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(十) Configuration Application Block 到目前为止,我们使用的模块都是在同一个配置 ...

  9. Microsoft Enterprise Library 5.0 系列教程(四) Logging Application Block

    Download dll: http://www.microsoft.com/en-us/download/confirmation.aspx?id=15104 http://www.cnblogs. ...

随机推荐

  1. CSS中常见的BUG调试

    1.布局--layout 布局是windows提出的概念,用于控制元素的尺寸和定位. 拥有布局的元素负责自身及其子元素的尺寸及定位,而没有布局的元素仅仅能依靠近期的祖先元素进行控制. 通常在IE6中出 ...

  2. 经常使用MD5算法代码

    经常使用的MD5算法代码日期: 2014年8月4日作者: 铁锚 MD5,全称为 Message Digest Algorithm 5(消息摘要算法第五版).详情请參考 维基百科:MD5  MD5加密后 ...

  3. Opencv笔记(1) 命名规则数据结构(CvMat,...)

    网上搜索了很多,检查中发现的信息劣势,检查源代码 同Cv为类的开始.包含详细的数据不(仅存储指针) CvMat typedef struct CvMat { int type; int step; / ...

  4. 基于raw os 的事件触发系统

    Raw os的事件触发系统有以下特点: 1 基于UML的状态机理念设计,实现了有限状态机(fsm)以及层次状态机(HSM). 2 实现了活动对象(ACTIVE OBJECT)的特性,一个活动对象包含了 ...

  5. SQL视图和多表连接

    本篇博客关注的焦点是视图的使用以及视图和多表连接的配合.以便可以了解视图,以及更好的使用视图. 首先,还是要说明一下视图的定义:视图是基于SQL语句的结果集的可视化虚拟表,换句话说视图就是SQL查询结 ...

  6. Unity3d 4.3.4f1执行项目

    今天.本来执行的非常快的一个项目. 忽然打开非常晚.尝试新建一个新的项目,竟然执行速度非常快. 心有不忿的,把整个Unity删除了.又一次安装. 再打开那个执行变慢的项目. 结果.执行速度回来了. 不 ...

  7. NET5

    ASP.NET5(RC1) - 翻译 前言 ASP.NET 5 是一次令人惊叹的对于ASP.NET的创新革命. 他将构建目标瞄准了 .NET Core CLR, 同时ASP.NET又是对于云服务进行优 ...

  8. Android 手势&amp;触摸事件 MotionEvent

    1.http://blog.csdn.net/omg_2012/article/details/7881443 这篇相当好啊 2.http://blog.csdn.net/android_tutor/ ...

  9. eclipse设置关联文件打开方式

    window->preferences: General->Editors->File Associations

  10. 事件总线帧---Otto

    我们如果这样一种业务场景.如今在做一款及时聊天应用,我们在聊天页面进行收发信息.同一时候也要实时更新前一页面的聊天记录,这时我们该怎样去实现?说说我曾经的实现策略.我使用的是广播接收器BroadCas ...