日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

微軟企業庫4.1學習筆記(二十三)加解密模塊3

系統 1856 0

  加密解密模塊可以滿足常用的對稱加解密和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 ","S ensitiveData " );
????????????
????????????
// 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();
????????}
????}
????
}

?

  未完待續。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

微軟企業庫4.1學習筆記(二十三)加解密模塊3 示例代碼


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

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

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 连州市| 成都市| 奉贤区| 安达市| 彭泽县| 锡林浩特市| 卓尼县| 乌苏市| 韶山市| 秀山| 全椒县| 蓬溪县| 扶沟县| 龙川县| SHOW| 汪清县| 建始县| 扎兰屯市| 祁阳县| 潮州市| 乡城县| 海宁市| 迁西县| 祁门县| 阳曲县| 达尔| 遵义县| 陈巴尔虎旗| 多伦县| 拜泉县| 海丰县| 常州市| 闸北区| 武山县| 城口县| 汾阳市| 岳普湖县| 徐州市| 陇西县| 五台县| 昌宁县|