亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

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

系統 1599 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條評論
主站蜘蛛池模板: 亚洲日韩色综合视频 | 成人午夜爽爽爽免费视频 | 性生大片一级毛片免费观看 | 午夜影院0606 | 国产精品成人扳一级aa毛片 | 麻豆国产精品高中生视频 | 欧美黄视频在线观看 | 欧美一级www片免费观看 | 国产成人精品一区二三区在线观看 | 99资源站 | 免费一级欧美片在线观看 | 看全色黄大色黄大片 视 | 国产免费观看a大片的网站 国产免费精彩视频 | 久艾草国产成人综合在线视频 | 欧美成人午夜视频免看 | 精品亚洲一区二区三区在线播放 | 农村妇女高清毛片一级 | 四虎日韩| 国产精品午夜免费福利视频 | 亚洲精品人成网线在线 | a视频网站 | 精品欧美一区二区三区免费观看 | 日本不卡网站 | 天堂成人在线 | 真实国产精品视频国产网 | 99热这里只有精品88 | 天天干天天操天天玩 | 久久青草18免费观看网站 | 亚洲视频 中文字幕 | 综合网在线观看 | 久久精品动漫99精品动漫 | 日韩高清在线二区 | 国产色婷婷精品免费视频 | 日韩欧美精品综合一区二区三区 | 色综合久久婷婷天天 | 国内精品久久久久久网站 | 青青爽国产手机在线观看免费 | 久久精品影视 | 看一级特黄a大片日本片 | 国产五月色婷婷六月丁香视频 | 久久精品免费看 |