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

本文介绍ASP.NET中machine.config文件配置的两种内置加密方式:RsaProtectedConfigurationProvider和DataProtectionConfigurationProvider,并提供了通过命令行工具进行加密操作的方法。此外,还展示了如何使用System.Security.Cryptography命名空间中的SHA1CryptoServiceProvider类来加密字符串。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 machine.config中指出了asp.net内置的两种加密方式,如下

  1. <configProtectedData defaultProvider="RsaProtectedConfigurationProvider">
  2.     <providers>
  3.       <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" />
  4.       <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="" />
  5.     </providers>
  6. </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,示例如下

       

  1. UnicodeEncoding ue = new UnicodeEncoding();
  2. byte[] byteMessage = ue.GetBytes( "abcde" );
  3. SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider( );
  4. byte[] hashValue = sha1.ComputeHash( byteMessage );
  5. string hashString = Convert.ToBase64String( hashValue );
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值