`

微软企业库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形式

代码
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->
            
//Encrypt the Sensitive Data String
            string encryptedContentBase64=Cryptographer .EncryptSymmetric ("symmProvider","password");
            
  

 

  1.2加密结果是bytep[]形式

  

 

<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->

            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解密字符串

代码
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->//Encrypt the Sensitive Data String
            string encryptedContentBase64=Cryptographer .EncryptSymmetric ("symmProvider","SensitiveData");
            
            
//Decrypt the base64 encoded string
            string readableString=string.Empty ;
            readableString 
=Cryptographer .DecryptSymmetric ("symmProvider",encryptedContentBase64 );
        

 

  

  

  2.2解密字符数组

代码
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->
            
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值

 

代码
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->
            
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值和文本是否匹配

   

 

代码
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->
            
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、实现接口的两个方法

  

 

代码
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->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方法

  

 

代码
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->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();
        }
    }
    
}

 

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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics