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/2W89HUIq0LeJAegAAAAAOgAAAAAIAACAAAACYuFOjNtk1iprbV91mmP8aCIULLZvRHAPw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
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层完成客户端和服务端的数据交互(接受数据,发送数据),service层完成业务逻辑(注册,登录),dao层操作数
- 故障:收到服务器报警,内存使用率超过80%1.查看使用dstat和top查看内存使用最高的应用使用dstat查到内存占用最高的是java应用
- 1、Java数组介绍在Java中,数组是用来存放同一种数据类型的集合,注意只能存放同一种数据类型(Object类型数组除外)。①、数组的声明
- name hobbyTom &nb
- 通常情况下,Android实现自定义控件无非三种方式。Ⅰ、继承现有控件,对其控件的功能进行拓展。Ⅱ、将现有控件进行组合,实现功能更加强大控件
- 目录1.问题引出2.解决办法3.另外一种自定义序列化机制(介绍Externalizable)1.问题引出在某些情况下,我们可能不想对于一个对
- 微信公众号发送模版消息 背景:如下图,当用户发布需求的时候,公众号自定推送消息。例如:微信支付的时候,公众号会推送支付成功消息前提:发送模版
- Activities提供了一种方便管理的创建、保存、回复的对话框机制,例如 onCreateDialog(int), onPrepareDi
- 本文实例讲述了Golang+Android基于HttpURLConnection实现的文件上传功能。分享给大家供大家参考,具体如下:这里要演
- 本文实例为大家分享了flutter实现头部tabTop滚动栏的具体代码,供大家参考,具体内容如下效果图如下:main.dart代码如下:im
- 目录1、简单介绍2、Lambdas和Scopes3、Lambdas与局部变量4、Lambda体与局部变量5、Lambdas和'Thi
- 本文实例讲述了C#实现获取程序路径方法。分享给大家供大家参考。具体如下:获取DLL的目录:Assembly myAssembly = Ass
- 本文实例讲述了C#面向对象编程之猜拳游戏实现方法。分享给大家供大家参考。具体实现方法如下:1.需求现在要制作一个游戏,玩家与计算机进行猜拳游
- 今天群里有人问了关于仿京东App分类页面的实现,而我之前正好查过这方面的资料,手上正好有一个demo,正好分享给大家看看,个人觉得效果棒棒哒
- # 前言之前在学习C语言的时候,做过一个三子棋的小游戏,最近开始学习Java,就想着能不能用Java再把之前的练习重新实现一边,既然有这个想
- 一、单线程改造为多线程也是个技术活正如我们看到耗子叔叔博客里写的那样,原来是单线程的应用程序,”后来,我们的程序性能有问题,所以需要变成多线
- 最近在维护老项目,老项目有一个地方需要修改,就是垂直跑马灯的问题,之前的垂直跑马灯是只有文字跑马灯,新版需要加上。之前是用的MarqueeV
- 一、前期准备提示:如果友友你没有看过系列一的文章点击这个链接:王者荣耀中一个英雄是怎么被产生的?(一)我们现在功能比较多,所有为了让程序运行
- mybatis plus新增(insert)数据获取主键id在我们执行insert操作的时候,往往会需要拿到新插入数据的主键id做下一步操作