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链接串明 ...
随机推荐
- Java:集合工具类-Collections
Java.util.Collections 集合框架工具类Collections,其方法都是静态的,本身没有构造函数. 常见方法: static <T extends Comparable< ...
- 解决SQLite database is locked
前些时候,同事在站点服务端使用SQlite存储一些临时数据,但是在多人并发的时候Sqlite会抛出异常:The database file is locked , database is locked ...
- JSP常见指令
JSP常见指令 标签: jspincludeservletjavaappletarchive 2011-11-07 20:07 13193人阅读 评论(3) 收藏 举报 版权声明:本文为博主原创文章, ...
- 存储过程实现登录(.net)
工作中,可能有时为了安全等的考虑,需要更多 的运用存储过程.有的公司甚至在登录一栏也会提出这样的要求,那么怎么用存储过程实现登录呢.好处就不用言名了,一个速度,一个就是安全系统更高. 下面贴上:1.存 ...
- 关于ImageLoader的详细介绍
转载请注明本文出自xiaanming的博客(http://blog.csdn.net/xiaanming/article/details/26810303),请尊重他人的辛勤劳动成果,谢谢! 相信大家 ...
- 如何提高Service的优先级避免被杀死或者杀死后如何再次重启Service?
2014-01-21 16:45:02 我们知道,当进程长期不活动时,如果系统资源吃紧,会杀死一些Service,或不可见的Activity等所在的进程. 如何避免Service被系统杀死,随便在网上 ...
- imx6q uboot启动流程牛人的图片(转)
- windows 版的julia repl 启动时间已经大大优化!
julia 是一门语法类似python 偏向主要用于科学计算的语言,julia吸收了很多其它语言的优点,内置了大量函数,使用起来很方便. 之前windows下的 julia repl(交互解释器)启动 ...
- php中 -> 和 => 和 :: 的用法 以及 self 和 $this 的用法
=> 数组中 用于数组的 key 和 value之间的关系例如:$a = array( '0' => '1', '2' => '4',); echo $a['0'];echo $a[ ...
- BZOJ 3670 && BZOJ 3620 && BZOJ 3942 KMP
最近感到KMP不会啊,以前都是背板的现在要理解了. #include <iostream> #include <cstring> #include <cstdio> ...