using System; using System.IO; using System.Security.Cryptography; using System.Text; namespace GTech.Solution.Api.Common { public static class EncryptHelper { #region 创建字符串的MD5哈希值 /// /// 创建字符串的MD5哈希值 /// /// /// 字符串MD5哈希值的十六进制字符串 public static string StringToMD5Hash(string inputString) { MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); byte[] encryptedBytes = md5.ComputeHash(Encoding.ASCII.GetBytes(inputString)); StringBuilder sb = new StringBuilder(); for (int i = 0; i < encryptedBytes.Length; i++) { sb.AppendFormat("{0:x2}", encryptedBytes[i]); } return sb.ToString(); } #endregion #region 1、一般可逆加密解密 /// /// 加密 /// /// 加密数据 /// 密钥 /// 加密向量 /// 加密数据结果 public static string Encode(string data, string KEY_64, string IV_64) { byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64); byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64); DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider(); int i = cryptoProvider.KeySize; MemoryStream ms = new MemoryStream(); CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateEncryptor(byKey, byIV), CryptoStreamMode.Write); StreamWriter sw = new StreamWriter(cst); sw.Write(data); sw.Flush(); cst.FlushFinalBlock(); sw.Flush(); return Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length); } /// /// 解密 /// /// 解密数据 /// 密钥 /// 加密向量 /// 解密数据结果 public static string Decode(string data, string KEY_64, string IV_64) { byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64); byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64); byte[] byEnc; try { byEnc = Convert.FromBase64String(data); } catch { return null; } DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider(); MemoryStream ms = new MemoryStream(byEnc); CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateDecryptor(byKey, byIV), CryptoStreamMode.Read); StreamReader sr = new StreamReader(cst); return sr.ReadToEnd(); } #endregion #region 3、DES加密解密 /// /// DES 加密 /// /// 加密字符串 /// 密钥,要求八位 /// 向量 /// public static string DESEncode(string str, string strDESKey, string strDESIV) { return DESEncrytion.DESEncrypt(str, strDESKey, strDESIV); } /// /// DES 解密 /// /// 解密字符串 /// 密钥,要求八位 /// 向量 /// public static string DESDecode(string str, string strDESKey, string strDESIV) { return DESEncrytion.DESDecrypt(str, strDESKey, strDESIV); } /// /// DES 加密 加密向量默认 /// /// 加密字符串 /// 密钥,要求八位 /// public static string DESEncode(string str, string encrytKey) { return DESEncrytion.EncryptDES(str, encrytKey); } /// /// DES 解密 加密向量默认 /// /// 待解密字符串 /// 解密密钥,要求为8位,和加密密钥相同 /// public static string DESDecode(string str, string encrytKey) { return DESEncrytion.DecryptDES(str, encrytKey); } #endregion #region 4、文件加密解密 /// /// 加密文件 /// /// 源文件全名称 /// 加密后文件保存全名称 /// 加密密钥 /// 加密向量 private static void EncryptData(String inName, String outName, byte[] desKey, byte[] desIV) { FileEncrytion.EncryptData(inName, outName, desKey, desIV); } /// /// 解密文件 /// /// 源文件全名称 /// 解密文件全名称 /// 解密密钥 /// 解密向量 private static void DecryptData(String inName, String outName, byte[] desKey, byte[] desIV) { FileEncrytion.DecryptData(inName, outName, desKey, desIV); } #endregion #region 6、对称加密 /// /// 加密方法 /// /// 待加密的串 /// 经过加密的串 public static string Encrypto(string Source) { return SymmetricEncrytion.Encrypto(Source); } /// /// 解密方法 /// /// 待解密的串 /// 经过解密的串 public static string Decrypto(string Source) { return SymmetricEncrytion.Decrypto(Source); } #endregion } }