首先是加密,解密类。

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(0, 8));des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));System.IO.MemoryStream ms = new System.IO.MemoryStream();CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);cs.Write(inputByteArray, 0, 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 / 2;byte[] inputByteArray = new byte[len];int x, i;for (x = 0; x < len; x++){i = Convert.ToInt32(Text.Substring(x * 2, 2), 16);inputByteArray[x] = (byte)i;}des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));System.IO.MemoryStream ms = new System.IO.MemoryStream();CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);cs.Write(inputByteArray, 0, 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 = { 42, 16, 93, 156, 78, 4, 218, 32, 15, 167, 44, 80, 26, 20, 155, 112, 2, 94, 11, 204, 119, 35, 184, 197 }; //定义密钥byte[] IV = { 55, 103, 246, 79, 36, 99, 167, 3 };  //定义偏移量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, 0, 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 = { 42, 16, 93, 156, 78, 4, 218, 32, 15, 167, 44, 80, 26, 20, 155, 112, 2, 94, 11, 204, 119, 35, 184, 197 }; //定义密钥byte[] IV = { 55, 103, 246, 79, 36, 99, 167, 3 };   //定义偏移量TripleDESCryptoServiceProvider TripleDES = new TripleDESCryptoServiceProvider();TripleDES.IV = IV;TripleDES.Key = key;ICryptoTransform encrypto = TripleDES.CreateDecryptor();System.IO.MemoryStream ms = new System.IO.MemoryStream(bytIn, 0, 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);}}}
}

View Code

加密使用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;}}

View Code

加密的原因是,为客户开发的某些程序,需要访问公司(我们自己工作的)在公网的数据库,但是我们不能将明文数据库访问字符串,存放在客户的应用程序上,最好的办法就是将其加密。下面就在一个为客户开发的程序中使用这个加密连接。这里,我将密钥和密文都写在了配置文件中。如果用户猜出我的加密算法,他们可以根据密钥,可以轻松获得我的明文。所以,不要傻到直接将密钥配置命名成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 / 2;byte[] inputByteArray = new byte[len];int x, i;for (x = 0; x < len; x++){i = Convert.ToInt32(Text.Substring(x * 2, 2), 16);inputByteArray[x] = (byte)i;}des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));System.IO.MemoryStream ms = new System.IO.MemoryStream();CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);cs.Write(inputByteArray, 0, inputByteArray.Length);cs.FlushFinalBlock();return Encoding.Default.GetString(ms.ToArray());}}

View Code

下面是解密效果,如果客户不是专业人士,我们公网数据库连接就是安全的了:

connectionString加密相关推荐

  1. [转]信息安全相关理论题(二)

    27.在工程实施之前,验收方可以不给施工方弱电布线图纸,但施工结束后必须有图纸 A. 对 B. 错 您的答案: 标准答案: B 28.在OSI七层协议中,提供一种建立连接并有序传输数据的方法的层是 A ...

  2. 两种获取connectionString的方式

    两种获取connectionString的方式 1. public static string connectionString = ConfigurationManager.ConnectionSt ...

  3. ASP.NET 配置文件加密

    众所周知,web.config可以存储包括数据库链接在内的众多信息,所以为配置文件加密有时候就显得必要了. 1.加密前的配置文件如下: Web.config 1 <?xml version=&q ...

  4. web.config加密解密批处理

    打开.Net Framework附带的SDK命令提示符. 加密命令: aspnet_regiis -pe "connectionStrings" -app "/encry ...

  5. Web.config配置文件的加密,解密及读写操作

    代码都比较简单.不用多做说明: Code aspx: <body> <form id="form1" runat="server"> & ...

  6. 加密(Asp.Net配置文件的)配置节

    序言 开发者经常需要在配置文件中存储一些配置信息.比如,数据库联接字符串,用户名和密码等.(这就提出)一个要求,使用ASP.NET 1.x的时候,.net framework应该提供一些方法密或解密这 ...

  7. C# 对WinForm应用程序的App.config的使用及加密

    原文地址:http://blog.163.com/zhou_zzq/blog/static/1019622120137621739874/ 我们在写C#应用程序时,在工程文件中放置一个app.conf ...

  8. WebConfig配置 文件加密处理

    webconfig 文件加密处理 前几日正好遇到配置文件加密解密的问题,简单记录下流程. 1.首先运行cmd然后打开Framework.cd C:\Windows\Microsoft.NET\Fram ...

  9. 解决打不开 RSA 密钥容器 即:加密web.config中的内容

    简单的解决方法: WebConfig 加解密,未能使用提供程序"RsaProtectedConfigurationProvider"进行解密.提供程序返回错误消息为: 打不开 RS ...

最新文章

  1. php 的ob start,php ob_start()函数详解
  2. vijos1055 奶牛浴场
  3. javafx 自定义控件_JavaFX自定义控件– Nest Thermostat第2部分
  4. 每日一问:Android 滑动冲突,你们都是怎样处理的
  5. 逻辑回归 python_深入研究Python的逻辑回归
  6. 1.5 编程基础之循环控制 09 奇数求和
  7. 解码(七):音频重采样SwrContext和swr_convert相关函数详解
  8. 机械自动化算不算计算机相关专业,机械设计制造及其自动化专业属于什么类别...
  9. ESET ESS 激活码
  10. 草料二维码生成怎么用php代码实现
  11. linux清理垃圾文件,安装及使用Sweeper从Linux系统上清除垃圾文件
  12. Python爬虫开发从入门到实战
  13. Thttpd上传大文件的方法
  14. 对于软件,我是认真的
  15. 有序数组合并及等长数组对位穿插
  16. 7-10 公路村村通
  17. Java函数式编程 - 再谈Stream
  18. 【单片机原理及接口技术】【理解】七种寻址方式
  19. 互联网晚报 | 10月28日 星期四 | 农夫山泉钟睒睒首次成为中国首富;淘宝购物车分享功能上线;段永平否认牵头OV联合造车...
  20. PhotoShop暂存盘已满导致无法启动的至简解决方案

热门文章

  1. PostgreSQL 的 target_list分析(四)
  2. Window平台实时流媒体
  3. js 控制 获取 dropdownlist的值
  4. SQL 2005中pivot and unpivot的用法
  5. 开机explorer无法启动,无法进入桌面
  6. mynewt 编译环境搭建
  7. yii 添加数据时 有默认值 就赋值默认值
  8. Js里面IF(var)表示什么意思?js中if的写法、含义
  9. 头回遇见网上找不到的问题,“缺少实例ID,实例ID是必需的”
  10. XCTF-高手进阶区:ics-04