using System; using System.IO; using System.Security.Cryptography; using System.Text; namespace RoadFlow.Utility { /// /// 加解密类 /// public class Encryption { /// /// 字符串MD5加密 /// /// /// public static string MD5(string str) { var md5 = System.Security.Cryptography.MD5.Create(); var result = md5.ComputeHash(Encoding.UTF8.GetBytes(str)); var strResult = BitConverter.ToString(result); return strResult.Replace("-", ""); } /// /// 字符串SHA1加密 /// /// /// public static string SHA1(string str) { var sha1 = System.Security.Cryptography.SHA1.Create(); var result = sha1.ComputeHash(Encoding.UTF8.GetBytes(str)); var strResult = BitConverter.ToString(result); return strResult.Replace("-", ""); } /// /// 加密KEY /// private static readonly string encryptKey = "4h!@w$rn"; /// /// 加密 /// /// /// public static string DESEncrypt(string encryptString) { try { DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] inputByteArray; inputByteArray = Encoding.Default.GetBytes(encryptString); string md5SKey = encryptKey; des.Key = Encoding.ASCII.GetBytes(md5SKey); des.IV = Encoding.ASCII.GetBytes(md5SKey); MemoryStream ms = new 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(); } catch { return ""; } } /// /// 解密 /// /// /// public static string DESDecrypt(string decryptString) { try { DESCryptoServiceProvider des = new DESCryptoServiceProvider(); int len; len = decryptString.Length / 2; byte[] inputByteArray = new byte[len]; int x, i; for (x = 0; x < len; x++) { i = Convert.ToInt32(decryptString.Substring(x * 2, 2), 16); inputByteArray[x] = (byte)i; } string md5SKey = encryptKey; des.Key = Encoding.ASCII.GetBytes(md5SKey); des.IV = Encoding.ASCII.GetBytes(md5SKey); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); return Encoding.Default.GetString(ms.ToArray()); } catch { return ""; } } } }