zoukankan      html  css  js  c++  java
  • 加密webconfig中的连接字符串,利用RSA非对称加密,利用windows保存密钥容器

    简单的解决方法:

    WebConfig 加解密,未能使用提供程序“RsaProtectedConfigurationProvider”进行解密。提供程序返回错误消息为: 打不开 RSA 密钥容器。
    问题:未添加用于访问 RSA 密钥容器
    命令:aspnet_regiis -pa "NetFrameworkConfigurationKey" "NT AUTHORITY/NETWORK SERVICE"
    注意事项:XP下:aspnet_regiis -pa "NetFrameworkConfigurationKey" "aspnet"
    加密:aspnet_regiis -pe "appSettings" -app "/应用程序名"
    解密:aspnet_regiis -pd "appSettings" -app "/应用程序名"  如(/PetShop/web)

    更灵活的解决方法:
    1、创建一个密钥容器 
       aspnet_regiis -pc "ConnectionStringsKey" -exp
       ConnectionStringsKey为密钥容器的名称 
       可以使用aspnet_regiis /?查看该命令的用法

    2、在web.config中加入如下内容

    1. <configProtectedData>   
    2.         <providers>   
    3.             <clear />   
    4.             <add name="ConnectionStringsKeyProvider"  
    5.         type="System.Configuration.RsaProtectedConfigurationProvider, System.Configuration, Version=2.0.0.0,Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"   
    6.         keyContainerName="ConnectionStringsKey"   
    7.         useMachineContainer="true"/>   
    8.         </providers>   
    9. </configProtectedData>  

     
    3、通过命令行:用指定的密钥加密指定目录下的web.config文件的指定的配置节
         aspnet_regiis -pef "connectionStrings" "d:/testproj/websitetest" -prov "ConnectionStringsKeyProvider"
        对于子配置节用/分隔表示, 如identity配置节 需要写成 "system.web/identity"
    4、如果访问web程序,页面提示 Error message from the provider: The RSA key Container could not be opened.
         是由于network service帐户无法访问密钥文件造成的。 找到密钥文件, 赋予network service读权限。该密钥文件位于(可按时间排序,找到自己产生的那个密钥文件)
    vista: c:/ProgramData/Microsoft/Crypto/RSA/MachineKeys/
    xp或其他:C:/Documents and Settings/All Users/Application Data/Microsoft/Crypto/RSA/MachineKeys

    至此:查看被加密的标记, 内容就已经是被加密过的了。

    5.通过.aspx页面:加密连接字符串:界面如图:

    截图06

    后台代码:

    1. //加密按钮  
    2. protected void Button1_Click(object sender, EventArgs e)   
    3. {   
    4.    //①需要加密的节点:   
    5.     string name = @"connectionStrings";   
    6.    //②当前路径;   
    7.     string appPath = "/loginContral";   
    8.     Configuration config = WebConfigurationManager.OpenWebConfiguration(appPath);   
    9.     //③提供加密的方式:(这里使用自定义的加密方式)   
    10.     // string provider = "RsaProtectConfigurationProvider";   
    11.     string provider = "ConnectionStringsKeyProvider";    
    12.     config.GetSection(name).SectionInformation.ProtectSection(provider);   
    13.   
    14.     //⑤保存web.config文件   
    15.     try   
    16.     {   
    17.         config.Save();   
    18.     }   
    19.     catch (Exception ex)   
    20.     {   
    21.         Response.Write(ex.Message);   
    22.     }   
    23.     if (config.GetSection(name).SectionInformation.IsProtected)   
    24.     {   
    25.         Button1.Enabled = false;   
    26.         Response.Write("加密成功!");   
    27.     }   
    28.     else   
    29.     {   
    30.         Response.Write("加密失败!");   
    31.     }   
    32. }  
    33.   
    34. //解密按钮:  
    35.   
    36. protected void Button2_Click(object sender, EventArgs e)   
    37. {   
    38.      //①需要节密的节点:   
    39.      string name = @"connectionStrings";   
    40.   
    41.     //②当前路径;   
    42.      string appPath = "/loginContral";   
    43.      Configuration config = WebConfigurationManager.OpenWebConfiguration(appPath);   
    44.   
    45.     //③使用UnprotectSection方法进行解密;        
    46.   
    47.      config.GetSection(name).SectionInformation.UnprotectSection();   
    48.   
    49.      //④保存web.config文件        
    50.      config.Save();   
    51.   
    52.      if (config.GetSection(name).SectionInformation.IsProtected==false)   
    53.      {   
    54.          Button2.Enabled = false;   
    55.          Response.Write("解密成功!");   
    56.      }   
    57.      else   
    58.      {   
    59.          Response.Write("解密失败!");   
    60.      }   
    61. }  

    注意:string appPath = "/loginContral" 为当前项目路径;

    截图07

    加密前的连接字符串:

    1. <connectionStrings>  
    2.     <add name="connection" connectionString="data source=.;database=aspnetdb;user id=sa;pwd=123;" />  
    3. </connectionStrings>  

         
    加密后的连接字符串:

    1. <connectionStrings configProtectionProvider="ConnectionStringsKeyProvider">  
    2.        <EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element"  
    3.            xmlns="http://www.w3.org/2001/04/xmlenc#">  
    4.            <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc" />  
    5.            <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">  
    6.                <EncryptedKey xmlns="http://www.w3.org/2001/04/xmlenc#">  
    7.                    <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5" />  
    8.                    <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">  
    9.                        <KeyName>Rsa Key</KeyName>  
    10.                    </KeyInfo>  
    11.                    <CipherData>  
    12.                        <CipherValue>AepogG4vVhd8K6NVhVmdO8FAGFMopOdDvnBN5vPV0mxP8NgrImnZFvflrhhvooiu56McmMr6n5cUnixzimGB/zTgCNMsIkU8Sr6YtX8iUh64U9IVujwaOAbtZp4AhLhMiH6YwkHXjmqrjYyS2ecsocquZQ0ndkKC3OMg/UcOIk0=</CipherValue>  
    13.                    </CipherData>  
    14.                </EncryptedKey>  
    15.            </KeyInfo>  
    16.            <CipherData>  
    17.                <CipherValue>biMAH/6vwvi0FKvqijpSZzKhk+a6QNi0Aa794yxi1X+sffKdtsUR15hVcByOlborcKPRhX94MpOm2eKoBqYVyCf24PdYAkIFFAzO1sluzmUtcXFVU/lTBqn83bnJDgBgo6eVtDg4m7DSAVR6qWyEP8wySqWWuBkWSLzsMynqPOyGhVB9bTVJbSCWiUZ4ynFhvUTziGISJQA=</CipherValue>  
    18.            </CipherData>  
    19.        </EncryptedData>  
    20.    </connectionStrings>  

     
    其他备用操作:
    1、解密web.config 
        aspnet_regiis -pdf "connectionStrings" "d:/testproj/websitetest"
    2、把密钥容器导出为xml文件 
        aspnet_regiis -px "ConnectionStringsKey" "c:/Key.xml"这个命令只导出公钥,因此以后只能用于加密,而无法解密。
        aspnet_regiis -px "ConnectionStringsKey" "c:/Keys.xml" -pri  这个则连私钥一起导出了,所以我们要用这个
    3、把密钥容器删除  
       aspnet_regiis -pz "LixinKey"   删除后再运行程序,会提示出错: 
        分析器错误信息: 未能使用提供程序“LixinKeyProvider”进行解密。提供程序返回错误信息为: 打不开 RSA 密钥容器。 
        同理可以证明,在任何一台未安装正确的密钥容器LixinKey的机器上,程序都无法对connectionStrings节进行解密,因此也就无 法正常运行。
    4、导入key.xml文件 
         aspnet_regiis -pi "LixinKey" "c:/Keys.xml"

         此时,再运行程序会发现又可以解密了。证明加密与解密机制运行正常。
    最后说一下这个机制所提供的安全性保障可以运用在什么方面:
    1. 对winform程序的app.config进行加密实际意义并不大,因为无论如何,客户机都可以通过运行aspnet_regiis -pdf 来对配置文件进行解密,从而暴露敏感信息。
    2. 对于web.config进行加密的意义也仅限于,当web.config文件不小心泄露时,不会同时泄露敏感信息,如果恶意攻击者已经取得了在服务器上运行程序的权限,那么同app.config一样,可以很容易通过通过运行aspnet_regiis -pdf 获取明文了。
    3. 还有,通过aspnet_regiis -pa "Key" "NT AUTHORITY/NETWORK SERVICE"控制对不同用户对密钥容器的访问权限,应该还可以进一步获取一些安全性,比如可以控制某些用户即使登录到服务器上,也无法用aspnet_regiis -pdf对配置文件进行解密。

  • 相关阅读:
    AJAX请求MVC控制器跨域头问题
    HTTP 错误500.19 -Internal Server Error 错误代码 0x80070021
    C# 同一时间批量生成订单号不重复
    Unity书籍下载地址
    几种常见的设计模式
    C# web api 对象与JSON互转
    自动按参数首字母排序参数
    C# 3DES加密 解密
    C#大量数据导出Excel
    判断对象是数组
  • 原文地址:https://www.cnblogs.com/armanda/p/6068818.html
Copyright © 2011-2022 走看看