<span id="vmaw3"></span>
  • <mark id="vmaw3"></mark>

    <small id="vmaw3"><dfn id="vmaw3"></dfn></small>
      <meter id="vmaw3"></meter>
       
      設為首頁 加入收藏
      首 頁 企業簡介 項目案例 軟件定制 行業軟件 解決方案 企業資訊 服務專區 客服中心
      服務項目
      案例展示 更多 >>
      ·維修管理系統 2012/5/14
      ·園區游客服務管理系統 2012/5/10
      ·黨風廉政信息公開平臺 2012/5/10
      ·職工提案系統軟件 2012/1/11
      ·電信客戶資源管理系統軟… 2012/1/11
      ·洗衣收銀軟件 2012/1/11
      ·觸摸查詢系統軟件 2012/1/11
      ·西安來電彈屏軟件 2011/8/31
      ·西安軟件公司倉庫管理軟… 2011/6/9
      ·會員管理系統 2011/6/9
      聯系人:李先生
      電  話:029-87878512
      手  機:13468700578
      地  址:西安市歐亞大道絲路國際創意夢工廠4號樓
      在線咨詢:  762176615
      Email:junsoft@126.com
       
      當前的位置 >> 返回首頁 >> 解決方案
      Web.config加密——使用編程方式方式
      發布者:西安軟件公司   發布時間:2012/8/23   閱讀:16次

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

      <!--應用程序設置值-->
      <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>

      首先引用使用空間

      using System.Configuration;
      using System.Web.Configuration;

      將加密方式定義一下。主要是為了使用方便。

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

      使用DPAPI加密

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

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

      加密前后的數據對比

      <!--加密前-->
      <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>

      -------------------------
      <!--加密后-->
      <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>

      對使用DPAPI加密的數據解密

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

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

      調用DPAPI加密數據(無需解密)

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

      使用RSA加密

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

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

      加密前后數據對比:

      <!--加密前-->
      <appSettings>
      <add key="EricTest" value="EricTest"/>
      <add key="Encrypt" value="Encrypt value"/>
      </appSettings>
      ------------------------------
      <!--加密后-->
      <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>CJIkulw6qBtLeY5MJ9bs1ROpF1l3f4ulRzKnd6ZXN6XyG9O+b6Hr52ijK1AL9/+nsBseAPfdKDGaX/SKlJYwgzHhhi9sBrDBJ10dJcSnuGuWpI5zSLc+QHdpV0Z4iJTw83jmRDb9eFCX7aG60qWl52ofeqlI/ps1HsOjlKPSv8M=</CipherValue>
      </CipherData>
      </EncryptedKey>
      </KeyInfo>
      <CipherData>
      <CipherValue>y1aEM/BRwcwZXWeuLe9mbakU8AuI7CpElrjoJgQEfzaoZXq7uEJspQAxJyDIYmCF4EgjKhE7pY6WBRAjRaBBODxxEQHGJ8I1+T554H8zosZ2InO43h5X0ZjCmvAWxNbEq1rP9DnuTcHEYqrw70nNShf79W6e2fmUF1DoVpwYNWMLeHJCP7ZkZg==</CipherValue>
      </CipherData>
      </EncryptedData>
      </appSettings>

      解密RSA加密數據

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

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

      調用使用RSA加密數據(無需解密)

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

      網站首頁 | 關于我們 | 售后服務 | 網站地圖 | 查看留言 | 在線留言 | 客服中心
      © 版權所有:西安潤宇軟件科技有限公司 
      公司地址:西安市絲路國際創意夢工廠4號樓  聯系電話:029-87878512 手機:13468700578 聯系人:李先生
      Copyright ® 2011-2020 Xbwbw.com Inc. All Rights Reserved 
      技術支持:西安潤宇軟件科技有限公司  陜ICP備11000720號-2
      亚洲高清最新av网站