一、微软企业库加密应用模块提供了两种加密:
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");
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
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)
{
}