润宇软件
首 页 企业简介 项目案例 软件定制 行业软件 解决方案 企业动态 服务专区 客服中心
业务介绍:西安软件公司、软件开发、软件定制、软件外包
软件 方案 文章
  润宇软件 >> 新闻资讯  >> 解决方案

Web.config加密——使用编程方式方式

发布时间:2015/8/23  浏览次数:23次  字体【    】

有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;

将加密方式定义一下。主要是为了使用方便。

/// <summary>
/// 加密方式
/// </summary>
public enum EncryptType
{
DataProtectionConfigurationProvider,
RSAProtectedConfigurationProvider
}

使用DPAPI加密

/// <summary>
/// 以DPAPI方式加密Config
/// </summary>
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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TKoUcp6DGmoogZqbHODL3MbgKFQyjdvXV9+4Aa9qOlHb
cKDL5tAAAAAChj0UAPAO59pmMZ7gJ67ho1Mxjg9NTuAh/lG5XI+phDRzWcNRm
jv2ZrUhz8eWIgCMoIG7NviBnbmCeT4K8pXUw==</CipherValue>
</CipherData>
</EncryptedData>
</connectionStrings>

对使用DPAPI加密的数据解密

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

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

使用RSA加密

/// <summary>
/// 以RSA方式加密Config
/// </summary>
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 Key</KeyName>
</KeyInfo>
<CipherData>
<CipherValue>CJIkulw6qBtLeY5MJ9bs1ROpF1l3f4ulRzKnd6ZXN6XyG9O+b6Hr52ijK1AL9/+nsBseAPfdKDGaX/SKlJYwgzHh
hi9sBrDBJ10dJcSnuGuWpI5zSLc+QHdpV0Z4iJTw83jmRDb9eFCX7aG60qWl52ofeqlI/ps1HsOjlKPSv8M=</CipherValue>
</CipherData>
</EncryptedKey>
</KeyInfo>
<CipherData>
<CipherValue>y1aEM/BRwcwZXWeuLe9mbakU8AuI7CpElrjoJgQEfzaoZXq7uEJspQAxJyDIYmCF4EgjKhE7pY6WBRAjRaBBODxxEQH
GJ8I1+T554H8zosZ2InO43h5X0ZjCmvAWxNbEq1rP9DnuTcHEYqrw70nNShf79W6e2fmUF1DoVpwYNWMLeHJCP7ZkZg==</CipherValue>
</CipherData>
</EncryptedData>
</appSettings>

解密RSA加密数据

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

/// <summary>
/// 取得加密后的数据
/// </summary>
private void GetEncryptWebConfigByRsa()
{
string cncryptConnection = WebConfigurationManager.AppSettings["EricTest"];
string sqlExpressConnection = WebConfigurationManager.AppSettings["Encrypt"];
}
  关闭本页
西部IT网合作伙伴 合作伙伴
陕西省 | 榆林 | 延安 | 铜川 | 渭南 | 商洛 | 宝鸡 | 汉中 | 安康 | 咸阳
网站首页 | 关于我们 | 售后服务 | 项目合同 | 查看留言 | 在线留言 | 客服中心
© 版权所有:西安润宇软件科技有限公司 
公司地址:西安市丝路国际创意梦工厂4号楼 联系电话:029-87878512 手机:13468700578 联系人:李先生
Copyright ® 2009-2020 RunYusoft.com Inc. All Rights Reserved 
技术支持:西安润宇软件科技有限公司  陕ICP备11000720号