本章介绍的是企业库加密应用程序模块Cryptographyproviders中为对称加密配置Key文件的3种方式:
废话少说,现在就开始看看如何使用它们吧:
一. Create a new key
1.运行EntLibConfig.exe,选择Blocks菜单 ,单击 Add CryptographySettings .
2.点击symmetriccryptography provider 区块右上角的加号按钮,然后点击 Add Symmetric Cryptography Providers,在弹出的加密算法中任意选择一个:
3.在弹出的Key文件生成方案中,我们选择第一个Create a new key,点击Next:
4.下一步是要你输入Key码,你可以自己手动输入一串自己定制好的Key码,也可以点击右下角的Generate按钮,让计算机为你自动生成一串Key码,在此我是直接点击Generate按钮生成的Key码,然后点击Next:
5.接着是选择Key文件导出的目录,这里我先保存成桌面的test.key文件,设置好后点击Next:
6.接着是选择模式,有User模式和Machine模式:
(1)User模式:每个应用程序有自己的唯一标识,无法访问其他应用程序的资源.
(2)Machine模式:加密的文件只能使用在本电脑上使用,也就是说用这个模式,在其他电脑你还需要重新生成一个Key文件.
在本地调试哪个模式都无所谓,我们就选择User模式吧:
7.再点击Finish,就完成配置啦:
桌面导出的Key文件:
8.点击 File 菜单,单击 Save,保存为一个App.config文件,可以先保存到桌面,之后要用到它. 用记事本打开App.config,可以看到如下内容:
< configuration >
< configSections >
< section name ="securityCryptographyConfiguration" type ="Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.Configuration.CryptographySettings,Microsoft.Practices.EnterpriseLibrary.Security.Cryptography, Version=5.0.414.0,Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission ="true" />
</ configSections >
< securityCryptographyConfiguration >
< symmetricCryptoProviders >
< add name ="RC2CryptoServiceProvider" type ="Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.SymmetricAlgorithmProvider,Microsoft.Practices.EnterpriseLibrary.Security.Cryptography, Version=5.0.414.0,Culture=neutral, PublicKeyToken=31bf3856ad364e35"
algorithmType ="System.Security.Cryptography.RC2CryptoServiceProvider,mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
protectedKeyFilename ="C:\Users\Administrator\Desktop\test.key"
protectedKeyProtectionScope ="CurrentUser" />
</ symmetricCryptoProviders >
</ securityCryptographyConfiguration >
</ configuration >
9.要使用缓存应用程序模块, 需要导入相应的Dll文件,在此我们要导入的是Microsoft.Practices.EnterpriseLibrary.Caching.dll ,将App.config文件添加到项目中,并添加Microsoft.Practices.EnterpriseLibrary.Security.Cryptography引用:
添加引用:
using Microsoft.Practices.EnterpriseLibrary.Security.Cryptography;
10.测试:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Security.Cryptography;
namespace test
{
class Program
{
static void Main( string [] args)
{
string Encrypt = Cryptographer.EncryptSymmetric( " RC2CryptoServiceProvider " , " HuangCong " );
Console.WriteLine( " 密文: " + Encrypt);
Console.WriteLine( " ------------------------------------------------ " );
Encrypt = Cryptographer.DecryptSymmetric( " RC2CryptoServiceProvider " , Encrypt);
Console.WriteLine( " 原文: " + Encrypt);
}
}
}
11.运行结果:
二. use an existingDPAPI-protected key file:
6.点击 File 菜单,单击 Save更新原有的App.config文件,打开可看到以下内容.
< configuration >
< configSections >
< section name ="securityCryptographyConfiguration" type ="Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.Configuration.CryptographySettings,Microsoft.Practices.EnterpriseLibrary.Security.Cryptography, Version=5.0.414.0,Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission ="true" />
</ configSections >
< securityCryptographyConfiguration >
< symmetricCryptoProviders >
< add name ="RC2CryptoServiceProvider" type ="Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.SymmetricAlgorithmProvider,Microsoft.Practices.EnterpriseLibrary.Security.Cryptography, Version=5.0.414.0,Culture=neutral, PublicKeyToken=31bf3856ad364e35"
algorithmType ="System.Security.Cryptography.RC2CryptoServiceProvider,mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
protectedKeyFilename ="C:\Users\Administrator\Desktop\test.key"
protectedKeyProtectionScope ="CurrentUser" />
</ symmetricCryptoProviders >
</ securityCryptographyConfiguration >
</ configuration >
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Security.Cryptography;
namespace test
{
class Program
{
static void Main( string [] args)
{
string Encrypt = Cryptographer.EncryptSymmetric( " RC2CryptoServiceProvider " , " HuangCong " );
Console.WriteLine( " 密文: " + Encrypt);
Console.WriteLine( " ------------------------------------------------ " );
Encrypt = Cryptographer.DecryptSymmetric( " RC2CryptoServiceProvider " , Encrypt);
Console.WriteLine( " 原文: " + Encrypt);
}
}
}
三. Import a password-protected key file
10. 点击 File 菜单,单击 Save更新原有的App.config文件,打开可看到以下内容:
< configuration >
< configSections >
< section name ="securityCryptographyConfiguration" type ="Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.Configuration.CryptographySettings,Microsoft.Practices.EnterpriseLibrary.Security.Cryptography, Version=5.0.414.0,Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission ="true" />
</ configSections >
< securityCryptographyConfiguration >
< symmetricCryptoProviders >
< add name ="RC2CryptoServiceProvider" type ="Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.SymmetricAlgorithmProvider,Microsoft.Practices.EnterpriseLibrary.Security.Cryptography, Version=5.0.414.0,Culture=neutral, PublicKeyToken=31bf3856ad364e35"
algorithmType ="System.Security.Cryptography.RC2CryptoServiceProvider,mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
protectedKeyFilename ="C:\Users\Administrator\Desktop\test1.key"
protectedKeyProtectionScope ="CurrentUser" />
</ symmetricCryptoProviders >
</ securityCryptographyConfiguration >
</ configuration >
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Security.Cryptography;
namespace test
{
class Program
{
static void Main( string [] args)
{
string Encrypt = Cryptographer.EncryptSymmetric( " RC2CryptoServiceProvider " , " HuangCong " );
Console.WriteLine( " 密文: " + Encrypt);
Console.WriteLine( " ------------------------------------------------ " );
Encrypt = Cryptographer.DecryptSymmetric( " RC2CryptoServiceProvider " , Encrypt);
Console.WriteLine( " 原文: " + Encrypt);
}
}
}
http://www.cnblogs.com/huangcong/archive/2010/05/29/1747015.html