DES对称加密
/// <summary> /// DES对称加密 /// </summary> public static class DESHelper { /// <summary> /// 根据用户名解密 /// </summary> /// <param name="val"></param> /// <param name="userid"></param> /// <returns></returns> public static string Decrypt(string val, string userid = "") { var key = GetKey(userid); var iv = GetDefaultIV(); return Decrypt(val, key, iv); } /// <summary> /// 根据用户名加密 /// </summary> /// <param name="val"></param> /// <param name="userid"></param> /// <returns></returns> public static string Encrypt(string val, string userid = "") { var key = GetKey(userid); var iv = GetDefaultIV(); return Encrypt(val, key, iv); } /// <summary> /// Des加密方法 /// </summary> /// <param name="val"></param> /// <param name="key"></param> /// <param name="IV"></param> /// <returns></returns> public static string Encrypt(string val, string key, string IV, CipherMode cipherMode = CipherMode.ECB) { try { if (string.IsNullOrEmpty(key) || string.IsNullOrEmpty(key)) { throw new Exception("密钥和偏移向量不足8位"); } if (key.Length > 8) key = key.Substring(0, 8); if (IV.Length > 8) IV = IV.Substring(0, 8); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] btKey = Encoding.Default.GetBytes(key); byte[] btIV = Encoding.Default.GetBytes(IV); StringBuilder builder = new StringBuilder(); using (MemoryStream ms = new MemoryStream()) { byte[] inData = Encoding.Default.GetBytes(val); using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(btKey, btIV), CryptoStreamMode.Write)) { cs.Write(inData, 0, inData.Length); cs.FlushFinalBlock(); } foreach (byte num in ms.ToArray()) { builder.AppendFormat("{0:X2}", num); } } return builder.ToString(); } catch // (Exception ex) { return ""; } } /// <summary> /// Des解密方法 /// cbc模式:(key值和iv值一致) /// </summary> /// <param name="val"></param> /// <param name="key"></param> /// <param name="IV"></param> /// <returns></returns> public static string Decrypt(string val, string key, string IV, CipherMode cipherMode = CipherMode.ECB) { try { if (string.IsNullOrEmpty(key) || string.IsNullOrEmpty(key)) { throw new Exception("密钥和偏移向量不足8位"); } if (key.Length > 8) key = key.Substring(0, 8); if (IV.Length > 8) IV = IV.Substring(0, 8); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] btKey = Encoding.Default.GetBytes(key); byte[] btIV = Encoding.Default.GetBytes(IV); using (MemoryStream ms = new MemoryStream()) { byte[] inData = new byte[val.Length / 2]; for (int i = 0; i < (val.Length / 2); i++) { int num2 = Convert.ToInt32(val.Substring(i * 2, 2), 0x10); inData[i] = (byte)num2; } using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(btKey, btIV), CryptoStreamMode.Write)) { cs.Write(inData, 0, inData.Length); cs.FlushFinalBlock(); } return Encoding.Default.GetString(ms.ToArray()); } } catch // (System.Exception ex) { return ""; } } /// <summary> /// Md5加密 /// </summary> /// <param name="str"></param> /// <returns></returns> public static string MD5(string str) { if (string.IsNullOrEmpty(str)) return str; System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create(); string encoded = BitConverter.ToString(md5.ComputeHash(Encoding.UTF8.GetBytes(str))).Replace("-", ""); return encoded; } private static string GetKey(string key) { string defaultKey = "C560F02F693B4A0BA62D2B3B5FB74534"; string sTemp = defaultKey; if (!string.IsNullOrEmpty(key)) { sTemp = string.Concat(key, defaultKey.Substring(key.Length, 32 - key.Length)); } return MD5(sTemp).Substring(0, 8); } /// <summary> /// 获得初始向量IV /// </summary> /// <returns>初试向量IV</returns> private static string GetDefaultIV() { string sTemp = "87DE696F56DE49C0B96EB85139A48805"; return MD5(sTemp).Substring(0, 8); } }