微软企业库5.0学习-Security.Cryptography模块

一、微软企业库加密应用模块提供了两种加密:

1、Hash providers :离散加密,即数据加密后无法解密

2、Symmetric Cryptography Providers:密钥(对称)加密法,即数据加密后可解密还原
注:企业库不支持非对称加密方式。

二、使用说明

1.使用静态类Cryptographer

using Microsoft.Practices.EnterpriseLibrary.Security.Cryptography;

//Hash加密
string encrypted = Cryptographer.CreateHash("MD5CryptoServiceProvider", "plainText");
//对称加密
string encrypted = Cryptographer.EncryptSymmetric("RC2CryptoServiceProvider", "plainText");
2.使用企业库容器EnterpriseLibraryContainer获取CryptographyManager抽象类实现实例
using Microsoft.Practices.EnterpriseLibrary.Security.Cryptography;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.ServiceLocation;
//Hash加密
CryptographyManager crypt = EnterpriseLibraryContainer.Current.GetInstance<CryptographyManager>();
string encrypted = crypt.CreateHash("MD5CryptoServiceProvider", "plainText");

三、设计视图

四、实现IHashProvider或ISymmetricCryptoProvider接口的自定义加、解密方法

参考企业库帮助手册 加密模块部分的Extending and Modifying the Cryptography Application Block节

注意:

1.须增加类属性[Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationElementType(typeof(CustomHashProviderData))],配置工具方可识别自定义加解密类

2.必须存在一带参System.Collections.Specialized.NameValueCollection attributes的构造函数,参数可从配置文件中获取指定的配置属性,如下

public MyHashProvider(NameValueCollection attributes)
{
}

 

参考MSDN The Cryptography Application Block

原文地址:https://www.cnblogs.com/shijun/p/3369270.html