黄聪:Microsoft Enterprise Library 5.0 系列教程(二) Cryptography Application Block (高级)
原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(二) Cryptography Application Block (高级)
本章介绍的是企业库加密应用程序模块Cryptographyproviders中为对称加密配置Key文件的3种方式:
- create a new key : 使用一串字符串作为Key,然后通过加密保存到一个Key文件中.
- use an existing DPAPI-protected key file : 使用一个现有的Key文件进行配置.
- import a password-protected key file : 对导出的Key文件再进行一次Password加密.
废话少说,现在就开始看看如何使用它们吧:
一. Create a new key
1.运行EntLibConfig.exe,选择Blocks菜单 ,单击 Add CryptographySettings .
2.点击symmetriccryptography provider 区块右上角的加号按钮,然后点击 Add Symmetric Cryptography Providers,在弹出的加密算法中任意选择一个:
3.在弹出的Key文件生成方案中,我们选择第一个Create a new key,点击Next:
4.下一步是要你输入Key码,你可以自己手动输入一串自己定制好的Key码,也可以点击右下角的Generate按钮,让计算机为你自动生成一串Key码,在此我是直接点击Generate按钮生成的Key码,然后点击Next:
5.接着是选择Key文件导出的目录,这里我先保存成桌面的test.key文件,设置好后点击Next:
6.接着是选择模式,有User模式和Machine模式:
()User模式:每个应用程序有自己的唯一标识,无法访问其他应用程序的资源.
()Machine模式:加密的文件只能使用在本电脑上使用,也就是说用这个模式,在其他电脑你还需要重新生成一个Key文件.
在本地调试哪个模式都无所谓,我们就选择User模式吧:
7.再点击Finish,就完成配置啦:
桌面导出的Key文件:
8.点击 File 菜单,单击 Save,保存为一个App.config文件,可以先保存到桌面,之后要用到它. 用记事本打开App.config,可以看到如下内容:
<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>
<symmetricCryptoProviders>
<add name="RC2CryptoServiceProvider" type="Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.SymmetricAlgorithmProvider,Microsoft.Practices.EnterpriseLibrary.Security.Cryptography, Version=5.0.414.0,Culture=neutral, PublicKeyToken=31bf3856ad364e35"
algorithmType="System.Security.Cryptography.RC2CryptoServiceProvider,mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
protectedKeyFilename="C:\Users\Administrator\Desktop\test.key"
protectedKeyProtectionScope="CurrentUser"/>
</symmetricCryptoProviders>
</securityCryptographyConfiguration>
</configuration>
9.要使用缓存应用程序模块, 需要导入相应的Dll文件,在此我们要导入的是Microsoft.Practices.EnterpriseLibrary.Caching.dll ,将App.config文件添加到项目中,并添加Microsoft.Practices.EnterpriseLibrary.Security.Cryptography引用:
添加引用:
using Microsoft.Practices.EnterpriseLibrary.Security.Cryptography;
10.测试:
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 Encrypt = Cryptographer.EncryptSymmetric("RC2CryptoServiceProvider", "HuangCong"); Console.WriteLine("密文:"+ Encrypt); Console.WriteLine("------------------------------------------------"); Encrypt = Cryptographer.DecryptSymmetric("RC2CryptoServiceProvider", Encrypt); Console.WriteLine("原文:"+ Encrypt); } }}
11.运行结果:
二. use an existingDPAPI-protected key file:
- 为了不混淆之前的实验,我们先将原来的策略删除,恢复到最初的状态:
- 点击symmetric cryptography provider 区块右上角的加号按钮,然后点击 Add Symmetric Cryptography Providers,在弹出的加密算法中我们需要选择和之前一样的加密方法,因为你之前导出的Key文件只针对该算法而导出的:
- 又到了Key文件生成方案中,我们选择第二个Use an existing DPAPI-protected key file,点击Next:
- 接着就会要我们选择一个已经存在的Key文件作为本策略的Key文件,我们就选择之前刚刚创建好的test.key文件吧:
- 接着是选择模式,以前讲过,就不重复了,点击Finish完成配置.
6.点击 File 菜单,单击 Save更新原有的App.config文件,打开可看到以下内容.
<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>
<symmetricCryptoProviders>
<add name="RC2CryptoServiceProvider" type="Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.SymmetricAlgorithmProvider,Microsoft.Practices.EnterpriseLibrary.Security.Cryptography, Version=5.0.414.0,Culture=neutral, PublicKeyToken=31bf3856ad364e35"
algorithmType="System.Security.Cryptography.RC2CryptoServiceProvider,mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
protectedKeyFilename="C:\Users\Administrator\Desktop\test.key"
protectedKeyProtectionScope="CurrentUser"/>
</symmetricCryptoProviders>
</securityCryptographyConfiguration>
</configuration>
- 测试:
- 运行结果:
三. Import a password-protected key file
- 为了完成该实验,我们要先导出一个用Password加密过的key文件,则我们可以在刚才的策略工具栏上右键,选择Export Key:
- 接着设置的是对Key文件加密的密码和密码保存文件存放目录,在此我设置的密码是123456,存放目录为桌面的test.txt文件:
- 点击OK,就可以在桌面看到导出的密码保存文件啦:
- 为了不混淆之前的实验,我们先将原来的策略删除,恢复到最初的状态:
- 点击symmetric cryptography provider 区块右上角的加号按钮,然后点击 Add Symmetric Cryptography Providers,在弹出的加密算法中任意选择一个,为了做区分,我们仍然要选择原来的加密方法:
- Key文件生成方案中,我们选择第三个Import a password-protected key file,点击Next:
- 接着程序要你提供密码保存文件和密码,在这里我们可以导入桌面的test.txt文件,密码输入123456,接着点击Next:
- 接着选择新导出的key文件的存放目录,这里我们保存它成桌面的test1.key文件,点击Next:
- 接着是选择模式,以前讲过,就不重复了,点击Finish完成配置.
10. 点击 File 菜单,单击 Save更新原有的App.config文件,打开可看到以下内容:
<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>
<symmetricCryptoProviders>
<add name="RC2CryptoServiceProvider" type="Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.SymmetricAlgorithmProvider,Microsoft.Practices.EnterpriseLibrary.Security.Cryptography, Version=5.0.414.0,Culture=neutral, PublicKeyToken=31bf3856ad364e35"
algorithmType="System.Security.Cryptography.RC2CryptoServiceProvider,mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
protectedKeyFilename="C:\Users\Administrator\Desktop\test1.key"
protectedKeyProtectionScope="CurrentUser"/>
</symmetricCryptoProviders>
</securityCryptographyConfiguration>
</configuration>
- 测试:
- 运行结果:
黄聪:Microsoft Enterprise Library 5.0 系列教程(二) Cryptography Application Block (高级)的更多相关文章
- 黄聪: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 (高级) 企业库验证应用程序模块之配置文件模式: ...
- 黄聪:Microsoft Enterprise Library 5.0 系列教程(一) Caching Application Block (高级)
原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(一) Caching Application Block (高级) Caching Application Bl ...
- 转:Microsoft Enterprise Library 5.0 系列教程(一) Caching Application Block (高级)
http://www.360doc.com/content/13/0918/22/15643_315482318.shtml http://www.360doc.com/content/13/0918 ...
- 黄聪:Microsoft Enterprise Library 5.0 系列教程(六) Security Application Block
原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(六) Security Application Block 开发人员经常编写需要安全功能的应用程序.这些应用程序 ...
- 黄聪:Microsoft Enterprise Library 5.0 系列教程(四) Logging Application Block
原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(四) Logging Application Block 企业库日志应用程序模块工作原理图: 从上图我们可以 ...
- 黄聪:Microsoft Enterprise Library 5.0 系列教程(三) Validation Application Block (初级)
原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(三) Validation Application Block (初级) 企业库提供了一个很强大的验证应用程序模 ...
- 黄聪:Microsoft Enterprise Library 5.0 系列教程(一) : Caching Application Block (初级)
原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(一) : Caching Application Block (初级) 本篇文章具体官方解释请参照以下链接: h ...
- 黄聪:Microsoft Enterprise Library 5.0 系列教程(十) Configuration Application Block
原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(十) Configuration Application Block 到目前为止,我们使用的模块都是在同一个配置 ...
随机推荐
- Android使用SharedPreferences保存数组
核心原理: 对象序列化 步骤 1.要保存的对象实现序列化Serializable 2.将序列化的对象保存String(本文的做法是保存为byte数组在转为16进制的String类型保存起来) 3.将保 ...
- TsFltMgr.sys其原因是,该系统蓝屏QQ计算机管理器!
同事一WindowsXP系统,正常执行,关闭后,第二天无法启动.详细症状为: (1)安全模式以及带网络功能的安全模式都能够进入. (2)正常模式.还没出现WindowXP滚动栏就開始重新启动: (3) ...
- http2.0笔记
二进制分帧层 定义了如何封装 HTTP 消息并在客户端与服务器之间传输 http2.0的消息传输特点: 流 已建立的连接上的双向字节流 消息 与逻辑消息对应的完整的一系列数据帧 帧 http2.0通信 ...
- ubuntu10.10 tftp安装,配置,测试
ubuntu10.10 tftp安装,配置,测试 成于坚持,败于止步 虽然ubuntu/centos/redhat都是linux,但是内核其中存在一定的修改,所以对于tftp服务器的安装存在不同的命令 ...
- CloseHandle(),TerminateThread(),ExitThread()的差别
线程的handle用处: 线程的handle是指向"线程的内核对象"的,而不是指向线程本身.每一个内核对象仅仅是内核分配的一个内存块,而且仅仅能由内核訪问.该内存块是一种数据结构, ...
- inline与lnk2001、lnk2019,鸡肋?
inline函数居然出现了lnk2001.lnk2019,先贴代码. a.h #pragma once class A { public: inline void foo(); void us ...
- [C#基础] 类
类成员 字段和方法是最重要的类成员类型,字段是数据成员,方法是函数成员 字段 字段是隶属于类的变量 它可以是任何类型,无论是预定义类型还是用户定义类型 和所有变量一样,字段用来保存数据 它们可以被写入 ...
- ie浏览器提交参数和其它浏览器的区别
场景描述: 用户注册模块(ajax提交方式,post方法),在url后追加了一个参数,如:url+‘btnvalue=中文参数’,如此在非ie浏览器注册时,功能完好,但在ie下注册不成功.调式后发现在 ...
- sprintf,多少钱你知道?
选<CSDN 社区电子杂志——C/C++杂志>http://emag.csdn.net 2005 年1 月 总号1 期 - 93 -笔者:steedhorse(晨星)printf 可能是很 ...
- DS Scheduler 0.7 发布,Linux 调度系统 - 开源中国社区
DS Scheduler 0.7 发布,Linux 调度系统 - 开源中国社区 DS Scheduler 0.7 发布,Linux 调度系统