如何加密Web.Config中的连接字符串

来源:互联网 发布:淘宝客服售后快捷短语 编辑:程序博客网 时间:2024/06/05 06:24
Asp.Net用一种无格式的文本文件储存所有的配置信息叫Web.Config和Machine.Config,我们保存所有致关重要的信息,包括数据库连接字符串,其中就有数据库的用户名,密码,一旦在没有任何保护的安全措施下,这种后果将不可估计的;
       顺着这个线索,Microsoft在Asp.net2.0种已经提供了在配置文件中加密一些敏感信息的方法,包括数据库连接字符串,我们使用这个新的方法可以很简单,很容易加密配置文件的节点,从而使你的应用程序更安全。 
      在Asp.Net 2.0中介绍保护配置模型中,你可以使用两种保护配置信息的Providers,他们是:
           RSAProtectedConfigurationProvider :
               这是默认的Provider,可以使用这个RAS密匙和加密法则来加密和解密数据
           DataRrotectionConfigurationProvider : 
               这个Provider 使用Windows提供的数据保护应用程序接口(DPAPI)来加密解密数据
让我们探究一下,如何在Asp.Net 2.0中用以上两种Providers对Web.Config中数据库连接字符串加密和解密
Programmatic Encrption /Decrption

 下面是在没有经过加密的Web.Config的数据库连接字符串

<configuration>
    
<appSettings/>
    
<connectionStrings>
  
<add name="AspNet_BBsConnectionString" connectionString="Data Source=.;Initial Catalog=AspNet_BBs;User ID=sa"
   providerName
="System.Data.SqlClient" />
 
</connectionStrings>

    在以上的Web.Config中,可以观察一下Web.Config中的<connectionStrings>节点,包括了数据库的用户名
密码
   下面我们用第一种方法来对数据库连接字符串加密,
    首先新建一个WebApplication,并且添加一个单独的类,在这个单独的类中,我们主要用到三个命名空
间,第一个是System.configuration,在这个类中包含了处理Client 和Asp.Net Application的配置信息。第二个是System.Web.Configuration.WebConfigurationManager类,它包含了对Asp.net Web pplications的配置文件进行编辑。
  然后添加一个对Web.Config加密的静态方法EncrptConnectionString,

/// <summary>
    
/// 加密类  Create : 兴百放 Time : 2007-6-7-16-29
    
/// </summary>

    public static void EncrptConnction()
    {
        
//打开此WebApplication的Web.Config

        Configuration config = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);

        
//得到Web.Config的connectionStrings

        ConfigurationSection section = config.GetSection("connectionStrings");
        
if(!
section.SectionInformation.IsProtected)
        {
            section.SectionInformation.ProtectSection(
"RsaProtectedConfigurationProvider"
);
            config.Save();
        }
    }
之后再添加一个对Web.Config解密的静态方法DecrptConnectionString
/// <summary>
    
/// 解密类   Create : 兴百放 Time : 2007-6-7-16-29

    
/// </summary>

    public static void DecrptConnction()
    {
        Configuration config 
=
 WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
        ConfigurationSection section 
= config.GetSection("connectionStrings"
);
        
if
 (section.SectionInformation.IsProtected)                 
        {
            section.SectionInformation.UnprotectSection(); 
//更新Web.config

            config.Save();
        }
    }

    最后新建一个Web页,在这个页上拖两个Button,一个命名为Web.Config Encrpt.它执行的是EncrptConnectionString;另一个为Web.Config Decrpt ,执行的是DecrptConnection最后
执行,可以看看效果了
上面就是经过加密之后的Web.Config中的数据库连接字符串
但是,我们不能对Web.Config中的所有的节点用以上的方法加密,有些节点,我们需要一些额外的步骤对他们加密
<processModel>

<configuration>
    
<appSettings/>
    
<connectionStrings configProtectionProvider="RsaProtectedConfigurationProvider">
  
<EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element"
   xmlns
="http://www.w3.org/2001/04/xmlenc#">
   
<EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc" />
   
<KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
    
<EncryptedKey xmlns="http://www.w3.org/2001/04/xmlenc#">
     
<EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5" />
     
<KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
      
<KeyName>Rsa Key</KeyName>
     
</KeyInfo>
     
<CipherData>
      
<CipherValue>fgIDeCHVR8nxK3rppwqc8pJPh3E3anEoqCSAdDFfe2Oysq28i++11Y3fIx7PH/CsoetCRw2Tx9qCw2ATFZ6GVPwQ+/938A8g8DsfaPG2S9s89RL0Yz4szQpILvhkTZlyuB8C7kH/9TjQhWb3Q5/XSrkqVe3ggvKBIf+QsqfCKbw=</CipherValue>
     
</CipherData>
    
</EncryptedKey>
   
</KeyInfo>
   
<CipherData>
    
<CipherValue>lGZRXAwKFXGO36CyqqfKG6PXUsegnkm9ZmKMBoFN3wxVDPC9WkVr9yEySo2hScbrMzcWgJiy9dx7mjUDEa2QevpRe6nI3Gx5QKRLy0rTboBI2ID49XrWYMsrBLOVrqs8bMJNSFHe5aKmQJCdAGqrDeB9PKf6Syuupc2gv89m9Vq5hXy2k7Lw20vTghdK/YJyoJcwt/dPUaShwhoYGrI5P0cGqtI/c15jo6tiwt5M+ZvKtogcAfy15SfMvrERojWhUQeo98rl19XmxQwrNxn7Cw==</CipherValue>
   
</CipherData>
  
</EncryptedData>
 
</connectionStrings>

 

<runtime>

<mscorlib>

<startup>

<system.runtime.remoting>

<configProtectedData>

<satelliteassemblies>

<cryptographySettings>

<cryptoNameMapping>

<cryptoClasses>

       为了加密这些configruation section ,你必须加密这些节点的值,并且在注册表中保存
Encryption/Decryption using aspnet_regiis.exe command line tool

        在这里我们介绍另一种对Web.Config加密的方法,就是使用Ms提供的命令行工具(aspnet_regiis.exe)进行操作,你可以在<WINDOWSDIR>/Microsoft.Net/Framework/你的版本,下找到aspnet_regiis。
        我们使用一下的命令对Web.Config进行加密
                     aspnet_regiis.exe -pe "connectionStrings" -app "/你的站点名称" “prov "DataProtectionConfigurationProvider"
        如果想要解密的话,使用一下的命令
                    aspnet_regiis.exe -pd "connectionStrings" -app "/你的站点的名称"

          对于第二种方法我没有进行测试,如果有什么不对的地方,还请谅解!
         虽然,Asp.Net对所有以.config后缀名的Http请求拒绝,但是如果一些恶意的攻击者,获得了对服务器文
件系统操作权限,那么我们保存在Web.Config中的一些敏感信息将会泄露。幸运的是,Asp.Net 2.0提供了对
配置文件加密的加密术来避免这种问题,你可以从Programmatically 和 aspnet_regiis.exe工具任选一个来对Web.Config和Machine.Config进行加密和解密


        以上是我对Encrypting Connection Strings in web.config file的翻译,这是我第一次翻译文章,如果有什么
出路的话,还请谅解,谢谢!
 

原创粉丝点击