加密解密模塊可以滿足常用的對稱加解密和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?);
?
1.3使用的時候有兩點需要注意
- 確保symmProvider是在配置中存在的對稱加解密算法,配置了適當的算法。
- 敏感數據應該及時從內存中清空。Array.Clear方法就是這個功能,在內存中保留敏感數據是很危險的。
2、使用對稱加密算法解密數據
2.1解密字符串

???????????? 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.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.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();
????????}
????}
????
}
?
未完待續。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
