如何爲配置文件加密

原創地址:http://www.cnblogs.com/jfzhu/p/4039216.htmlhtml

轉載請註明出處web

 

在web.config或app.config文件裏咱們常常會存儲一些敏感信息,好比connectionStrings或者appSettings,好比像下面的文件。算法

<?xml version="1.0"?>
<configuration>
    <system.web>
      <compilation debug="true" targetFramework="4.0" />
    </system.web>
    <connectionStrings>
      <add name="MyNwConnectionString" connectionString="Server=myServerAddress;Database=myDataBase;User Id=myUsername; Password=myPassword;"/>
    </connectionStrings>
    <appSettings>
      <add key="User" value="myUsername"/>
      <add key="Password" value="myPassword"/>
    </appSettings>
</configuration>
using System;
using System.Configuration;

namespace WebConfigEncryptTest
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string user = ConfigurationManager.AppSettings.Get("User");
            string password = ConfigurationManager.AppSettings.Get("Password");
            string connectionString = ConfigurationManager.ConnectionStrings["MyNwConnectionString"].ConnectionString;
        }
    }
}

 

(一)加密文件能夠使用的Provider

.NET爲咱們提供了一個工具aspnet_regiis.exe來對web.config文件中的敏感信息進行加密(app.config文件能夠先更名爲web.config,加密後再改回app.config)。你能夠使用兩個provider中的一個來進行加密:c#

  • System.Configuration.DPAPIProtectedConfigurationProvider:在System.Configuration.dll中,使用Windows DPAPI(Data Protection API)來進行加密,密鑰存在Windows Local Security Authority(LSA)中。注意:當使用DPAPIProtectedConfigurationProvider時,加密文件所使用的賬號須要與運行web application的賬號相同,不然web application沒法解密加密的內容。
  • System.Configuration.RSAProtectedConfigurationProvider:在System.Configuration.dll中,使用RSA算法來進行加密(RSA算法是非對稱加密,參見《對稱加密與非對稱加密 》),公鑰存放在config文件當中,只有加密的計算機有密鑰。RSAProtectedConfigurationProvider一般是默認的缺省provider。

(二)加密文件的命令

加密web.config文件能夠使用:app

aspnet_regiis -pef section web-app-physical-diride

                    Encrypt the configuration section. Optional arguments:工具

                    [-prov provider] Use this provider to encrypt.post

 

好比運行下面的命令就會分別對connectionStrings和appSettings中的信息進行加密:this

 

aspnet_regiis.exe -pef "connectionStrings" "C:\myweb\HelloService"加密

aspnet_regiis.exe -pef "appSettings" "C:\myweb\HelloService"

 

image

 

加密後的web.config文件變成:

 

<?xml version="1.0"?>
<configuration>
    <system.web>
      <compilation targetFramework="4.0" />
    </system.web>
    <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>E2fO9C0TJVxImLYQZza+fCQdDbTpNh/kOKLRsK6zcFjkgtUCl6SnMViuu/2G1NVTxqXyEWYwyK6AiCZA+feeG/AvYvmEEVopVDb0YyGeuJgEI1r8HxTl8Cv+f2EIimP7LJI+JRZVerI4MU6Ke3wxm2S/ATc73/W6eg9808f4/D6J0pp3wND4E79gBiAnBHFYQIefdJYUsmHR9z9LiIqjCllkkj/JB0kso0kGJ9i+iew1Jae5jugIN8gPxsXbCfmw6ru3I3Kbpa8Z5AllfkFA2YKrsuV3c7eLLJ0kB4lsIJIUTy3kRyA4GjdChOmlNwwffIbhwUPPxa25CiF0VAq27Q==</CipherValue>
                    </CipherData>
                </EncryptedKey>
            </KeyInfo>
            <CipherData>
                <CipherValue>I1DWG11Iz/rq+NC9C/21B3Q22J9+IexHPH6kkWvQPeHUO6OvOWeQbk3wHALR2ql8pz0gQJFyfTypMk/xSSikFI2Dcy5mgYY3kP73bQQ83ho3O1HPw9TsRtK1G8gmVNGyQLj7iTRcoGfiYYmSibPynv1MzSV1qDXlnVfKiMqKRZ5ZPiMSMc5u3dDEL/JW1oCvAGs5tHrZU5+vgvm0yCmSuCWZbXva+iv9J35EQqs58pq+hwVo1hg1dffdupGCBykaXGl5VX3TIGc=</CipherValue>
            </CipherData>
        </EncryptedData>
    </connectionStrings>
    <appSettings 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>WVoFIs8rSEgqKw1C0QCmePs7WK6EIoGCfdx9CTJNmABoVvoEWPnOEQwz/6Ruu0rGwa7q91KuhGILmy4NEN0padnX6FScCdEzP6CS59U3IFumYmTrD7D9ihqFO2aIL/SuBvV3D2kxhHaYGFaPuvYgsyOLf3+aYR3O/uh/k5wZxLoIeKUUrT762J3bdaK6cJWQeuOu4j2vDXEdawdwhlnK12UV8+/AXZNlFW1N3Z0RUVFX1nMSwTaIu8F3tZ9hCFbGwbTm2T0XnfDOcB6dCxCutqC8pXD36laAfiSANzAWoC+Yhf5eFSj24fX0NU6UTQB8fqLyOgWsIMLxZLKVrwnlmg==</CipherValue>
                    </CipherData>
                </EncryptedKey>
            </KeyInfo>
            <CipherData>
                <CipherValue>5W2KhG/oETLUDptobcOM52x1qD/g9A0By/wcGXI+fm7EdcD8mT3TxsLVBVcHRBCyUO7OIHl8NyCrduRSYwyd8ggBCriQ5KrbAmW4LXrNnw/JjjCEJWPuRcRucVRfpgap2nHh6BXRXC/AU6v0GcRqy7LV8179PgGtyAa8IE1mV/w=</CipherValue>
            </CipherData>
        </EncryptedData>
    </appSettings>
</configuration>

 

 RSAProtectedConfigurationProvider是默認的缺省provider,若是想使用DPAPIProtectedConfigurationProvider,能夠用-prov參數指明:

 

aspnet_regiis.exe -pef "connectionStrings" "C:\myweb\HelloService" -prov "DataProtectionConfigurationProvider"

aspnet_regiis.exe -pef "appSettings" "C:\myweb\HelloService" -prov "DataProtectionConfigurationProvider"

 

加密配置文件後,源程序不須要作任何改動。若是要修改或添加新的配置信息,須要先解密配置文件。不論使用哪一種Provider,都只能在進行加密的計算機上對配置文件進行解密。

 

(三)解密文件的命令

解密的命令以下(解密命令不須要-prov參數):

-pdf section web-app-physical-dir

                    Decrypt the configuration section.

 

aspnet_regiis.exe -pdf "connectionStrings" "C:\myweb\HelloService"

aspnet_regiis.exe -pdf "appSettings" "C:\myweb\HelloService"

 

image

 

(四)總結

配置文件中常常會有用戶名密碼的敏感信息,爲了防止該信息泄露,須要對配置文件進行加密。加密與解密能夠使用.NET提供的工具aspnet_regiis.exe,能夠在Windows .NET的文件夾中找到它。

該工具只對web.config文件進行修改,若是要加密或解密app.config,能夠先將app.config文件更名爲web.config,加密或解密後再改回來。

相關文章
相關標籤/搜索