zoukankan      html  css  js  c++  java
  • Enterprise Library: Cryptography Application Block概述

    Enterprise Library: Cryptography Application Block概述

     

    Written by: Rickie Lee (rickieleemail#yahoo.com)

    My blog: www.cnblogs.com/rickie

    Enterprise Library Cryptography Application Block v1.0)简化了开发人员在应用程序中采用加密功能。应用程序可以使用该Application Block实现多项任务,如加密/解密信息,创建散列(Hash)和比较散列值确保原始数据没有被改变等等。通过使用Symmetric ProvidersHash Providers,该Application Block可以保护应用程序中的敏感数据。只需要简单1行代码,你就可以加密或解密、创建或比较散列。

     

    Cryptography Application Block配置文件

    Configuration Console配置管理工具用来定义和配置特定的对称加密算法或散列算法,如下是Cryptography Application Block Quickstart的配置文件:
    Enterprise_CryptographyAB_Configuration.jpg

     

    其中Symmetric Provider设置对称加密提供程序(Symmetric Provider),Hash Provider则设置散列提供程序(Hash Provider)。

     

    Cryptographer

    与其他Application Block类似,为了简化多数通用加密任务,Cryptography Application Block仅支持少量的方法。该Application Block提供Crytographer类,该类定义了如下一组静态方法:

    l         CreateHash 创建散列

    l         CompareHash  比较散列

    l         EncryptSymmetric 对称加密

    l         DecryptSymmetric 对称解密

     

    重载方法

    上述每一个静态方法均有2个重载,分别支持string字符串参数和字节数组(Byte Array)。字符串重载相对而言比较方便,返回值格式为base64编码。但使用字节数组重载可以最小化应用程序敏感信息在内存中的拷贝数,相对而言减少受攻击的几率。

    如下为EncryptSymmetric静态方法的重载列表:

    [Visual Basic] Shared  OverloadsPublic Function EncryptSymmetric(ByVal String,ByVal Byte()) As Byte()

    [C#] public static byte[] EncryptSymmetric(string,byte[]);

     

    [Visual Basic] Shared  OverloadsPublic Function EncryptSymmetric(ByVal String,ByVal String) As String

    [C#] public static string EncryptSymmetric(string,string);

    其中,第一个string参数指定配置文件中Symmetric Provider实例名称,第二个参数字节数组或string字符串指定需要加密的原始数据。

     

    散列Hash和盐值(Salt

    Cryptography Application Block支持大量现有的散列算法,如SHA,MD5等等。如下图所示,指定散列提供程序(Hash Provider):
    Enterprise_Cryptography_HashAlgorithm.jpg

    Hash Provider可以用于单向加密敏感数据,如口令等,可以通过比较Hash值进行验证,但不能反向解密。关于Hash的基本知识,可以参考《Duwamish密码分析篇, Part 1》(作者:Rickie Lee)。

    另外,默认情况下,该Application Block采用随机字节的SaltSalt值增加了Hacker字典攻击所需要的计算时间,这一策略有助于阻止潜在的攻击者利用预先计算的字典攻击。

    Cryptography Application Block也提供了加密的Hash算法,如HMACSHA1。加密的Hash算法使用密钥来保护Hash值,如果攻击者没有获取加密密钥,则无法计算并比较Hash值。如下图所示,在选择HMACSHA1作为Hash Provider时,要求进一步输入加密密钥:
    Enterprise_Cryptography_KeyCreation.jpg

    对称加密和解密

    Cryptography Application Block封装常用的对称加密解密算法,只需要使用Configuration Console配置管理工具和简单的代码就可以在应用程序中调用,如下所示:

    加密代码:

    byte[] valueToEncrypt = Encoding.Unicode.GetBytes("password");

    byte[] encryptedContents = Cryptographer.EncryptSymmetric("symmProvider", valueToEncrypt);

    // Clear the byte array memory that holds the password

    Array.Clear(valueToEncrypt, 0, valueToEncrypt.Length);

    解密代码:

    // stringToDecrypt contains an encrypted string

    byte[] decryptedContents = Cryptographer.DecryptSymmetric("symmProvider",stringToDecrypt);

    string plainText = (new UnicodeEncoding()).GetString(decryptedContents);

     

    可以通过Configuration Console配置管理工具选择需要的对称加密算法或Symmetric Provider和加密密钥。如下是在选择RijindaelManaged提供程序后出现的密钥输入窗口:
    Enterprise_Cryptography_KeyCreation2.jpg

    一旦你产生密钥并保存应用程序后,你将不再能够在Configuration Console配置工具中查看密钥,唯一的选择是导出密钥到文件中。

    如下是Enterprise Library Cryptography Application Block QuickStart演示程序界面:

    Enterprise_Cryptography_QuickStart.jpg

    ***

    作者:Rickie Lee (rickieleemail#yahoo.com)

    本文参考Microsoft Enterprise LibraryCryptography Application Block文档及其QuickStart

     

    References:

    1. Microsoft Enterprise Library: Caching Application Block.

    2. Rickie, Microsoft patterns & practices Enterprise Library January 2005 [中文稿], http://www.cnblogs.com/rickie/archive/2005/01/30/99443.html

    3. Rickie, Enterprise Library released! http://www.cnblogs.com/rickie/archive/2005/01/29/99106.html

    4. Hisham Baz, Easy Cryptography With EntLib, http://blog.hishambaz.com/archive/2005/02/08/274.aspx

     

  • 相关阅读:
    进度条
    html5 表单新增事件
    html5 表单的新增type属性
    html5 表单的新增元素
    html5 语义化标签
    jq 手风琴案例
    codeforces 702D D. Road to Post Office(数学)
    codeforces 702C C. Cellular Network(水题)
    codeforces 702B B. Powers of Two(水题)
    codeforces 702A A. Maximum Increase(水题)
  • 原文地址:https://www.cnblogs.com/rickie/p/105505.html
Copyright © 2011-2022 走看看