machine.config中指出了asp.net内置的两种加密方式,如下
- <configProtectedData defaultProvider="RsaProtectedConfigurationProvider">
- <providers>
- <add name="RsaProtectedConfigurationProvider" type="System.Configuration.RsaProtectedConfigurationProvider,System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" description="Uses RsaCryptoServiceProvider to encrypt and decrypt" keyContainerName="NetFrameworkConfigurationKey" cspProviderName="" useMachineContainer="true" useOAEP="false" />
- <add name="DataProtectionConfigurationProvider" type="System.Configuration.DpapiProtectedConfigurationProvider,System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" description="Uses CryptProtectData and CryptUnProtectData Windows APIs to encrypt and decrypt" useMachineProtection="true" keyEntropy="" />
- </providers>
- </configProtectedData>
调用方法:在Visual Studio 2005 Command Prompt里面输入
aspnet_regiis -pef connectionStrings F:/WebSite -prov DataProtectionConfigurationProvider
注释:-pef 表示加密; -pdf表示解密.
connectionStrings 表示web.config文件中的节点名
F:/WebSite 表示站点路径
-prov DataProtectionConfigurationProvider表示采用何种方式加密(如果省略则表示使用machine.config中的默认配置defaultProvider)
-------------------------------------------------------------------------------------------------------
btw:如果需要在程序代码中加密字符串,可以使用System.Security.Cryptography中提供的一些类,例如SHA1CryptoServiceProvider,示例如下
- UnicodeEncoding ue = new UnicodeEncoding();
- byte[] byteMessage = ue.GetBytes( "abcde" );
- SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider( );
- byte[] hashValue = sha1.ComputeHash( byteMessage );
- string hashString = Convert.ToBase64String( hashValue );