(译)利用ASP.NET加密和解密Web.config中连接字符串
介绍
这篇文章我将介绍如何利用ASP.NET来加密和解密Web.config中连接字符串
背景描述
在以前的博客中,我写了许多关于介绍 Asp.net, Gridview, SQL Server, Ajax, JavaScript等的文章。大多数情况下,我都把数据库的连接字符串放在了web.config中。其中包含许多敏感信息,包括连接数据库的用户名密码等。然而我们在web.config和machine.config中以纯文本的方式保存密码安全吗?
如果我们的程序只是部署在内部服务器中,这应该没什么问题。但如果我们的程序是运行在共享主机上面,那我们应该提高安全等级了。ASP. NET 2.0提供了一个保护配置模型来加密和解密web.config中sections信息。RSAProtectedConfigurationProvider:默认通过RSA公钥来加密和解密。
通过在命令行中工具运行aspnet_regiis.exe命令,可以对web.config中的连接串进行加密和解密。
第一种方式
首先,我们通过在windows命令行中执行aspnet_regiis.exe来加密与解密。
在VS中创建一个新的websit项目,打开web.config,加入数据库连接串,如:
然后我们按下面的步骤来加密和解密数据连接串
<connectionStrings>
<add name="dbconnection" connectionString="Data Source=RahulMittal;Integrated Security=true;Initial Catalog=MySampleDB"/>
</connectionStrings >
1. 开始菜单>>所有程序>>Microsoft visual studio 2008 >> Visual Studio Tools >> Visual Studio 2008 开发人员命令提示(如果是windows7,点右键与管理员身份运行)
2. 在命令窗口中,输入命令 aspnet_regiis.exe -pef "connectionStrings" "C:\VisualStudio2008\Authorization"
–pef表明程序是以文件系统的形式建立的。第二个“connectionStrings”是你要加密的configuration 节点名字。第三个参数指名 web.config的物理路径。
3. 成功执行命令后会显示:加密成功。
现在,再打开程序中的 web.config,会变成像下面这样子了。
<connectionStrings 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>ZNUbIEnOwlZzC8qbzHj5F2GS9gLYSkWCIgCJGkrgZAX8A+8oEIssyohhxUKvAubD3jizFc5IjbLGt7HNXhoFhXNTUPYz2y6tdKJDVgDmtCgVf8Z2C990zoMRBJG+VXhmgnlo1vtHYhGx8x/bBzE1prT1+xDpep98vHF22d+LrVI=</CipherValue>
</CipherData>
</EncryptedKey>
</KeyInfo>
<CipherData>
<CipherValue>tODWlPD0Q/B/mP14GQ/5tUxcjmhHcy9a0oPunV5osNrMQRztgi2h5V6sxJOEh+NC+G9gQNkv1huXf1s7eoZRRLy5/LDtLXzzqMUOqLSlJUs9igChvi33c9XG4rwGF15Tpn4N34bpQBt94n0rpSkQ18V9HCPzii+UO64PlA+ykDeQhc9aQr4gO3mCfUzmY2S9gsXzRbzdq0oCWBDvx8UkX2uDxaysVHC9Fo7u6IrlpU0+hOdK95Y3/A==</CipherValue>
</CipherData>
</EncryptedData>
</connectionStrings>
我们在程序中并不要写任何代码来解密连接字符串,因为.NET会自动的为我们解密。如果我们要用连接字符串,可以像平常那样调用.
string strconnection = ConfigurationManager.AppSettings["dbconnection"].ToString();
如果我们想解密,只需要在VS的命令窗口中,输入aspnet_regiis.exe -pdf "connectionStrings" "C:\VisualStudio2008\Authorization"
成功执行后,会显示解密成功。
再打开web.config,我们可以看到解密后的字符串。
现在,我们知道了如何在文件系统中加密和解密连接字符串。如果我们想加密运行在IIS上的默认网站,就像IE上展示的那样,可以用下面的命令。
加密IIS默认网站的web.config
aspnet_regiis.exe -pe "connectionStrings" -app "/SampleWebSite"
-pe说明程序是运行在IIS上的。第二个参数指名要加密的configuration节点。-app用来指定虚拟目录,最后一个参数就是程序部署的虚拟目录名。
Decrypt connectionStrings in web.config of IIS based site
解密IIS默认网站上的web.config
aspnet_regiis.exe -pd "connectionStrings" -app "/SampleWebSite"
到这里我们知道如何用命令行工具执行aspnet_regiis.exe命令来加密和解密web.config了。下面我将介绍如何在后台代码中来加密解密web.config。
第二种方式
在第二种方法中我会用RSAProtectedConfigurationProvider和DataProtectionConfgurationProvider来加密解密web.config
首先,打开Default.aspx,添加如下代码:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button id="btnEncrypt" runat="server" Text="Encrypt" onclick="btnEncrypt_Click" />
<asp:Button ID="btnDecrypt" runat="server" Text="Decrypt" onclick="btnDecrypt_Click" />
</div>
</form>
</body>
</html>
打开后台代码,添加下列命名空间:
using System;
using System.Configuration;
using System.Web.Configuration;
再添加如下代码
string provider = "RSAProtectedConfigurationProvider";
string section = "connectionStrings";
protected void Page_Load(object sender, EventArgs e)
{ }
protected void btnEncrypt_Click(object sender, EventArgs e)
{
Configuration confg = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
ConfigurationSection configSect = confg.GetSection(section);
if (configSect != null)
{
configSect.SectionInformation.ProtectSection(provider);
confg.Save();
}
} protected void btnDecrypt_Click(object sender, EventArgs e)
{
Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
ConfigurationSection configSect = config.GetSection(section);
if (configSect.SectionInformation.IsProtected)
{
configSect.SectionInformation.UnprotectSection();
config.Save();
}
}
完成之后,打开web.config,添加数据库连接字符串
<connectionStrings>
<add name="dbconnection" connectionString="Data Source=RahulMittal;Integrated Security=true;Initial Catalog=MySampleDB"/>
</connectionStrings >
现在运行程序并点击加密按钮之后,再打开web.config,会变成下面那样:
<connectionStrings 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>WagJ9DDjWTNc1nmYVNQXaQqXalQzXaiCHAOtUJvTWBRZiuT6UK1fBElM80PnL6dC5Umb8qvfHdkSMgoMW9CJzwOTZ0zTy17JBGZqRQmlfW2G9LacoWIil0UrxjhgmJmRXhwXHFpdGwEVl7AoQGVlJGabXuChutaTxmfGOoUbCr0=</CipherValue>
</CipherData>
</EncryptedKey>
</KeyInfo>
<CipherData>
<CipherValue>qry5qnr3qxOgyoNPeP7OKEiHpr/PPTsaeQ2mYUsSK7cg4Kkl9uPO4RyUXgBIkgCTsjbObqLlyndcSBnYyek6bxG/IBL82G1R5J1ci8i1eyt8kIDqouzYOx5vtouErld4z1L+7WGf9Wg37QAH5RiiEfkCHndJJq3dTqjxnnXZSno6NgbxSXDfqzwE/eKDVhGV3oaTQSfjVmO8e5a9wvREYeeyasDhojx8J2mdy7/Q9rEIpv98RTiRxA==</CipherValue>
</CipherData>
</EncryptedData>
</connectionStrings>
如果我们想用DataProtectionConfigurationProvider来实现加密与解密,只需在代码中将RSAProtectedConfigurationProvider替换成DataProtectionConfigurationProvider即可。
原文:http://www.codeproject.com/Tips/304638/Encrypt-or-Decrypt-Connection-Strings-in-web-confi
(译)利用ASP.NET加密和解密Web.config中连接字符串的更多相关文章
- 利用ASP.NET加密和解密Web.config中连接字符串
摘自:博客园 介绍 这篇文章我将介绍如何利用ASP.NET来加密和解密Web.config中连接字符串 背景描述 在以前的博客中,我写了许多关于介绍 Asp.net, Gridview, SQL Se ...
- web.config中连接字符串的读写和加密解密
转载:https://www.cnblogs.com/shuai/articles/2248703.html 1.先来看看如何在web.config中写入数据库连接字符串.打开web.config文件 ...
- 【转载】两个Web.config中连接字符串中特殊字符解决方案
userid = test password = aps'"; 那么连接字符串的写法为: Provider=SQLOLEDB.1;Password="aps'"&quo ...
- 命令行工具aspnet_regiis.exe实现加密和解密web.config
命令行工具aspnet_regiis.exe,是一个类似于DOS的命令工具,称之为命令解释器.使用命令行工具加密和解密web.config文件中的数据库连接字符串时,只需要简单的语法命令即可. 加密语 ...
- ASP.NET web.config中数据库连接字符串connectionStrings节的配置方法
ASP.NET web.config中数据库连接字符串connectionStrings节的配置方法 第一种情况,本地开发时,使用本地数据库,如下面的代码 <connectionStrings& ...
- ASP.NET MVC系列:web.config中ConnectionString aspnet_iis加密与AppSettings独立文件
1. web.config中ConnectionString aspnet_iis加密 web.config路径:E:\Projects\Libing.Web\web.config <conne ...
- 加密,解密web.config数据库连接字符串
"connectionStrings" 路径是web.config所在的工程目录. 1.加密EncryptWebConfig.bat @echo offC:\Windows\Mic ...
- Web.Config中配置字符串含引号的处理
配置文件中往往要用到一些特殊的字符, Web.Config默认编码格式为UTF-8,对于XML文件,要用到实体转义码来替换.对应关系如下: 字符 转义码 & 符号 & & 单引 ...
- 不同数据库下的web.config中数据库连接字符串
<connectionStrings> <add name="OADBConnectionString" connectionString="Data ...
随机推荐
- [译]Mongoose指南 - Population
MongoDB没有join, 但是有的时候我们需要引用其它collection的documents, 这个时候就需要populate了. 我们可以populate单个document, 多个docum ...
- Linq→join中指定多个条件
还是习惯先撸一段SQL * FROM User_Pic P AND P.Guid = R.UserPicGuid ORDER BY PicSize DESC 然后发现Linq中的join不能多条件.. ...
- Oracle11安装
图片上传失败,重新编辑 1.选择安装目录,一般设置数据库口令为system 2.环境检查 3.注册页面,直接下一步 4.点击安装按钮 5.进入安装界面 6.等待 7.直到出现下面界面,点击口令管理 8 ...
- MYSQL基础语句
参考书籍< MySQL数据库基础与实例教程> --孔祥盛 SQL(structured query language)结构化查询语言,应用最为广泛的关系型数据库语言. MYSQL属于关系型 ...
- javascript高级程序设计---Event对象三
进度事件 进度事件用来描述一个事件进展的过程,比如XMLHttpRequest对象发出的HTTP请求的过程.<img>.<audio>.<video>.<st ...
- HDU 5384 字典树、AC自动机
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5384 用字典树.AC自动机两种做法都可以做 #include<stdio.h> #includ ...
- 自定义Listview
public class MyListView extends ListView { public MyListView(Context context) { super(context); } pu ...
- python解释器快捷键
13. 交互式输入的编辑和历史记录 某些版本的 Python 解释器支持编辑当前的输入行和历史记录,类似于在 Korn shell 和 GNU Bash shell 中看到的功能.这是使用GNU Re ...
- [HDU3709]Balanced Number
[HDU3709]Balanced Number 试题描述 A balanced number is a non-negative integer that can be balanced if a ...
- ios中二维码的使用之一: 二维码的生成
iOS在7之后,具备了原生的二维码生成API; 生成二维码的准备: #import <CoreImage/CoreImage.h> 导入框架: 开始生成: //1- 创建过滤器 CIFi ...