zoukankan      html  css  js  c++  java
  • 加密解密技术—Web.config加密和解密

      阅读目录

      一:我们为什么要对web.config配置文件中的配置节加密?

      二:怎么样加密和解密?

      三:实例

      四:运行效果

      一:我们为什么要对web.config配置文件中的配置节加密?

      因为在我们的项目中,有的配置节可能包含敏感信息,我们看下面的<connectionStrings/>配置节中包含了我们连接数据库的用户名和密码以及IP地址,这要是暴露出去是很危险的,还有<identity/>配置节中包含了运行时使用的模拟账号的用户名和密码,这些配置节都包含着敏感信息,我们不希望密码以明文的方式存储在配置文件里,所以我们要对其进行加密

      <connectionStrings>
         <add name="LocalHostEPGConnectionStr" connectionString="server=.;database=NewNewEPG;User ID=sa;password=123" providerName="System.Data.SqlClient"/>
      </connectionStrings>

      二:怎么样加密和解密?

      使用SectionIntomation对象可以对web.config进行加密和解密

      如果要加密一个配置节,只需要调用SectionIntomation对象的ProtectSection()方法,传递一个要使用的提供程序的名字来执行加密

      如果要解密一个配置节,当需要解密文件的配置节时,只需要调用SectionIntomation对象的UnprotectSection()方法完成解密

      1:ProtectSection()方法

      此方法对web.config中的配置节进行加密

      语法如下:

      public void ProtectSection(string ProtectProvider)

      参数说明如下:
      ProtectProvider:要使用的保护提供程序的名称,默认下包含以下保护提供程序加密,这个参数必须写已存在的保护提供程序的名称,比如:“RSAProtectedConfigurationProvider”,不能写“MyName”,否则会报找不到保护提供程序“MyName”

      1.1:RSAProtectedConfigurationProvider:使用RSA加密算法对数据进行加密和解密

      1.2:DPAPIProtectedConfigurationProvider:使用Windows数据保护API(DPAPI)对数据进行加密和解密

      2:UnprotectSection()方法

      此方法对关联的配置节移除受保护的配置解密

      三:实例

      ConfigurationManager来自命名空间System.Configuration,而WebConfigurationManager来自命名空间System.Web.Configuration,微软建议:在Web应用程序配置文件的操作时建议采用WebConfigurationManager ;在客户端配置文件的操作时建议采用ConfigurationManager ,我们都得引用这两个命名空间

      我们最后看到解密后的<connectionStrings/>配置节和未加密前的配置节是一模一样的

      WebConfigEncryptDecrypt.aspx

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Web;
      using System.Web.UI;
      using System.Web.UI.WebControls;
      using System.Web.Configuration;
      using System.Configuration;

      namespace EPG.WebAdmin.EncryptDecrypt
      {
          public partial class WebConfigEncryptDecrypt : System.Web.UI.Page
          {
              protected void Page_Load(object sender, EventArgs e)
              {

              }

              /// <summary>
              /// 加密Web.config文件
              /// </summary>
              protected void btnEncrypt_Click(object sender, EventArgs e)
              {
                  //得到当前配置文件
                  Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
                  //得到节部分
                  ConfigurationSection section = config.GetSection("connectionStrings");
                  //如果节不为空并且这个节没被保护
                  if (section != null && !section.SectionInformation.IsProtected)
                  {
                      //保护指定的节使用RSA加密算法对数据进行加密和解密
                      section.SectionInformation.ProtectSection("RSAProtectedConfigurationProvider");

            //保存
                      config.Save();

            RegisterStartupScript("","<script>alert('加密成功!')</script>");
                  }
              }

         
              /// <summary>
              /// 解密Web.config文件
              /// </summary>
              protected void btnDecrypt_Click(object sender, EventArgs e)
              {
                  //得到当前配置文件
                  Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
                  //得到节部分
                  ConfigurationSection section = config.GetSection("connectionStrings");
                  //如果节不为空并且这个节被保护
                  if (section != null && section.SectionInformation.IsProtected)
                  {
                      //保护指定的节使用RSA加密算法对数据进行加密和解密
                      section.SectionInformation.UnprotectSection();
                      //保存
                      config.Save();
                      RegisterStartupScript("", "<script>alert('解密成功!')</script>");
                  }
              }

          }
      }

      四:运行效果

      界面设计

      

      未加密的<connectionStrings/>配置节

      

      加密后的<connectionStrings/>配置节

      

      解密后的<connectionStrings/>配置节

      

      

  • 相关阅读:
    Win 及 Linux 查找mac地址的方法
    Maven 手动添加selenium JAR 包到本地仓库
    Cucumber 行为驱动开发简介
    Cucumber 相关资源
    测试相关资源网站
    openstack之neutron
    Python集合操作
    openstack之nova
    操作系统
    openstack之horizon部署
  • 原文地址:https://www.cnblogs.com/menglin2010/p/2363829.html
Copyright © 2011-2022 走看看