欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#中Web.Config加密與解密的方法

 更新時(shí)間:2013年04月24日 17:23:30   作者:  
C#中Web.Config加密與解密的方法,需要的朋友可以參考一下

Web.Config,其中一部分配置如下:

復(fù)制代碼 代碼如下:

  <appSettings>
    <add key="EricTest" value="EricTest"/>
    <add key="Encrypt" value="Encrypt value"/>
  <appSettings>

  <connectionStrings >
    <add name="EncryptConnection" connectionString="Data Source=.\SQL2000;Initial Catalog=Northwind;user id=sa;password=test"/>
    <add name="SQLExpress" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=myDB;user id=sa;password=test"
     providerName="System.Data.SqlClient" />
  <connectionStrings>
 

在加密前,先做一些準(zhǔn)備工作。

首先引用使用空間

復(fù)制代碼 代碼如下:

using System.Configuration;
using System.Web.Configuration;
//將加密方式定義一下。主要是為了使用方便。

        ///
        /// 加密方式
        ///
        public enum EncryptType
        {
            DataProtectionConfigurationProvider,
            RSAProtectedConfigurationProvider
        }
 


使用DPAPI加密
復(fù)制代碼 代碼如下:

        ///
        /// 以DPAPI方式加密Config
        ///
        private void EncryptWebConfigByDPAPI()
        {
            Configuration configuration = null;
            ConfigurationSection connectionSection = null;

            //打開Request所在路徑網(wǎng)站的Web.config文件
            configuration = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
            //取得Web.config中connectionStrings設(shè)置區(qū)塊
            connectionSection = configuration.GetSection("connectionStrings");
            //未加密時(shí)
            if (!connectionSection.SectionInformation.IsProtected)
            {
                connectionSection.SectionInformation.ProtectSection(EncryptType.DataProtectionConfigurationProvider.ToString());
                configuration.Save();
            }
        }


加密前后的數(shù)據(jù)對(duì)比
復(fù)制代碼 代碼如下:

  <connectionStrings >
    <add name="EncryptConnection" connectionString="Data Source=.\SQL2000;Initial Catalog=Northwind;user id=sa;password=test"/>
    <add name="SQLExpress" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=myDB;user id=sa;password=test"
     providerName="System.Data.SqlClient" />
  <connectionStrings>

復(fù)制代碼 代碼如下:

  <connectionStrings configProtectionProvider="DataProtectionConfigurationProvider">
    <EncryptedData>
      <CipherData>
        <CipherValue>AQAAANCMnd8BFdERjHoAwE/Cl+sBAAAAPCENeNbVhU6C+bx4E8qcPAQAAAACAAAAAAAQZgAAAAEAACAAAADiE56Y0pGCoKEpOvxMVmMYO3tMqI/2W89HUIq0LeJAegAAAAAOgAAAAAIAACAAAACYuFOjNtk1iprbV91mmP8aCIULLZvRHAPwbAvoHvtXpKACAABP0/YOt/B8IG/eLnaxrDVCXPq6l8McVOvpL0hV4507VEpJb6FyRoM9c5TI6iIF6Jz8GQfnfQiF4P27RLyvvvu/R9KpuwDsZ0IKjpe47Nt/q/qOLlQx6vhQVE8yAjJ64DrujH6wjS2XdZSC03Co4u9OG/YdJX9zkpjVMNW8cx5FFklYmIzHxWx+b1ZFtZMu0CA8lzU4slkTBFmE3JMMa4KqC6EGedDXD3z53QkBt3KISWt1lM5ulPeQ8rfR7qrzUEWQsgaGLuNTJvCDwlPJWbZVzQaOHo71/epQRPHgvmNAkK1/hRqwXr0uMF9K6HNKCr0NDLFECLHcjCC4zR6QhhWdWT8FHPm2Zg2yucekeHQsrbiWtjZqg/DdyVPLWqmEdj82T1Gm9u9xhDHuNLpOT1FXy7bGjjok9TW1MxbWIXQ7bBih0mUwmvESD8aZGdxoH0XEFyy3flY2hjwszG4Opg3Qmz1/S0x6Sbz1vJJL7rk7FTdG3PwMDFvcvKlmmDZQTkM3SqplazwrjYI4IJBnIAL/uBxwMdxO515lWS55dDkdnx5r8HtGoeCN+cw5qFW8xxRPRsQKg6Sgti1GF2KzezZ5WJegN0hqUs18XoEpzCuuALbzHqRNBswwn0/GfdadxfwdNxoTHdJ+cQC3vSLk5f02pTW9CFZWDn30AFjIpMtArNltppLvWAP1YxtKMtyzjmv7iiIOsMtHFICTJAzO7FeTc+YToifu/wddPESZQB2MlrefnUK+cBkoSzAusfhtqUWfhblv6JnEq5A/PdohEkSu0dn2pC6AeqoG/Yngb6BJzpRFxssDfIkDH6LfXdo4s5WJXJx7VQNqUo4mmTKoUcp6DGmoogZqbHODL3MbgKFQyjdvXV9+4Aa9qOlHbcKDL5tAAAAAChj0UAPAO59pmMZ7gJ67ho1Mxjg9NTuAh/lG5XI+phDRzWcNRmjv2ZrUhz8eWIgCMoIG7NviBnbmCeT4K8pXUw==CipherValue>
      <CipherData>
    <EncryptedData>
  <connectionStrings>
 

 

對(duì)使用DPAPI加密的數(shù)據(jù)解密

復(fù)制代碼 代碼如下:

        ///
        /// 解密DPAPI
        ///
        private void DecryptWebConfigByDPAPI()
        {
            Configuration configuration = null;
            ConfigurationSection connectionSection = null;

            //打開Request所在路徑網(wǎng)站的Web.config文件
            configuration = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
            //取得Web.config中connectionStrings設(shè)置區(qū)塊
            connectionSection = configuration.GetSection("connectionStrings");
            if (connectionSection.SectionInformation.IsProtected)
            {
                connectionSection.SectionInformation.UnprotectSection();
                configuration.Save();
            }
        }
 


調(diào)用DPAPI加密數(shù)據(jù)(無需解密)
復(fù)制代碼 代碼如下:

        ///
        /// 取得加密后的數(shù)據(jù)
        ///
        private void GetEncryptWebConfigByDPAPI()
        {
            string cncryptConnection = WebConfigurationManager.ConnectionStrings["EncryptConnection"].ConnectionString;
            string sqlExpressConnection = WebConfigurationManager.ConnectionStrings["SQLExpress"].ConnectionString;
        }

使用RSA加密
復(fù)制代碼 代碼如下:

        ///
        /// 以RSA方式加密Config
        ///
        private void EncryptWebConfigByRsa()
        {
            Configuration configuration = null;
            ConfigurationSection connectionSection = null;

            //打開Request所在路徑網(wǎng)站的Web.config文件
            configuration = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
            //取得Web.config中connectionStrings設(shè)置區(qū)塊
            connectionSection = configuration.GetSection("appSettings");
            //未加密時(shí)
            if (!connectionSection.SectionInformation.IsProtected)
            {
                connectionSection.SectionInformation.ProtectSection(EncryptType.RSAProtectedConfigurationProvider.ToString());
                configuration.Save();
            }
        }


加密前后數(shù)據(jù)對(duì)比:
復(fù)制代碼 代碼如下:

    <appSettings>
    <add key="EricTest" value="EricTest"/>
    <add key="Encrypt" value="Encrypt value"/>
  <appSettings>

復(fù)制代碼 代碼如下:

  <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 <KeyKeyName>
          <KeyInfo>
          <CipherData>
            <CipherValue>CJIkulw6qBtLeY5MJ9bs1ROpF1l3f4ulRzKnd6ZXN6XyG9O+b6Hr52ijK1AL9/+nsBseAPfdKDGaX/SKlJYwgzHhhi9sBrDBJ10dJcSnuGuWpI5zSLc+QHdpV0Z4iJTw83jmRDb9eFCX7aG60qWl52ofeqlI/ps1HsOjlKPSv8M=CipherValue>
          <CipherData>
        <EncryptedKey>
      <KeyInfo>
      <CipherData>
        <CipherValue>y1aEM/BRwcwZXWeuLe9mbakU8AuI7CpElrjoJgQEfzaoZXq7uEJspQAxJyDIYmCF4EgjKhE7pY6WBRAjRaBBODxxEQHGJ8I1+T554H8zosZ2InO43h5X0ZjCmvAWxNbEq1rP9DnuTcHEYqrw70nNShf79W6e2fmUF1DoVpwYNWMLeHJCP7ZkZg==CipherValue>
      <CipherData>
    <EncryptedData>
  <appSettings>

解密RSA加密數(shù)據(jù)

復(fù)制代碼 代碼如下:

        ///
        /// 解密Rsa
        ///
        private void DecryptWebConfigByRsa()
        {
            Configuration configuration = null;
            ConfigurationSection connectionSection = null;

            //打開Request所在路徑網(wǎng)站的Web.config文件
            configuration = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
            //取得Web.config中connectionStrings設(shè)置區(qū)塊
            connectionSection = configuration.GetSection("appSettings");
            if (connectionSection.SectionInformation.IsProtected)
            {
                connectionSection.SectionInformation.UnprotectSection();
                configuration.Save();
            }
        }

調(diào)用使用RSA加密數(shù)據(jù)(無需解密)

復(fù)制代碼 代碼如下:

        ///
        /// 取得加密后的數(shù)據(jù)
        ///
        private void GetEncryptWebConfigByRsa()
        {
            string cncryptConnection = WebConfigurationManager.AppSettings["EricTest"];
            string sqlExpressConnection = WebConfigurationManager.AppSettings["Encrypt"];
        }

相關(guān)文章

  • C# 讀寫XML文件實(shí)例代碼

    C# 讀寫XML文件實(shí)例代碼

    在本篇文章里小編給大家整理的是關(guān)于C# 讀寫XML文件最簡單方法,需要的朋友們可以跟著學(xué)習(xí)參考下。
    2020-03-03
  • C# Winform實(shí)現(xiàn)導(dǎo)出DataGridView當(dāng)前頁以及全部數(shù)據(jù)

    C# Winform實(shí)現(xiàn)導(dǎo)出DataGridView當(dāng)前頁以及全部數(shù)據(jù)

    基本上,所有的業(yè)務(wù)系統(tǒng)都會(huì)要求有導(dǎo)出的功能,所以這篇文章主要為大家介紹了如何利用Winform實(shí)現(xiàn)原生DataGridView的導(dǎo)出功能,需要的可以參考一下
    2023-07-07
  • C#窗口實(shí)現(xiàn)單例模式的方法

    C#窗口實(shí)現(xiàn)單例模式的方法

    本文介紹了C#窗口實(shí)現(xiàn)單例模式的方法,對(duì)于一個(gè)軟件如果第二次打開程序,就把已經(jīng)啟動(dòng)的那個(gè)進(jìn)程的窗口放到最前端顯示,需要了解的朋友可以參考下
    2015-07-07
  • WPF利用ScottPlot實(shí)現(xiàn)動(dòng)態(tài)繪制圖像

    WPF利用ScottPlot實(shí)現(xiàn)動(dòng)態(tài)繪制圖像

    ScottPlot是基于.Net的一款開源免費(fèi)的交互式可視化庫,支持Winform和WPF等UI框架,本文主要為大家詳細(xì)介紹了如何WPF如何使用ScottPlot實(shí)現(xiàn)動(dòng)態(tài)繪制圖像,需要的可以參考下
    2023-12-12
  • 解析C# Console 控制臺(tái)為什么也會(huì)卡死(原因分析)

    解析C# Console 控制臺(tái)為什么也會(huì)卡死(原因分析)

    在分析旅程中,總會(huì)有幾例控制臺(tái)的意外卡死導(dǎo)致的生產(chǎn)事故,有經(jīng)驗(yàn)的朋友都知道,控制臺(tái)卡死一般是動(dòng)了快速編輯窗口的緣故,雖然知道緣由,但一直沒有時(shí)間探究底層原理,市面上也沒有對(duì)這塊的底層原理介紹,昨天花了點(diǎn)時(shí)間簡單探究了下,感興趣的朋友一起看看吧
    2023-10-10
  • C#實(shí)現(xiàn)Zip壓縮目錄中所有文件的方法

    C#實(shí)現(xiàn)Zip壓縮目錄中所有文件的方法

    這篇文章主要介紹了C#實(shí)現(xiàn)Zip壓縮目錄中所有文件的方法,涉及C#針對(duì)文件的讀寫與zip壓縮相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-07-07
  • unity 實(shí)現(xiàn)攝像機(jī)繞某點(diǎn)旋轉(zhuǎn)一周

    unity 實(shí)現(xiàn)攝像機(jī)繞某點(diǎn)旋轉(zhuǎn)一周

    這篇文章主要介紹了unity 實(shí)現(xiàn)攝像機(jī)繞某點(diǎn)旋轉(zhuǎn)一周,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • C#生成Code39條形碼而非條形碼字體的方法

    C#生成Code39條形碼而非條形碼字體的方法

    由于Code39編譯簡單、能夠?qū)θ我忾L度的數(shù)據(jù)進(jìn)行編碼、支持設(shè)備比較廣泛所以被廣泛的采用,下面介紹下C#生成Code39條形碼而非條形碼字體的方法,需要的朋友可以參考下
    2015-07-07
  • 新手小白用C# winform 讀取Excel表的實(shí)現(xiàn)

    新手小白用C# winform 讀取Excel表的實(shí)現(xiàn)

    這篇文章主要介紹了新手小白用C# winform 讀取Excel表的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • C#實(shí)現(xiàn)讀取指定盤符硬盤序列號(hào)的方法

    C#實(shí)現(xiàn)讀取指定盤符硬盤序列號(hào)的方法

    這篇文章主要介紹了C#實(shí)現(xiàn)讀取指定盤符硬盤序列號(hào)的方法,涉及C#針對(duì)硬件屬性的相關(guān)操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2016-08-08

最新評(píng)論