国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C# 數(shù)據(jù)庫鏈接字符串加密解密工具代碼詳解

2020-01-24 00:18:10
字體:
供稿:網(wǎng)友

有些項目尤其是WinForm或者是WPF項目,針對一些工具形式的小項目,不想軟件流出去之后,懂程序的的拿到手之后一看配置文件就知道了我們數(shù)據(jù)庫的用戶名和密碼,如果外網(wǎng)能訪問的話,那就麻煩大了。所以這里為了防止項目外泄之后這些信息不被別人看到,我們就需要對鏈接字符串或者其他重要信息進行加密,用的時候在解密。

思路:使用兩個數(shù)對連接字符串進行加密,再用這兩個數(shù)進行解密。

<add key="ConfigString" value="4HsXBRNXTkeN0ZoKdEwFE501TKSqLZUyJ0Zf+C7s5+gPd1SbWBiuh4PG6jeFgcnCTFr0QFW8FN40m/S8xmQq+8srL8taMLO23z6GSmaQJoM="/>

直接上代碼:

1:定義一個初始化源數(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ù)的不外泄。

注意:這個加密和解密的算法方法,應(yīng)該放在服務(wù)器。通過請求加解密方法。不應(yīng)該放在本地代碼里,技術(shù)牛的的人,把你的項目反編譯一樣可以看到源代碼。

 我們在把加密源數(shù)據(jù)找出來。

所以這個加解密代碼不能寫在本地,必須部署到安全的服務(wù)器上。

總結(jié)

以上所述是小編給大家介紹的C# 數(shù)據(jù)庫鏈接字符串加密解密工具代碼詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對武林網(wǎng)網(wǎng)站的支持!

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 开阳县| 吉水县| 临沭县| 南充市| 新昌县| 富源县| 金寨县| 安新县| 秦皇岛市| 东源县| 大庆市| 高密市| 六枝特区| 始兴县| 嘉祥县| 开封市| 钦州市| 驻马店市| 鹿邑县| 响水县| 鹤岗市| 长治县| 个旧市| 沁水县| 错那县| 全南县| 仁寿县| 筠连县| 武鸣县| 旬阳县| 西贡区| 桐城市| 邹城市| 仙桃市| 新民市| 新巴尔虎右旗| 漯河市| 麦盖提县| 安陆市| 桂阳县| 江陵县|