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

 更新时间:2013年04月24日 17:23:30   作者:  
C#中Web.Config加密与解密的方法,需要的朋友可以参考一下

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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APAO59pmMZ7gJ67ho1Mxjg9NTuAh/lG5XI+phDRzWcNRmjv2ZrUhz8eWIgCMoIG7NviBnbmCeT4K8pXUw==CipherValue>
      <CipherData>
    <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"];
        }

相关文章

  • C# 读写XML文件实例代码

    C# 读写XML文件实例代码

    在本篇文章里小编给大家整理的是关于C# 读写XML文件最简单方法,需要的朋友们可以跟着学习参考下。
    2020-03-03
  • C# Winform实现导出DataGridView当前页以及全部数据

    C# Winform实现导出DataGridView当前页以及全部数据

    基本上,所有的业务系统都会要求有导出的功能,所以这篇文章主要为大家介绍了如何利用Winform实现原生DataGridView的导出功能,需要的可以参考一下
    2023-07-07
  • C#窗口实现单例模式的方法

    C#窗口实现单例模式的方法

    本文介绍了C#窗口实现单例模式的方法,对于一个软件如果第二次打开程序,就把已经启动的那个进程的窗口放到最前端显示,需要了解的朋友可以参考下
    2015-07-07
  • WPF利用ScottPlot实现动态绘制图像

    WPF利用ScottPlot实现动态绘制图像

    ScottPlot是基于.Net的一款开源免费的交互式可视化库,支持Winform和WPF等UI框架,本文主要为大家详细介绍了如何WPF如何使用ScottPlot实现动态绘制图像,需要的可以参考下
    2023-12-12
  • 解析C# Console 控制台为什么也会卡死(原因分析)

    解析C# Console 控制台为什么也会卡死(原因分析)

    在分析旅程中,总会有几例控制台的意外卡死导致的生产事故,有经验的朋友都知道,控制台卡死一般是动了快速编辑窗口的缘故,虽然知道缘由,但一直没有时间探究底层原理,市面上也没有对这块的底层原理介绍,昨天花了点时间简单探究了下,感兴趣的朋友一起看看吧
    2023-10-10
  • C#实现Zip压缩目录中所有文件的方法

    C#实现Zip压缩目录中所有文件的方法

    这篇文章主要介绍了C#实现Zip压缩目录中所有文件的方法,涉及C#针对文件的读写与zip压缩相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-07-07
  • unity 实现摄像机绕某点旋转一周

    unity 实现摄像机绕某点旋转一周

    这篇文章主要介绍了unity 实现摄像机绕某点旋转一周,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-04-04
  • C#生成Code39条形码而非条形码字体的方法

    C#生成Code39条形码而非条形码字体的方法

    由于Code39编译简单、能够对任意长度的数据进行编码、支持设备比较广泛所以被广泛的采用,下面介绍下C#生成Code39条形码而非条形码字体的方法,需要的朋友可以参考下
    2015-07-07
  • 新手小白用C# winform 读取Excel表的实现

    新手小白用C# winform 读取Excel表的实现

    这篇文章主要介绍了新手小白用C# winform 读取Excel表的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-01-01
  • C#实现读取指定盘符硬盘序列号的方法

    C#实现读取指定盘符硬盘序列号的方法

    这篇文章主要介绍了C#实现读取指定盘符硬盘序列号的方法,涉及C#针对硬件属性的相关操作技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2016-08-08

最新评论