您的位置:首页 > 编程语言 > ASP

.Net学习笔记 - Web.config节点加密

2008-07-17 14:55 615 查看
 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 );
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息