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

时间:2022-11-25 05:31:06 

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;
//将加密方式定义一下。主要是为了使用方便。

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


        ///
        /// 以DPAPI方式加密Config
        ///
        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/2W89HUIq0LeJ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herData>
    <EncryptedData>
  <connectionStrings>
 

 

对使用DPAPI加密的数据解密


        ///
        /// 解密DPAPI
        ///
        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加密数据(无需解密)


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


使用RSA加密


        ///
        /// 以RSA方式加密Config
        ///
        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 <KeyKeyName>
          <KeyInfo>
          <CipherData>
            <CipherValue>CJIkulw6qBtLeY5MJ9bs1ROpF1l3f4ulRzKnd6ZXN6XyG9O+b6Hr52ijK1AL9/+nsBseAPfdKDGaX/SKlJYwgzHhhi9sBrDBJ10dJcSnuGuWpI5zSLc+QHdpV0Z4iJTw83jmRDb9eFCX7aG60qWl52ofeqlI/ps1HsOjlKPSv8M=CipherValue>
          <CipherData>
        <EncryptedKey>
      <KeyInfo>
      <CipherData>
        <CipherValue>y1aEM/BRwcwZXWeuLe9mbakU8AuI7CpElrjoJgQEfzaoZXq7uEJspQAxJyDIYmCF4EgjKhE7pY6WBRAjRaBBODxxEQHGJ8I1+T554H8zosZ2InO43h5X0ZjCmvAWxNbEq1rP9DnuTcHEYqrw70nNShf79W6e2fmUF1DoVpwYNWMLeHJCP7ZkZg==CipherValue>
      <CipherData>
    <EncryptedData>
  <appSettings>

解密RSA加密数据


        ///
        /// 解密Rsa
        ///
        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加密数据(无需解密)


        ///
        /// 取得加密后的数据
        ///
        private void GetEncryptWebConfigByRsa()
        {
            string cncryptConnection = WebConfigurationManager.AppSettings["EricTest"];
            string sqlExpressConnection = WebConfigurationManager.AppSettings["Encrypt"];
        }

标签:Web.Config,加密,解密
0
投稿

猜你喜欢

  • Android实现探探图片滑动效果

    2022-11-14 17:09:19
  • 基于Java实现获取本地IP地址和主机名

    2023-03-19 04:03:55
  • Spring容器注册组件实现过程解析

    2023-07-10 11:08:07
  • RocketMQ生产者如何规避故障Broker方式详解

    2022-06-23 04:36:10
  • Android使用shape使组件呈现出特殊效果的方法

    2022-06-28 03:42:27
  • java HttpClient传输json格式的参数实例讲解

    2023-08-08 13:21:26
  • Java构造方法和方法重载详解

    2021-06-06 13:23:00
  • C#判断页面中的多个文本框输入值是否有重复的实现方法

    2022-10-30 19:41:56
  • Java接口返回省市区树形结构的实现

    2021-10-16 05:07:05
  • java实现在性能测试中进行业务验证实例

    2022-10-15 09:50:11
  • C语言进制转换代码分享

    2021-10-16 15:39:23
  • java实战之桌球小游戏

    2022-04-22 20:40:13
  • Java 图片与byte数组互相转换实例

    2023-06-24 03:28:39
  • c#添加图片、文本水印到PDF文件

    2021-08-30 13:36:57
  • Java中的Kotlin 内部类原理

    2021-10-26 23:02:13
  • 详解SpringBoot初始教程之Tomcat、Https配置以及Jetty优化

    2023-10-25 23:34:02
  • 详解Alibaba Java诊断工具Arthas查看Dubbo动态代理类

    2021-08-04 03:16:28
  • 使用IDEA开发配置Java Web的初始化过程

    2022-09-25 16:33:38
  • SpringBoot整合Swagger的方法示例

    2021-05-29 02:42:16
  • MyBatis一二级缓存

    2021-07-03 13:01:59
  • asp之家 软件编程 m.aspxhome.com