/* * MD5加密 * 肖俊 * 2013-6-7 */ using System; using System.IO; using System.Security.Cryptography; using System.Text; namespace AllBase { public static class Md5Base { /// /// MD5加密 /// /// /// public static string GetMd5(string instring) { byte[] result = Encoding.UTF8.GetBytes(string.IsNullOrEmpty(instring)?"": instring.Trim()); MD5 md5 = new MD5CryptoServiceProvider(); byte[] output = md5.ComputeHash(result); return BitConverter.ToString(output).Replace("-", ""); } /// /// MD5加密 /// /// /// //public static string GetMd5X16(string instring) //{ // string temp = GetMd5(instring); // return temp.Substring(8, 16); //} /// /// MD5加密 /// /// /// public static string GetMd5X15(string instring) { string temp = GetMd5(instring); return temp.Substring(8, 15); } /// /// 解码 /// /// /// public static string DecodeString(string code) { try { string code1 = code.Trim().Replace("_1/", "c") .Replace("_2/", "Y") .Replace("_3/", "A") .Replace("_4/", "Z") .Replace("_5/", "h") .Replace("_6/", "n") .Replace("_7/", "I") .Replace("_8/", "l") .Replace("_9/", "t") .Replace("_10/", "e") .Replace("_11/", "K") .Replace("_12/", "r") .Replace("_13/", "o") .Replace("_14/", "q") .Replace("_15/", "W") .Replace("_16/", "+") .Replace("_17/", "H") .Replace("_18/", "m") .Replace("_19/", "F") .Replace("_20/", "x") .Replace("_21/", "v") .Replace("_22/", "B") .Replace("_23/", "=") .Replace("_0/", "_"); byte[] inArray = Convert.FromBase64String(code1); string html = Encoding.UTF8.GetString(inArray); html = html.Replace("_1_", "=") .Replace("_2_", "?") .Replace("_3_", "/") .Replace("_4_", ".") .Replace("_5_", "&") .Replace("_6_", "<") .Replace("_7_", ">") .Replace("_8_", "'") .Replace("_9_", "\"") .Replace("_", "_0"); return html; } catch (Exception e) { LogSave.Add("解码数据错误:"+code); return ""; } } /// /// 加密返回数据 /// /// 密文 /// public static string EncodeString(string html) { if (string.IsNullOrEmpty(html)) return ""; html = html.Replace("_0", "_") .Replace("=", "_1_") .Replace("?", "_2_") .Replace("/", "_3_") .Replace(".", "_4_") .Replace("&", "_5_") .Replace("\\", "_6_") .Replace(">", "_7_") .Replace("'", "_8_") .Replace("\"", "_9_"); string code = Base64Encode.EncodeBase64(html); code = code.Replace("c", "_1_") .Replace("Y", "_2_") .Replace("A", "_3_") .Replace("Z", "_4_") .Replace("h", "_5_") .Replace("n", "_6_") .Replace("I", "_7_") .Replace("o", "_8_") .Replace("t", "_9_") .Replace("=", "_0_"); return code; } #region EBFB8THEA 加密 public static string _KEY = "XNNRSAA1"; //密钥 public static string _IV = "RSABPDD2"; //向量 /// /// 加密 /// /// 需要加密的文本 /// public static string EncryptEncode(string data) { byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(_KEY); byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(_IV); 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(); string strRet = Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length); sw.Dispose(); sw.Close(); cst.Dispose(); cst.Close(); ms.Dispose(); ms.Close(); return strRet; } /// /// 解密 /// /// /// public static string EncryptDecode(string data) { byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(_KEY); byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(_IV); byte[] byEnc; try { data.Replace("_%_", "/"); data.Replace("-%-", "#"); 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); var result = sr.ReadToEnd(); sr.Dispose(); sr.Close(); cst.Dispose(); cst.Close(); ms.Dispose(); ms.Close(); return result; } #endregion } }