Cryptography Application Block

本文介绍了CryptographyApplicationBlock的功能,包括加密算法和哈希算法,并提供了配置和使用的详细步骤。同时,文章还给出了一个简单的使用示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 功能简介

2. 如何配置和使用?

3. 设计实现及如何拓展?

4. 代码下载


1. 功能简介

Cryptography Application Block 主要实现了两个算法:Encryption algorithms和Hashing algorithms,其中Encryption algorithms就是我们通常意义上的加密算法,Hashing算法提供的是这样一种场景:用户提供一个带价密的文本,通过hash算法生成一个hash值,然后检查该hash值是否出自某个字符串(Checking whether a hash matches some text)。同样该block提供了自定义provider的功能。


2. 如何进行配置和简单使用?

2.1 新建一个c# console application,添加配置文件App.config,添加程序集引用如下图:

打开EntLibConfig.exe,打开上面的app.config。添加加密的application block,开始配置:

添加一个hash provider。

添加加密算法。

注意下面选择Machine Mode(The LocalMachine value means that that any code running on the machine has access to the protected key)。

配置加密程序块的默认hash和加密程序的provider:

配置完成,下面是一个简单的使用示例:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Practices.EnterpriseLibrary.Security.Cryptography; using Microsoft.Practices; using Microsoft.Practices.EnterpriseLibrary.Common.Configuration; namespace CryptographyBlockTest { class Program { static void Main(string[] args) { CryptographyManager crypto = EnterpriseLibraryContainer.Current.GetInstance<CryptographyManager>(); string encrypted = crypto.EncryptSymmetric("CryptoProvider", "test"); Console.WriteLine(encrypted); string decrypted = crypto.DecryptSymmetric("CryptoProvider", encrypted); Console.WriteLine(decrypted); HashTest(); Console.ReadKey(); } public static void HashTest() { CryptographyManager crypto = EnterpriseLibraryContainer.Current.GetInstance<CryptographyManager>(); byte[] valueToHash = (new UnicodeEncoding()).GetBytes("password"); byte[] generatedHash = crypto.CreateHash("MD5Cng", valueToHash); // generatedHash contains a hash value for a previously hashed string. byte[] stringToCompare = (new UnicodeEncoding()).GetBytes("password"); bool comparisonSucceeded = crypto.CompareHash("MD5Cng", stringToCompare, generatedHash); Console.WriteLine(comparisonSucceeded); // true } } }


3. 设计实现及如何拓展?


4. 代码下载

下载


本博客均是本人在学习过程中的总结,其中难免存在不足之处,欢迎指正。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值