微软企业库5.0 心得
微软企业库是开源的,它包含一系列类库和可重用的组件用来帮助通用的企业级开发。企业库能帮助你解决一些各个项目中的共同的问题。这些功能一般的系统都需要用的到,例如:日志功能,数据校验功能,缓存功能,异常处理功能,加密功能等等。
企业库应用分为三个简单的步骤:
- 下载微软企业库的类库
类库下载地址:http://www.microsoft.com/download/en/details.aspx?id=15104
选择第二项
- 用配置工具配置企业库,自动生成配置文件
解压缩类库文件,在目录EnterpriseLibrary5\EntLib50Src2\bin下找到EntLibConfig.exe文件
- 在代码中应用企业库提供的类
下面我先来介绍一下企业库其中的几个功能。
加密功能
数据加密的基本过程就是对原来为明文的文件或数据按某种算法进行处理,使其成为不可读的一段代码,通常称为“密文”,使其只能在输入相应的密钥之后才能显示出本来内容,通过这样的途径来达到保护数据不被非法人窃取、阅读的目的。 该过程的逆过程为解密,即将该编码信息转化为其原来数据的过程。
加密技术通常分为两大类:“对称式”和“非对称式”。
对称式加密就是加密和解密使用同一个密钥,通常称之为“Session Key ”这种加密技术目前被广泛采用,如美国政府所采用的DES加密标准就是一种典型的“对称式”加密法,它的Session Key长度为56Bits。
非对称式加密就是加密和解密所使用的不是同一个密钥,通常有两个密钥,称为“公钥” 和“私钥”,它们两个必需配对使用,否则不能打开加密文件。这里的“公钥”是指可以对外公布的,“私钥”则不能,只能由持有人一个人知道。它的优越性就在 这里,因为对称式的加密方法如果是在网络上传输加密文件就很难把密钥告诉对方,不管用什么方法都有可能被别窃听到。而非对称式的加密方法有两个密钥,且其 中的“公钥”是可以公开的,也就不怕别人知道,收件人解密时只要用自己的私钥即可以,这样就很好地避免了密钥的传输安全性问题。
企业库加密应用程序模块提供了2种方式让用户保护自己的数据:
- Hashingproviders: 离散加密法, 简单来说就是把你的信息保存到内存中后用一个离散值表示并返回给程序,这样在程序中只能看到离散值而不是明文,这样就起到简单的加密效果。
- SyMMetric Cryptographyproviders: 密钥加密法. 用对称加密方法对数据进行加密(尚未支持非对称加密).
下面介绍Hashingproviders 中的RC2CryptoServiceProvider离散加密法:
- 打开EntLibConfig.exe
- 选择Blocks菜单 ,单击 Add CryptographySettings .
- 下面创建Hash Providers
点击Hash Providers旁边的加号选择Add Hash Providers,选择Add Hash Algorithm Provider
4.在弹出的对话框中选择一个加密算法mscorlibàSysstem.Sercurity.CryptographyàMD5CryptoServiceProvider
- 点击 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=null" requirePermission="true" />
</configSections>
<securityCryptographyConfiguration>
<hashProviders>
<add name="MD5CryptoServiceProvider" type="Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.HashAlgorithmProvider, Microsoft.Practices.EnterpriseLibrary.Security.Cryptography, Version=5.0.414.0, Culture=neutral, PublicKeyToken=null"
algorithmType="System.Security.Cryptography.MD5CryptoServiceProvider, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
saltEnabled="true" />
</hashProviders>
</securityCryptographyConfiguration>
</configuration>
- 要使用缓存应用程序模块, 需要导入相应的Dll文件,在此我们要导入的是EnterpriseLibrary5\EntLib50Src2\bin 文件夹下的Microsoft.Practices.EnterpriseLibrary.Caching.dll ,将App.config文件中的内容更新到项目中,并添加usingMicrosoft.Practices.EnterpriseLibrary.Security.Cryptography引用:
源代码如下:
static void Main(string[] args)
{
//加密后把加密字符串赋给Encrypt
//"RC2CryptoServiceProvider"类为该加密算法的实例
string Encrypt = Cryptographer.EncryptSymmetric("RC2CryptoServiceProvider", "需要加密的字符串");
Console.WriteLine("密文:" + Encrypt);
Console.WriteLine("------------------------------------------------");
//解密后把源字符串赋给Encrypt
Encrypt = Cryptographer.DecryptSymmetric("RC2CryptoServiceProvider", Encrypt);
Console.WriteLine("原文:" + Encrypt);
Console.ReadLine();
}
运行结果:
下面介绍Hashingproviders 中的HMACMD5离散加密法:
1.下面创建Hash Providers
点击Hash Providers旁边的加号选择Add Hash Providers,选择Add Hash Algorithm Provider
2. 在弹出的对话框中选择一个加密算法mscorlibàSysstem.Sercurity.Cryptographyà HMACMD5
- 点击确定在弹出的对话框中选择Create a new key
新建一个key文件
4.点击netx ,再点击Generate选择生成一个密钥
5.点击next,选择保存key的目录和文件名
6. 接着是选择模式,有User模式和Machine模式:
(1)User模式:每个应用程序有自己的唯一标识,无法访问其他应用程序的资源.
(2)Machine模式:加密的文件只能使用在本电脑上使用,也就是说用这个模式,在其他电脑你还需要重新生成一个Key文件.
我们就选择User模式
- 点击finish, 点击 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=null" requirePermission="true" />
</configSections>
<securityCryptographyConfiguration>
<hashProviders>
<add name="HMACMD5" type="Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.KeyedHashAlgorithmProvider, Microsoft.Practices.EnterpriseLibrary.Security.Cryptography, Version=5.0.414.0, Culture=neutral, PublicKeyToken=null"
protectedKeyFilename="C:\Documents and Settings\Administrator\桌面\key.key"
protectedKeyProtectionScope="CurrentUser" algorithmType="System.Security.Cryptography.HMACMD5, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
saltEnabled="true" />
</hashProviders>
</securityCryptographyConfiguration>
</configuration>
- 同上,在此我们要导入的是EnterpriseLibrary5\EntLib50Src2\bin 文件夹下的Microsoft.Practices.EnterpriseLibrary.Caching.dll ,将App.config文件中的内容更新到项目中,并添加usingMicrosoft.Practices.EnterpriseLibrary.Security.Cryptography引用:
2.HashCryptographer(离散加密),这种方法根据特定的算法对数据进行加密,此种加密无法被解密,只能和原来的源字符串比较,判断是否一致。
源代码入下:
static void Main(string[] args)
{
//加密后把加密字符串赋给Encrypt
//"HMACMD5"类为该加密算法的实例
string Encrypt = Cryptographer.CreateHash("HMACMD5", "需要加密的字符串");
Console.WriteLine("密文:" + Encrypt);
Console.WriteLine("------------------------------------------------");
//判断是否和源字符串匹配
Boolean isEqual = Cryptographer.CompareHash("HMACMD5", "需要加密的字符串",Encrypt);
if (isEqual)
{
Console.WriteLine("匹配成功!");
}
else
{
Console.WriteLine("匹配失败!");
}
Console.ReadLine();
}
- 把key文件删除后,程序出现异常: