C# 數(shù)據(jù)庫鏈接字符串加密解密工具代碼詳解
有些項(xiàng)目尤其是WinForm或者是WPF項(xiàng)目,針對一些工具形式的小項(xiàng)目,不想軟件流出去之后,懂程序的的拿到手之后一看配置文件就知道了我們數(shù)據(jù)庫的用戶名和密碼,如果外網(wǎng)能訪問的話,那就麻煩大了。所以這里為了防止項(xiàng)目外泄之后這些信息不被別人看到,我們就需要對鏈接字符串或者其他重要信息進(jìn)行加密,用的時(shí)候在解密。
思路:使用兩個(gè)數(shù)對連接字符串進(jìn)行加密,再用這兩個(gè)數(shù)進(jìn)行解密。
<add key="ConfigString" value="4HsXBRNXTkeN0ZoKdEwFE501TKSqLZUyJ0Zf+C7s5+gPd1SbWBiuh4PG6jeFgcnCTFr0QFW8FN40m/S8xmQq+8srL8taMLO23z6GSmaQJoM="/>
直接上代碼:
1:定義一個(gè)初始化源數(shù)據(jù)的類。
public class ConfigInformation { private static ConfigInformation _configInformation; public ConfigInformation Instance { get { if (_configInformation == null) { _configInformation = new ConfigInformation(); } return _configInformation; } } // 數(shù)據(jù)庫鏈接字符串加解密 Key Value public static String Key = "27e167e9-2660-4bc1-bea0-c8781a9f01cb"; public static String Vector = "8280d587-f9bf-4127-bbfa-5e0b4b672958"; }
2:加解密方法:
/// <summary> /// 加密 解密 /// </summary> public class DecryptAndEncryptionHelper { private readonly SymmetricAlgorithm _symmetricAlgorithm; private const String DefKey = "qazwsxedcrfvtgb!@#$%^&*(tgbrfvedcwsxqaz)(*&^%$#@!"; private String _key = ""; public String Key { get { return _key; } set { if (!String.IsNullOrEmpty(value)) { _key = value; } else { _key = DefKey; } } } private const String DefIV = "tgbrfvedcwsxqaz)(*&^%$#@!qazwsxedcrfvtgb!@#$%^&*("; private String _iv = ""; public String IV { get { return _iv; } set { if (!String.IsNullOrEmpty(value)) { _iv = value; } else { _iv = DefIV; } } } public DecryptAndEncryptionHelper() { _symmetricAlgorithm = new RijndaelManaged(); } public DecryptAndEncryptionHelper(String Key, String IV) { _symmetricAlgorithm = new RijndaelManaged(); _key = String.IsNullOrEmpty(Key) ? DefKey : Key; _iv = String.IsNullOrEmpty(IV) ? DefIV : IV; } /// <summary> /// Get Key /// </summary> /// <returns>密鑰</returns> private byte[] GetLegalKey() { _symmetricAlgorithm.GenerateKey(); byte[] bytTemp = _symmetricAlgorithm.Key; int KeyLength = bytTemp.Length; if (_key.Length > KeyLength) _key = _key.Substring(0, KeyLength); else if (_key.Length < KeyLength) _key = _key.PadRight(KeyLength, '#'); return ASCIIEncoding.ASCII.GetBytes(_key); } /// <summary> /// Get IV /// </summary> private byte[] GetLegalIV() { _symmetricAlgorithm.GenerateIV(); byte[] bytTemp = _symmetricAlgorithm.IV; int IVLength = bytTemp.Length; if (_iv.Length > IVLength) _iv = _iv.Substring(0, IVLength); else if (_iv.Length < IVLength) _iv = _iv.PadRight(IVLength, '#'); return ASCIIEncoding.ASCII.GetBytes(_iv); } /// <summary> /// Encrypto 加密 /// </summary> public string Encrypto(string Source) { byte[] bytIn = UTF8Encoding.UTF8.GetBytes(Source); MemoryStream ms = new MemoryStream(); _symmetricAlgorithm.Key = GetLegalKey(); _symmetricAlgorithm.IV = GetLegalIV(); ICryptoTransform encrypto = _symmetricAlgorithm.CreateEncryptor(); CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write); cs.Write(bytIn, 0, bytIn.Length); cs.FlushFinalBlock(); ms.Close(); byte[] bytOut = ms.ToArray(); return Convert.ToBase64String(bytOut); } /// <summary> /// Decrypto 解密 /// </summary> public string Decrypto(string Source) { byte[] bytIn = Convert.FromBase64String(Source); MemoryStream ms = new MemoryStream(bytIn, 0, bytIn.Length); _symmetricAlgorithm.Key = GetLegalKey(); _symmetricAlgorithm.IV = GetLegalIV(); ICryptoTransform encrypto = _symmetricAlgorithm.CreateDecryptor(); CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read); StreamReader sr = new StreamReader(cs); return sr.ReadToEnd(); } }
3:使用
// 獲取加密的鏈接字符串,然后解密 string enString = ConfigurationManager.AppSettings["ConfigString"]; DecryptAndEncryptionHelper helper = new DecryptAndEncryptionHelper(ConfigInformation.Key, ConfigInformation.Vector); // 明文 var configStr = helper.Decrypto(enString); return configStr;
這樣至少保證了數(shù)據(jù)的不外泄。
注意:這個(gè)加密和解密的算法方法,應(yīng)該放在服務(wù)器。通過請求加解密方法。不應(yīng)該放在本地代碼里,技術(shù)牛的的人,把你的項(xiàng)目反編譯一樣可以看到源代碼。
我們在把加密源數(shù)據(jù)找出來。
所以這個(gè)加解密代碼不能寫在本地,必須部署到安全的服務(wù)器上。
總結(jié)
以上所述是小編給大家介紹的C# 數(shù)據(jù)庫鏈接字符串加密解密工具代碼詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
C#實(shí)現(xiàn)GridView導(dǎo)出Excel實(shí)例代碼
本篇文章主要介紹了C#實(shí)現(xiàn)GridView導(dǎo)出Excel實(shí)例代碼,這里整理了詳細(xì)的代碼,非常具有實(shí)用價(jià)值,需要的朋友可以參考下。2017-03-03- 本文章來給各位同學(xué)介紹關(guān)于C#單擊菜單欄或工具欄時(shí)通過反射打開窗體的方法,有需要了解的朋友可進(jìn)入?yún)⒖紖⒖肌?/div> 2015-05-05
C# 串口接收數(shù)據(jù)中serialPort.close()死鎖的實(shí)例
下面小編就為大家分享一篇C# 串口接收數(shù)據(jù)中serialPort.close()死鎖的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-11-11Unity實(shí)現(xiàn)截屏以及根據(jù)相機(jī)畫面截圖
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)截屏以及根據(jù)相機(jī)畫面截圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04深入淺析c#靜態(tài)多態(tài)性與動態(tài)多態(tài)性
多態(tài)就是多種形態(tài),也就是對不同對象發(fā)送同一個(gè)消息,不同對象會做出不同的響應(yīng)。這篇文章主要介紹了c#靜態(tài)多態(tài)性與動態(tài)多態(tài)性的相關(guān)知識,需要的朋友可以參考下2018-09-09C# .net core HttpClientFactory用法及說明
這篇文章主要介紹了C# .net core HttpClientFactory用法及說明,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11C#實(shí)現(xiàn)XML文件與DataTable、Dataset互轉(zhuǎn)
這篇文章介紹了C#實(shí)現(xiàn)XML文件與DataTable、Dataset互轉(zhuǎn)的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04小菜編程成長記(一 面試受挫——代碼無錯(cuò)就是好?)
小菜編程成長記(一 面試受挫——代碼無錯(cuò)就是好?)...2006-10-10最新評論