微软企业库4.1学习笔记(二十三)加解密模块3 示例代码
加密解密模块可以满足常用的对称加解密和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形式
//Encrypt the Sensitive Data String
string encryptedContentBase64=Cryptographer .EncryptSymmetric ("symmProvider","password");
1.2加密结果是bytep[]形式
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解密字符串
string encryptedContentBase64=Cryptographer .EncryptSymmetric ("symmProvider","SensitiveData");
//Decrypt the base64 encoded string
string readableString=string.Empty ;
readableString =Cryptographer .DecryptSymmetric ("symmProvider",encryptedContentBase64 );
2.2解密字符数组
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值
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值和文本是否匹配
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、实现接口的两个方法
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方法
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();
}
}
}
本文来自博客园,作者:jevan,转载请注明原文链接:https://www.cnblogs.com/DoNetCShap/archive/2011/12/14/2287866.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端