connectionString加密
首先是加密,解密类。
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks; namespace SqlConnectionEncryp
{
public class Encrypt
{
/// <summary>
/// MD5加密
/// </summary>
public static string MD5Encrypt(string Text, string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = Encoding.Default.GetBytes(Text);
des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(, ));
des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(, ));
System.IO.MemoryStream ms = new System.IO.MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, , inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret = new StringBuilder();
foreach (byte b in ms.ToArray())
{
ret.AppendFormat("{0:X2}", b);
}
return ret.ToString();
} /// <summary>
/// MD5解密
/// </summary>
public static string MD5Decrypt(string Text, string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
int len;
len = Text.Length / ;
byte[] inputByteArray = new byte[len];
int x, i;
for (x = ; x < len; x++)
{
i = Convert.ToInt32(Text.Substring(x * , ), );
inputByteArray[x] = (byte)i;
}
des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(, ));
des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(, ));
System.IO.MemoryStream ms = new System.IO.MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, , inputByteArray.Length);
cs.FlushFinalBlock();
return Encoding.Default.GetString(ms.ToArray());
} /// <summary>
/// TripleDES加密
/// </summary>
public static string TripleDESEncrypting(string strSource)
{
try
{
byte[] bytIn = Encoding.Default.GetBytes(strSource);
byte[] key = { , , , , , , , , , , , , , , , , , , , , , , , }; //定义密钥
byte[] IV = { , , , , , , , }; //定义偏移量
TripleDESCryptoServiceProvider TripleDES = new TripleDESCryptoServiceProvider();
TripleDES.IV = IV;
TripleDES.Key = key;
ICryptoTransform encrypto = TripleDES.CreateEncryptor();
System.IO.MemoryStream ms = new System.IO.MemoryStream();
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);
cs.Write(bytIn, , bytIn.Length);
cs.FlushFinalBlock();
byte[] bytOut = ms.ToArray();
return System.Convert.ToBase64String(bytOut);
}
catch (Exception ex)
{
throw new Exception("加密时候出现错误!错误提示:\n" + ex.Message);
}
} /// <summary>
/// TripleDES解密
/// </summary>
public static string TripleDESDecrypting(string Source)
{
try
{
byte[] bytIn = System.Convert.FromBase64String(Source);
byte[] key = { , , , , , , , , , , , , , , , , , , , , , , , }; //定义密钥
byte[] IV = { , , , , , , , }; //定义偏移量
TripleDESCryptoServiceProvider TripleDES = new TripleDESCryptoServiceProvider();
TripleDES.IV = IV;
TripleDES.Key = key;
ICryptoTransform encrypto = TripleDES.CreateDecryptor();
System.IO.MemoryStream ms = new System.IO.MemoryStream(bytIn, , bytIn.Length);
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read);
StreamReader strd = new StreamReader(cs, Encoding.Default);
return strd.ReadToEnd();
}
catch (Exception ex)
{
throw new Exception("解密时候出现错误!错误提示:\n" + ex.Message);
}
}
}
}
加密使用MD5Decrypt,自己给定一个密钥。下面是对连接字符串加密的界面:

代码和测试效果:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void btnCreate_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(txtClear.Text))
{
MessageBox.Show("明文不能为空!");
}
if (string.IsNullOrEmpty(txtKey.Text))
{
MessageBox.Show("密钥不能为空!");
} string strCipher = Encrypt.MD5Encrypt(txtClear.Text, txtKey.Text);
txtCipher.Text = strCipher;
}
}

加密的原因是,为客户开发的某些程序,需要访问公司(我们自己工作的)在公网的数据库,但是我们不能将明文数据库访问字符串,存放在客户的应用程序上,最好的办法就是将其加密。下面就在一个为客户开发的程序中使用这个加密连接。这里,我将密钥和密文都写在了配置文件中。如果用户猜出我的加密算法,他们可以根据密钥,可以轻松获得我的明文。所以,不要傻到直接将密钥配置命名成key。如果将密钥写死在代码中就不方便控制,客户反编译同样能获知加密算法和密钥,从而获得本来的连接字符串。

下面在获取连接字符串时,都要对其进行解密,所以构造一个解密类是必要的。
public class ConfigHelper
{
/// <summary>
/// 获取普通连接
/// </summary>
public static string GetConn(string conn)
{
return ConfigurationManager.ConnectionStrings[conn].ConnectionString;
}
/// <summary>
/// 获取appsetting
/// </summary>
public static string GetAppSetting(string key)
{
return ConfigurationManager.AppSettings[key];
}
/// <summary>
/// 获取解密连接
/// </summary>
public static string GetConn(string conn, string key)
{
string strConn = GetConn(conn);
string strKey = GetAppSetting(key);
return MD5Decrypt(strConn, strKey);
} /// <summary>
/// MD5解密
/// </summary>
private static string MD5Decrypt(string Text, string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
int len;
len = Text.Length / ;
byte[] inputByteArray = new byte[len];
int x, i;
for (x = ; x < len; x++)
{
i = Convert.ToInt32(Text.Substring(x * , ), );
inputByteArray[x] = (byte)i;
}
des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(, ));
des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(, ));
System.IO.MemoryStream ms = new System.IO.MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, , inputByteArray.Length);
cs.FlushFinalBlock();
return Encoding.Default.GetString(ms.ToArray());
}
}
下面是解密效果,如果客户不是专业人士,我们公网数据库连接就是安全的了:

connectionString加密的更多相关文章
- 对web.config的ConnectionString加密
原文:对web.config的ConnectionString加密 本文参考了wayshan的博客,原文地址:http://www.cnblogs.com/wayshan/archive/2012/0 ...
- .NET安全审核检查表
书籍名称:Web安全设计之道 -.NET代码安全,界面漏洞防范与程序优化 .NET安全审核检查表 检查项 任务描述 设计环节 Security descisions should no ...
- NBIbatis 基础框架
基础框架 NBIbatis 为真实在用的系统中剥离出的一个ibatis.net应用框架,目的在于通过此项目让软件工程师集中关注表现层及业务规则编写. 通过数据访问和业务规则可快速搭建不同表现形式的网站 ...
- ASP.NET MVC系列:web.config中ConnectionString aspnet_iis加密与AppSettings独立文件
1. web.config中ConnectionString aspnet_iis加密 web.config路径:E:\Projects\Libing.Web\web.config <conne ...
- 【转】Encrypt ConnectionString in Web.Config 【加密ASP.NET web.config数据库链接字串】
原文链接:https://www.codeproject.com/Tips/795135/Encrypt-ConnectionString-in-Web-Config web.config中一般会存放 ...
- 如何对ConnectionString进行加密解码?
这个就不说了就是一个类 public static class EncryptionConfig { /* 加密配置节点 * Response.Write(EncryptionConfig.Encry ...
- ASP.NET加密和解密数据库连接字符串
大家知道,在应用程序中进行数据库操作需要连接字符串,而如果没有连接字符串,我们就无法在应用程序中完成检索数据,创建数据等一系列的数据库操作.当有人想要获取你程序中的数据库信息,他首先看到的可能会是We ...
- C# salt+hash 加密
一.先明确几个基本概念 1.伪随机数:pseudo-random number generators ,简称为:PRNGs,是计算机利用一定的算法来产生的.伪随机数并不是假随机 数,这里的" ...
- ASP.NET MVC5+EF6+EasyUI 后台管理系统(62)-EF链接串加密
系列目录 前言: 这一节提供一个简单的功能,这个功能看似简单,找了一下没找到EF链接数据库串的加密帮助文档,只能自己写了,这样也更加符合自己的加密要求 有时候我们发布程序为了避免程序外的SQL链接串明 ...
随机推荐
- Hadoop技术内幕-第一章 阅读原代码前的准备
1.1 源代码学习环境 1.1.1 基础软件的下载 JDK-http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads ...
- WCF初探-2:手动实现WCF程序
1.前言 上一篇,我们通过VS自带的模板引擎自动生成了一个wcf程序,接下来我们将手动实现一个wcf程序.由于应用程序开发中一般都会涉及到大量的增删改查业务,所以这个程序将简单演示如何在wcf中构建简 ...
- iOS iPad开发之UIPopoverController的使用
1. 什么是UIPopoverController? 是iPad开发中常见的一种控制器(在iphone上不允许使用) 跟其他控制器不一样的是,它直接继承自NSObject,并非继承自UIViewCon ...
- WP8.1 Study18:动态磁贴
一.前言 动态磁贴在WindowsPhone8.1和Windows8.1都是其特色,有人喜欢有人讨厌,不过我觉得还是挺好的,可以让使用者很快知道App内的内容和吸引使用者打开App.下面来学习下怎样添 ...
- Rhel6-DM多路经存储配置文档
系统环境: rhel6 x86_64 iptables and selinux disabled 主机: 主机 网卡 软件 server21.example.com eth0:192.168.122. ...
- Array 、 Dictionary 、 Object 3大数据结构 以及 for ... in for each的研究
Array:存储方式-------索引存储 (要求key是大于等于0的整数) Dictionary.Object:存储方式---------索引存储 区别:可以说它们的 key 都 对应 各自的引用, ...
- Python time模块学习
Python time模块提供了一些用于管理时间和日期的C库函数,由于它绑定到底层C实现,因此一些细节会基于具体的平台. 一.壁挂钟时间 1.time() time模块的核心函数time(),它返回纪 ...
- Android启动模式(三种)
1,标准启动模式 通过任务栈,每点一次button,将每一个实例都压入,然后点返回键时候,就弹出之前压入的实例. 每一次的地址都是不同的 测试代码:通过创建一个button和textView来显示本身 ...
- BZOJ 1433 二分图上的博弈
首先对网格染色,发现是而二分图. 那么即在二分图上选一个起点走过的点无法再走,最后无路可走就输了. 如果起点必在最大匹配中,先手必赢. 如果起点不一定在最大匹配中(包括不可能在),后手必赢.网上有解释 ...
- js 网站顶部导航栏
(function(){ var map = { 'index' : 0, 'gift_code' : 1, 'base_info' : 1, 'band_phone': 1, 'unlink_pho ...