阅读目录

  一:我们为什么要对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/>配置节

  

  

加密解密技术—Web.config加密和解密的更多相关文章

  1. 对web.config加密,和解密码详细说明

    可以使用受保护配置来加密 Web 应用程序配置文件(如 Web.config 文件)中的敏感信息(包括用户名和密码.数据库连接字符串和加密密钥).对配置信息进行加密后,即使攻击者获取了对配置文件的访问 ...

  2. 创建自己的RSA密钥来保护web.config 加密数据库连接字符串

    通过创建自己的RSA密钥来保护web.config1创建RSA密钥:C:\Windows\Microsoft.NET\Framework64\v4.0.30319>aspnet_regiis - ...

  3. 【转】Encrypt ConnectionString in Web.Config 【加密ASP.NET web.config数据库链接字串】

    原文链接:https://www.codeproject.com/Tips/795135/Encrypt-ConnectionString-in-Web-Config web.config中一般会存放 ...

  4. web.config 加密/解密

    (Aspnet_regiis.exe) 这样的一个工具,可以对站点的.config文件的节进行加密 方法: #> 加密:aspnet_regiis -pef "加密的web.confi ...

  5. Web.config加密和解密

    在系统部署的时候,大家都会遇到关于用户凭证的安全性问题,而对于数据库连接的相关的信息,有些时候客户也需要我们对其加密,防止信息泄露,在此将加密和解的方法记录于此: 首先用管理员的权限启动cmd命令窗口 ...

  6. Web.Config加密与解密

    可以使用受保护配置来加密 Web 应用程序配置文件(如 Web.config 文件)中的敏感信息(包括用户名和密码.数据库连接字符串和加密密钥).对配置信息进行加密后,即使攻击者获取了对配置文件的访问 ...

  7. 说一说ASP.NET web.config 加密及解密方法 (代码)

    /// <summary> /// 保护web.config的加密和解密 /// </summary> public class ProtectHelper { /// < ...

  8. web.config 加密/解密 正确版

    一般加密方式: 利用Aspnet_regiis: cd C:\Windows\Microsoft.NET\Framework\v4.0.30319 aspnet_regiis -pe "co ...

  9. win10 IIS web.config加密不能访问:打不开 RSA 密钥容器

    C:\ProgramData\Microsoft\Crypto\RSA\MachineKeys 找到密钥文件, 根据时间判断具体是哪一个文件,赋予network service读权限

随机推荐

  1. unity读取Sqlite数据库

    using UnityEngine; using System.Collections; using Mono.Data.Sqlite; using System.Data; public enum ...

  2. css background-position (图片裁取)

    语法:background-position : length || length background-position : position || position 取值:length  : 百分 ...

  3. C++小知识之Vector排序

    // sort algorithm example #include <iostream>     // std::cout #include <algorithm>    / ...

  4. 动态include与静态include的区别

    搬一下以前写的 个人总结: 动态INCLUDE 用jsp:include动作实现 <jsp:include page="included.jsp" flush="t ...

  5. javascript 多行字符串

    javascript 字符串多行 平时一般使用 字符串+,或者[].join('')的方式 同事推荐了这样的形式 ExceptionDivHtml="<div class='gameI ...

  6. Linux中的那些英文缩写和她的含义们

    系统 man: Manual 意思是手册,可以用这个命令查询其他命令的用法. pwd:Print working directory 打印工作路径. su:Swith user 切换用户,切换到roo ...

  7. 获取IP地址(简单实现)

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/socket ...

  8. cdn与http缓存

    http缓存与cdn相关技术   摘要:最近要做这个主题的组内分享,所以准备了一个星期,查了比较多的资料.准备的过程虽然很烦很耗时间,不过因为需要查很多的资料,因此整个过程下来,对这方面的知识影响更加 ...

  9. 历峰集团3.43亿美元收购Net-a-Porter剩余股权_财经_腾讯网

    历峰集团3.43亿美元收购Net-a-Porter剩余股权_财经_腾讯网 历峰集团3.43亿美元收购Net-a-Porter剩余股权

  10. POJ 3831 &amp; HDU 3264 Open-air shopping malls(几何)

    题目链接: POJ:id=3831" target="_blank">http://poj.org/problem?id=3831 HDU:http://acm.h ...