国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#為配置文件加密的實(shí)現(xiàn)方法

2019-10-29 21:42:43
字體:
供稿:網(wǎng)友
這篇文章主要介紹了C#為配置文件加密的實(shí)現(xiàn)方法,可實(shí)現(xiàn)對(duì)配置文件中的敏感信息進(jìn)行加密,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
 

本文實(shí)例講述了C#為配置文件加密的實(shí)現(xiàn)方法,分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:

一般來說,在web.config或app.config文件里我們經(jīng)常會(huì)存儲(chǔ)一些敏感信息,比如connectionStrings或者appSettings,比如像下面的文件。

 

復(fù)制代碼代碼如下:
<?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>

 

 

復(fù)制代碼代碼如下:
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為我們提供了一個(gè)工具aspnet_regiis.exe來對(duì)web.config文件中的敏感信息進(jìn)行加密(app.config文件可以先改名為web.config,加密后再改回app.config)。你可以使用兩個(gè)provider中的一個(gè)來進(jìn)行加密:

System.Configuration.DPAPIProtectedConfigurationProvider:在System.Configuration.dll中,使用Windows DPAPI(Data Protection API)來進(jìn)行加密,密鑰存在Windows Local Security Authority(LSA)中。

注意:當(dāng)使用DPAPIProtectedConfigurationProvider時(shí),加密文件所使用的帳號(hào)需要與運(yùn)行web application的帳號(hào)相同,否則web application無法解密加密的內(nèi)容。
System.Configuration.RSAProtectedConfigurationProvider:在System.Configuration.dll中,使用RSA算法來進(jìn)行加密(RSA算法是非對(duì)稱加密,具體可參見前面一篇文章C#對(duì)稱加密與非對(duì)稱加密),公鑰存放在config文件當(dāng)中,只有加密的計(jì)算機(jī)有密鑰。RSAProtectedConfigurationProvider通常是默認(rèn)的缺省provider。

二、加密文件的命令

加密web.config文件可以使用:

 

復(fù)制代碼代碼如下:
aspnet_regiis -pef section web-app-physical-dir

 

Encrypt the configuration section. Optional arguments:

[-prov provider] Use this provider to encrypt.

 

比如運(yùn)行下面的命令就會(huì)分別對(duì)connectionStrings和appSettings中的信息進(jìn)行加密:

 

復(fù)制代碼代碼如下:
aspnet_regiis.exe -pef "connectionStrings" "C:/myweb/HelloService"

 

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

 

C#為配置文件加密的實(shí)現(xiàn)方法

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

 

復(fù)制代碼代碼如下:
<?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是默認(rèn)的缺省provider,如果想使用DPAPIProtectedConfigurationProvider,可以用-prov參數(shù)指明:

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

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

加密配置文件后,源程序不需要做任何改動(dòng)。如果要修改或添加新的配置信息,需要先解密配置文件。不論使用哪種Provider,都只能在進(jìn)行加密的計(jì)算機(jī)上對(duì)配置文件進(jìn)行解密。

三、解密文件的命令

解密的命令如下(解密命令不需要-prov參數(shù)):

 

復(fù)制代碼代碼如下:
-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"

 

C#為配置文件加密的實(shí)現(xiàn)方法

四、總結(jié)

配置文件中經(jīng)常會(huì)有用戶名密碼的敏感信息,為了防止該信息泄露,需要對(duì)配置文件進(jìn)行加密。加密與解密可以使用.NET提供的工具aspnet_regiis.exe,可以在Windows .NET的文件夾中找到它。

該工具只對(duì)web.config文件進(jìn)行修改,如果要加密或解密app.config,可以先將app.config文件改名為web.config,加密或解密后再改回來。

希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 卢湾区| 江口县| 邹城市| 常宁市| 固安县| 泰和县| 山阳县| 密云县| 托克托县| 民和| 正定县| 嘉黎县| 石屏县| 海宁市| 遂平县| 宜都市| 渝北区| 阿城市| 沭阳县| 罗田县| 和顺县| 革吉县| 彭州市| 凤台县| 自治县| 六安市| 德惠市| 浮梁县| 肃宁县| 根河市| 鹤峰县| 香港| 资溪县| 海伦市| 临清市| 万宁市| 永仁县| 北流市| 涪陵区| 大兴区| 淮阳县|