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+sBAAAAPCENeNbVhU6C+bx4E8qcPAQAAAACAAAAAAAQZgAAAAEAACAAAADiE56Y0pGCoKEpOvxMVmMYO3tMqI/2W89HUIq0LeJAegAAAAAOgAAAAAIAACAAAACYuFOjNtk1iprbV91mmP8aCIULLZvRHAPwbAvoHvtXpKACAABP0/YOt/B8IG/eLnaxrDVCXPq6l8McVOvpL0hV4507VEpJb6FyRoM9c5TI6iIF6Jz8GQfnfQiF4P27RLyvvvu/R9KpuwDsZ0IKjpe47Nt/q/qOLlQx6vhQVE8yAjJ64DrujH6wjS2XdZSC03Co4u9OG/YdJX9zkpjVMNW8cx5FFklYmIzHxWx+b1ZFtZMu0CA8lzU4slkTBFmE3JMMa4KqC6EGedDXD3z53QkBt3KISWt1lM5ulPeQ8rfR7qrzUEWQsgaGLuNTJvCDwlPJWbZVzQaOHo71/epQRPHgvmNAkK1/hRqwXr0uMF9K6HNKCr0NDLFECLHcjCC4zR6QhhWdWT8FHPm2Zg2yucekeHQsrbiWtjZqg/DdyVPLWqmEdj82T1Gm9u9xhDHuNLpOT1FXy7bGjjok9TW1MxbWIXQ7bBih0mUwmvESD8aZGdxoH0XEFyy3flY2hjwszG4Opg3Qmz1/S0x6Sbz1vJJL7rk7FTdG3PwMDFvcvKlmmDZQTkM3SqplazwrjYI4IJBnIAL/uBxwMdxO515lWS55dDkdnx5r8HtGoeCN+cw5qFW8xxRPRsQKg6Sgti1GF2KzezZ5WJegN0hqUs18XoEpzCuuALbzHqRNBswwn0/GfdadxfwdNxoTHdJ+cQC3vSLk5f02pTW9CFZWDn30AFjIpMtArNltppLvWAP1YxtKMtyzjmv7iiIOsMtHFICTJAzO7FeTc+YToifu/wddPESZQB2MlrefnUK+cBkoSzAusfhtqUWfhblv6JnEq5A/PdohEkSu0dn2pC6AeqoG/Yngb6BJzpRFxssDfIkDH6LfXdo4s5WJXJx7VQNqUo4mmTKoUcp6DGmoogZqbHODL3MbgKFQyjdvXV9+4Aa9qOlHbcKDL5tAAAAAChj0UAPAO59pmMZ7gJ67ho1Mxjg9NTuAh/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"];
}
相关文章
- 有时为了网站安全和版权问题,会对自己写的php源码进行加密,在php加密技术上最常用的是zend公司的zend guard 加密软件,现在我们来图文讲解一下。 下面就简单说说如何...2016-11-25
- 这篇文章主要介绍了vue接口请求加密实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-08-12
- 关于AES加密的算法我们就不说了,这里主要给各位演示了三个关于AES算法实现的加密例子,希望本文章能给你带来帮助。 话不多说,先放上代码,一共有两个文件:AES.php(aes算...2016-11-25
- 这次文章要给大家介绍的是node.JS md5加密中文与php结果不一致怎么办,不知道具体解决办法的下面跟小编一起来看看。 因项目需要,需要Node.js与PHP做接口调用,发现nod...2017-07-06
- 下面来给大家分享两个关于php威盾解密的例子,一个是批量解密一个是超级算法的解密都非常的好,大家有举的进入参考。 例子,批量解密 代码如下 复制代码 ...2016-11-25
- 使用require('crypto')调用加密模块。加密模块需要底层系统提供OpenSSL的支持。它提供了一种安全凭证的封装方式,可以用于HTTPS安全网络以及普通HTTP连接。该模块还提供了一套针对OpenSSL的hash(哈希),hmac(密钥哈希),cipher...2014-06-07
- 分享一个PHP加密解密的函数,此函数实现了对部分变量值的加密的功能。 加密代码如下: /* *功能:对字符串进行加密处理 *参数一:需要加密的内容 *参数二:密钥 */ function passport_encrypt($str,$key){ //加密函数 srand(...2015-10-30
- 对数据加密分两种,一种是对数据库本身进行加密,另一种是对数据表中的数据进行加密,下面通过本文给大家介绍C#连接加密的Sqlite数据库的方法,感兴趣的朋友一起看看吧...2020-06-25
- 这篇文章主要介绍了C#实现对文件进行加密解密的方法,涉及C#加密与解密的技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
- 这篇文章主要介绍了C# 获取硬盘号,CPU信息,加密解密技术的步骤,帮助大家更好的理解和学习c#,感兴趣的朋友可以了解下...2021-01-16
- 若a=b ^ c;则b=a ^ c (^是异或的意思),php在处理异或的字符时先把字符转化为二进制的ascii值,对这些值进行异或,获取结果后在将ascii值转化为字符...2013-09-26
- 复制代码 代码如下:<?php/***功能:对字符串进行加密处理*参数一:需要加密的内容*参数二:密钥*/function passport_encrypt($str,$key){ //加密函数srand((double)microtime() * 1000000);$encrypt_key=md5(rand(0, 32000...2014-06-07
- 这篇文章主要介绍了go语言使用RC4加密的方法,实例分析了RC4加密的技巧与实现方法,具有一定参考借鉴价值,需要的朋友可以参考下...2020-05-07
- 这篇文章主要介绍了C#为配置文件加密的实现方法,可实现对配置文件中的敏感信息进行加密,非常具有实用价值,需要的朋友可以参考下...2020-06-25
- 这篇文章主要介绍了md5加密,加密结果可以为32位、48位、64位,只要修改一下参数就可以实现...2020-06-25
- 本文给大家汇总介绍了几种C#编写的Base64加密和解密类的代码,从简单到复杂,都能够实现功能,有需要的小伙伴根据自己的项目需求参考下吧。...2020-06-25
- 这篇文章主要介绍了C#实现的MD5加密功能与用法,结合实例形式分析了C# MD5加密类的定义与使用方法,需要的朋友可以参考下...2020-06-25
- C#实现对AES加密和解密的方法,需要的朋友可以参考一下...2020-06-25
- 这篇文章主要介绍了微信小程序 MD5加密登录密码详解及实例代码的相关资料,这里附有实例代码,需要的朋友可以参考下...2017-01-16
- 这篇文章主要介绍了C# 实现对PPT文档加密、解密及重置密码的操作方法,非常不错,具有参考借鉴价值,需要的朋友可以参考下...2020-06-25