首先是加密,解密类。算法
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); } } } }
加密使用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。若是将密钥写死在代码中就不方便控制,客户反编译一样能获知加密算法和密钥,从而得到原本的链接字符串。app
下面在获取链接字符串时,都要对其进行解密,因此构造一个解密类是必要的。ide
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()); } }
下面是解密效果,若是客户不是专业人士,咱们公网数据库链接就是安全的了:测试