1.加密模块首先要添加引用
通过Enterprise Library Configuration工具打开app.config文件,如图
创建Cryptography Application Block节。系统会自动添加两个子节。Hash Providers和Symmetric Providers.
1)前一个是Hash算法加密,后者是对称算法加密。
2)删除Hash Providers节下SHA1Managed节,添加新节
选择偏好的加密方法。点击确定。
3)定义对称加密算法,操作类似hash加密算法
选择加密程序提供者
选择加密算法
创建密匙
生成密匙
指定密匙文件
指定加密模式(机器模式/用户模式)。完成加密
文件配置结束
3.代码
1)首先指定加密配置信息
symmetricName对应配置文件中加密模块子节symmetric providers中对称算法的名称
2)实现对称算法验证
static void Symmetric()
{
//待加密字符串
string test = "123456";
//加密后字符串
string oldstring;
//解密后字符串
string newstring;
//加密
oldstring = Cryptographer.EncryptSymmetric(symmetricName, test);
//解密
newstring = Cryptographer.DecryptSymmetric(symmetricName, oldstring);
//打印
Console.WriteLine(test);
Console.WriteLine(oldstring.ToString());
Console.WriteLine(newstring);
}
Cryptographer.EncryptSymmetric方法需要两个参数(加密算法名称和待加密字符串)
Cryptographer.decryptSymmetric方法需要两个参数(加密算法名称和待解密字符串)
输出
3)实现hash算法验证
static void Hash()
{
//待加密字符串
string test = "123456";
//转换为byte数组
byte[] oldstring;
//加密后的byte数组
byte[] newstring;
oldstring = System.Text.Encoding.UTF8.GetBytes(test);
//加密
newstring = Cryptographer.CreateHash(hashName, oldstring);
//判断输入字符串是否与待加密支付串一致
bool ok = Cryptographer.CompareHash(hashName, System.Text.Encoding.UTF8.GetBytes("123456"), newstring);
Console.WriteLine("test : "+test);
Console.WriteLine("result : "+ok.ToString());
Console.WriteLine();
}
Cryptographer.CreateHash()方法需要两个参数(加密算法名称和待加密字符串转换的字符数组)
Cryptographer.CompareHash()方法需要三个参数(加密算法名称和待比较字符串转换为字符数组以及待加密字符串转换的字符数组)
结果
using
Microsoft.Practices.EnterpriseLibrary.Security.Cryptography;
2.创建配置文件
通过Enterprise Library Configuration工具打开app.config文件,如图
创建Cryptography Application Block节。系统会自动添加两个子节。Hash Providers和Symmetric Providers.
1)前一个是Hash算法加密,后者是对称算法加密。
![](https://i-blog.csdnimg.cn/blog_migrate/d43e648a7123e84c473af5e4c63ac806.png)
2)删除Hash Providers节下SHA1Managed节,添加新节
![](https://i-blog.csdnimg.cn/blog_migrate/c1a9005a2ca75bc843c2fdec793877b9.png)
![](https://i-blog.csdnimg.cn/blog_migrate/f4b39e8d53002a1e8b402d756792eada.png)
选择偏好的加密方法。点击确定。
3)定义对称加密算法,操作类似hash加密算法
选择加密程序提供者
![](https://i-blog.csdnimg.cn/blog_migrate/5414a5477c1737de13df0172c57fdd37.png)
选择加密算法
![](https://i-blog.csdnimg.cn/blog_migrate/0257ed7a4c782bd24323ca8954a891dc.png)
创建密匙
![](https://i-blog.csdnimg.cn/blog_migrate/de97c415749483fde1b33b137051dca8.png)
生成密匙
![](https://i-blog.csdnimg.cn/blog_migrate/23c44029024325996b0008819501209c.png)
指定密匙文件
![](https://i-blog.csdnimg.cn/blog_migrate/f873e075873848500f2e4310015b61c4.png)
指定加密模式(机器模式/用户模式)。完成加密
![](https://i-blog.csdnimg.cn/blog_migrate/f21202bbdc84b869421bcbb03b52e3a4.png)
文件配置结束
3.代码
1)首先指定加密配置信息
static
string
hashName
=
"
SHA1Managed
"
;
static string symmetricName = " TripleDESCryptoServiceProvider " ;
其中hashName对应配置文件中加密模块子节hash providers 中hash算法名称
static string symmetricName = " TripleDESCryptoServiceProvider " ;
symmetricName对应配置文件中加密模块子节symmetric providers中对称算法的名称
2)实现对称算法验证
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
static void Symmetric()
{
//待加密字符串
string test = "123456";
//加密后字符串
string oldstring;
//解密后字符串
string newstring;
//加密
oldstring = Cryptographer.EncryptSymmetric(symmetricName, test);
//解密
newstring = Cryptographer.DecryptSymmetric(symmetricName, oldstring);
//打印
Console.WriteLine(test);
Console.WriteLine(oldstring.ToString());
Console.WriteLine(newstring);
}
Cryptographer.decryptSymmetric方法需要两个参数(加密算法名称和待解密字符串)
输出
![](https://i-blog.csdnimg.cn/blog_migrate/8eeb80e5eb728d5508a11197ebb9493e.png)
3)实现hash算法验证
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
static void Hash()
{
//待加密字符串
string test = "123456";
//转换为byte数组
byte[] oldstring;
//加密后的byte数组
byte[] newstring;
oldstring = System.Text.Encoding.UTF8.GetBytes(test);
//加密
newstring = Cryptographer.CreateHash(hashName, oldstring);
//判断输入字符串是否与待加密支付串一致
bool ok = Cryptographer.CompareHash(hashName, System.Text.Encoding.UTF8.GetBytes("123456"), newstring);
Console.WriteLine("test : "+test);
Console.WriteLine("result : "+ok.ToString());
Console.WriteLine();
}
Cryptographer.CompareHash()方法需要三个参数(加密算法名称和待比较字符串转换为字符数组以及待加密字符串转换的字符数组)
结果
![](https://i-blog.csdnimg.cn/blog_migrate/8921f811f3273bb596f9a13e700867b8.png)