微软企业库4.1学习笔记(二十三)加解密模块3 示例代码

加密解密模块可以满足常用的对称加解密和hash功能要求。在应用中加入模块,需要下面的步骤:

  1)添加对模块的程序集引用。添加对程序集Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.dll的引用。

  2)添加对程序集Microsoft.Practices.ObjectBuilder2.dll和Microsoft.Practices.EnterpriseLibrary.Common.dll的引用。

  3)在需要模块功能的文件中引入命名空间

  using Microsoft.Practices.EnterpriseLibrary.Security.Cryptography;

  4)在代码中使用模块提供的功能

  典型的功能

  1、使用对称加密算法加密数据 

  1.1加密结果是string形式

代码

            
// Encrypt the Sensitive Data String
             string  encryptedContentBase64 = Cryptographer .EncryptSymmetric ( " symmProvider " ,"password " );
            
  

 

  1.2加密结果是bytep[]形式

  

 


             byte []valueToEncrypt = System.Text.Encoding .Unicode .GetBytes ( " passowrd " );
            
byte  []encryptedContents = Cryptographer .EncryptSymmetric ( " symmProvider " ,valueToEncrypt );
Array.Clear (valueToEncrypt ,0,valueToEncrypt .Length );

 

  

  1.3使用的时候有两点需要注意

  •   确保symmProvider是在配置中存在的对称加解密算法,配置了适当的算法。
  •   敏感数据应该及时从内存中清空。Array.Clear方法就是这个功能,在内存中保留敏感数据是很危险的。

  2、使用对称加密算法解密数据

  

  2.1解密字符串

复制代码
代码
// Encrypt the Sensitive Data String
             string  encryptedContentBase64 = Cryptographer .EncryptSymmetric ( " symmProvider ","S ensitiveData " );
            
            
// Decrypt the base64 encoded string
             string  readableString = string .Empty ;
            readableString 
= Cryptographer .DecryptSymmetric ( " symmProvider " ,encryptedContentBase64 );
        
复制代码

 

  

  

  2.2解密字符数组

复制代码
代码

            
byte []valueToEncrypt = System.Text.Encoding .Unicode .GetBytes ( " passowrd " );
            
byte  []encryptedContents = Cryptographer .EncryptSymmetric ( " summProvider " ,valueToEncrypt );
            
            
byte []decryptContents = Cryptographer .DecryptSymmetric ( " symmProvider " ,encryptedContentBase64 );
            
string  plainText = ( new  System.Text.UnicodeEncoding ()).GetString (decryptContents );
复制代码

   2.3需要注意的地方

  确保在配置文件中配置了正确的算法provider。

  3、获取数据的hash值

  3.1获取hash值

 

代码

            
byte  []valutHash = ( new  System.Text.UnicodeEncoding ()).GetBytes ( " password " );
            
byte []generatedHash = Cryptographer .CreateHash ( " hashProvider " ,valutHash );
            Array .Clear (generatedHash ,
0 ,generatedHash.Length );

 

  3.2注意的地方

  •   CreateHash方法有两个重载,区别就是方法的返回值一个是string,一个是byte[]。
  •   确保在配置中配置了相应的hash  provider
  •   及时清空敏感数据,在内存中保留敏感数据是很危险的。你应该知道,内存中的值可以被写回硬盘,因为操作系统会将数据写到交换文件中。如果系统崩溃,系统有可能将内存中的数据丢到硬盘上。

  4、检查hash值和文本是否匹配

   

 

复制代码
代码

            
byte  []valutHash = ( new  System.Text.UnicodeEncoding ()).GetBytes ( " password " );
            
byte []generatedHash = Cryptographer .CreateHash ( " hashProvider " ,valutHash );
            
            
byte  []stringToCompare = ( new  System.Text.UnicodeEncoding ()).GetBytes ( " TestValue " );
            
bool  comparisionSuccessed = Cryptographer .CompareHash ( " hashProvider " ,stringToCompare ,generatedHash );
复制代码

 

 

  需要注意的是,一定要确保配置了适当的hash provider。

 

  扩展和修改加解密模块

  一、创建一个自定义的hash 算法provider

  1、创建一个类

  2、子文件中添加引用

using Microsoft.Practices.EnterpriseLibrary.Security.Cryptography;
using Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.Configuration;

  3、让类实现IHashProvider接口

  4、添加ConfigurationElementType特性,添加CustomHashProviderData作为特性的参数。

  5、添加构造函数,参数是NameValueCollection类型

  6、实现接口的两个方法

  

 

复制代码
代码
using  System;
using  System.Collections.Generic;
using  Microsoft.Practices.EnterpriseLibrary.Common;
using  Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using  Microsoft.Practices.EnterpriseLibrary.Security.Cryptography;
using  Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.Configuration;

namespace  BeautyCode.ConApp
{
    [ConfigurationElementType (
typeof  (CustomHashProviderData ))]
    
public   class  MyHashProvider:IHashProvider 
    {
        
        
public  MyHashProvider (System.Collections.Specialized.NameValueCollection attributes)
        {
        }
        
public   byte [] CreateHash( byte [] plaintext)
        {
            
throw   new  NotImplementedException();
        }
        
        
public   bool  CompareHash( byte [] plaintext,  byte [] hashedtext)
        {
            
throw   new  NotImplementedException();
        }
    }
}
复制代码

 

 

  二、创建一个自定义的对称加解密算法

  2.1添加一个类文件

  2.2添加引用

using System;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Security.Cryptography;
using Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.Configuration;

  2.3实现接口ISymmetricCryptoProvider

  2.4添加ConfigurationElementType特性,参数是CustomSymmetricCryptoProviderData类型

  2.5添加构造函数,参数是NameValueCollection类型

  2.6实现接口的Encrypt和Decrypt方法

  

 

复制代码
代码
using  System;
using  System.Collections.Generic;
using  Microsoft.Practices.EnterpriseLibrary.Common;
using  Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using  Microsoft.Practices.EnterpriseLibrary.Security.Cryptography;
using  Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.Configuration;

namespace  BeautyCode.ConApp
{
    [ConfigurationElementType (
typeof  (CustomSymmetricCryptoProviderData ))]
    
public   class  MySymmetricCryptoProvider:ISymmetricCryptoProvider 
    {
        
public  MySymmetricCryptoProvider (System.Collections.Specialized.NameValueCollection attributes)
        {}
        
        
public   byte [] Encrypt( byte [] plaintext)
        {
            
throw   new  NotImplementedException();
        }
        
        
public   byte [] Decrypt( byte [] ciphertext)
        {
            
throw   new  NotImplementedException();
        }
    }
    
}
复制代码

 

  未完待续。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

【Blog】http://virusswb.cnblogs.com/

【MSN】jorden008@hotmail.com

【说明】转载请标明出处,谢谢


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值