日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

connectionString加密

發(fā)布時(shí)間:2023/12/31 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 connectionString加密 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

首先是加密,解密類。

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("加密時(shí)候出現(xiàn)錯(cuò)誤!錯(cuò)誤提示:\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("解密時(shí)候出現(xiàn)錯(cuò)誤!錯(cuò)誤提示:\n" + ex.Message);}}} } View Code

加密使用MD5Decrypt,自己給定一個(gè)密鑰。下面是對(duì)連接字符串加密的界面:

代碼和測(cè)試效果:

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

加密的原因是,為客戶開發(fā)的某些程序,需要訪問公司(我們自己工作的)在公網(wǎng)的數(shù)據(jù)庫(kù),但是我們不能將明文數(shù)據(jù)庫(kù)訪問字符串,存放在客戶的應(yīng)用程序上,最好的辦法就是將其加密。下面就在一個(gè)為客戶開發(fā)的程序中使用這個(gè)加密連接。這里,我將密鑰和密文都寫在了配置文件中。如果用戶猜出我的加密算法,他們可以根據(jù)密鑰,可以輕松獲得我的明文。所以,不要傻到直接將密鑰配置命名成key。如果將密鑰寫死在代碼中就不方便控制,客戶反編譯同樣能獲知加密算法和密鑰,從而獲得本來(lái)的連接字符串。

下面在獲取連接字符串時(shí),都要對(duì)其進(jìn)行解密,所以構(gòu)造一個(gè)解密類是必要的。

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

下面是解密效果,如果客戶不是專業(yè)人士,我們公網(wǎng)數(shù)據(jù)庫(kù)連接就是安全的了:

?

總結(jié)

以上是生活随笔為你收集整理的connectionString加密的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。