日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Microsoft Enterprise Library 5.0 系列教程(二) Cryptography Application Block (初级)

發布時間:2025/3/15 编程问答 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Microsoft Enterprise Library 5.0 系列教程(二) Cryptography Application Block (初级) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

企業庫加密應用程序模塊提供了2種方式讓用戶保護自己的數據:

  • Hashingproviders: ?離散加密法, 簡單來說就是把你的信息保存到內存中后用一個離散值表示并返回給程序,這樣在程序中只能看到離散值而不是明文,這樣就起到簡單的加密效果啦.
  • Cryptographyproviders:?密鑰加密法. 用對稱加密方法對數據進行加密(尚未支持非對稱加密).
  • 使用企業庫加密應用程序模塊的優勢:

  • 減少了需要編寫的模板代碼,執行標準的任務,可以用它來解決常見的應用程序加密的問題.
  • 有助于維持一個應用程序內和跨企業的數據傳輸加密.
  • 允許管理員進行加密配置,包括使用組策略.
  • 可擴展,支持用戶自定義加密技術.
  • 下面介紹如何使用Microsoft Enterprise Library 5.0中的加密應用程序模塊.

    1.下載安裝好MicrosoftEnterprise Library 5.0,然后在運行EntLibConfig.exe

    2. 選擇Blocks菜單 ,單擊?Add CryptographySettings .

    下面分別樣式如何創建Hash Providers?和?Symmetric CryptographyProviders?加密策略:

    (A)???Hash Providers?策略使用步驟:

      (1)????點擊HashProviders?區塊右上角的加號按鈕,?Add Hash Providers, 然后點擊Add Hash Algorithm Provider,在彈出的對話框中選擇System.Core下的MD5Cng,

        ? 表示我們要用MD5的加密方法獲取離散值.

      (2) 點擊?File?菜單,單擊?Save,保存為一個App.config文件,可以先保存到桌面,之后要用到它. 用記事本打開App.config,可以看到如下內容.

    代碼

      (3) 要使用緩存應用程序模塊, 需要導入相應的Dll文件,在此我們要導入的是Microsoft.Practices.EnterpriseLibrary.Caching.dll?,將App.config文件添加到項目中,

         并添加usingMicrosoft.Practices.EnterpriseLibrary.Security.Cryptography引用:

      添加引用:

    usingMicrosoft.Practices.EnterpriseLibrary.Security.Cryptography;

      (4) 測試:

    usingSystem;
    using?System.Collections.Generic;
    using?System.Linq;
    using?System.Text;

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

    namespace?test
    {
    classProgram
    {
    staticvoid Main(
    string[]args)
    {

    //獲取離散碼
    stringhash?=?Cryptographer.CreateHash("MD5Cng",?"SensitiveData");

    //打印顯示
    Console.WriteLine(hash);

    Console.WriteLine(
    "------------------------------------------------");

    //驗證
    boolequal?=?Cryptographer.CompareHash("MD5Cng",?"SensitiveData",hash);

    //打印結果
    if(equal)
    {
    Console.WriteLine(
    "正確");
    }
    else
    {
    Console.WriteLine(
    "錯誤");
    }
    }
    }
    }

    運行結果:

    (B)????Symmetric CryptographyProviders策略實現步驟:

      (1)????點擊symmetriccryptography provider??區塊右上角的加號按鈕,然后點擊?Add Symmetric Cryptography Providers, 在此我們能看到3個選項,下面介紹一下:  

    •  Add Custom SymmetricCrypto Provider :顧名思義,用戶自定義的加密策略,較麻煩,要自己寫相應的加密類.?
    •  Add DPAPI Symmetric Crypto Provider :?添加一個數據加密API生成的對稱密鑰進行加密.
    •  Add Sysmmetric Algorithm Provider :?較高級的對稱加密方法,需要用戶生成Key文件對數據進行保護.

    ?  在此我介紹的是第二種方法,因此請單擊選擇?Add DPAPI Symmetric Crypto Provider.

    ?  

      (2)????點擊?File?菜單,單擊?Save更新原有的App.config文件,打開可看到以下內容.

    代碼

      (3)???? 測試:

    using?System;
    using?System.Collections.Generic;
    using?System.Linq;
    using?System.Text;

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

    namespace?test
    {
    class?Program
    {
    staticvoid?Main(string[] args)
    {
    ////獲取離散碼
    //string hash = Cryptographer.CreateHash("MD5Cng", "SensitiveData");

    ////打印顯示
    //Console.WriteLine(hash);

    //Console.WriteLine("------------------------------------------------");

    ////驗證
    //bool equal = Cryptographer.CompareHash("MD5Cng", "SensitiveData", hash);

    ////打印結果
    //if (equal)
    //{
    //?Console.WriteLine("正確");
    //}
    //else
    //{
    //?Console.WriteLine("錯誤");
    //}

    string?Encrypt?=?Cryptographer.EncryptSymmetric("DPAPI Symmetric Crypto Provider",?"SensitiveData");

    Console.WriteLine(
    "密文:"+?Encrypt);

    Console.WriteLine(
    "------------------------------------------------");

    Encrypt?
    =?Cryptographer.DecryptSymmetric("DPAPI Symmetric Crypto Provider", Encrypt);
    Console.WriteLine(
    "原文:"+?Encrypt);
    }
    }
    }

    運行結果:

    ?




    本文轉自黃聰博客園博客,原文鏈接:http://www.cnblogs.com/huangcong/archive/2010/05/28/1746634.html,如需轉載請自行聯系原作者

    總結

    以上是生活随笔為你收集整理的Microsoft Enterprise Library 5.0 系列教程(二) Cryptography Application Block (初级)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。