![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
/// <summary> /// DES加密 /// </summary> /// <param name="key">密钥</param> /// <param name="IV">向量(可以和密钥相同,解密需要)</param> /// <param name="content">要加密的内容</param> /// <returns>密文</returns> public static string Encrypt_DES(string key, string IV, string content) { if (!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(IV) && !string.IsNullOrEmpty(content)) { byte[] bKey = Encoding.UTF8.GetBytes(key); byte[] bIV = Encoding.UTF8.GetBytes(IV); byte[] byteArray = Encoding.UTF8.GetBytes(content); string encrypt = null; DESCryptoServiceProvider des = new DESCryptoServiceProvider(); try { using (MemoryStream mStream = new MemoryStream()) { using (CryptoStream cStream = new CryptoStream(mStream, des.CreateEncryptor(bKey, bIV), CryptoStreamMode.Write)) { cStream.Write(byteArray, 0, byteArray.Length); cStream.FlushFinalBlock(); encrypt = Convert.ToBase64String(mStream.ToArray()); cStream.Close(); cStream.Dispose(); } mStream.Close(); mStream.Dispose(); } } catch (Exception err) { } des.Clear(); return encrypt; } return null; } /// <summary> /// DES解密 /// </summary> /// <param name="key">密钥</param> /// <param name="IV">向量</param> /// <param name="encryptStr">需要解密的字符串</param> /// <returns>原文</returns> public static string Decrypt_DES(string key, string IV, string encryptStr) { if (!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(IV) && !string.IsNullOrEmpty(encryptStr)) { string decrypt = null; DESCryptoServiceProvider des = new DESCryptoServiceProvider(); try { byte[] bKey = Encoding.UTF8.GetBytes(key); byte[] bIV = Encoding.UTF8.GetBytes(IV); byte[] byteArray = Convert.FromBase64String(encryptStr); using (MemoryStream mStream = new MemoryStream()) { using (CryptoStream cStream = new CryptoStream(mStream, des.CreateDecryptor(bKey, bIV), CryptoStreamMode.Write)) { cStream.Write(byteArray, 0, byteArray.Length); cStream.FlushFinalBlock(); decrypt = Encoding.UTF8.GetString(mStream.ToArray()); } } } catch (Exception err) { } des.Clear(); return decrypt; } return null; } /// <summary> /// MD5加密 /// </summary> /// <param name="text">要加密的字符串或参数名称</param> /// <param name="charset">字符串编码格式,一般填utf-8</param> /// <param name="isArg">加密字符串类型 true:参数值 false:字符串,一般选false</param> /// <returns></returns> public static string MD5(string text, string charset, bool isArg) { try { MD5CryptoServiceProvider MD5 = new MD5CryptoServiceProvider(); if (isArg) { NameValueCollection Collect = HttpUtility.ParseQueryString(HttpContext.Current.Request.Url.Query, Encoding.GetEncoding(charset)); //使用Collect接收参数值 if (Collect[text] != null) { return BitConverter.ToString(MD5.ComputeHash(Encoding.GetEncoding(charset).GetBytes(Collect[text].ToString()))).Replace("-", ""); } } else { return BitConverter.ToString(MD5.ComputeHash(Encoding.GetEncoding(charset).GetBytes(text))).Replace("-", ""); } } catch (Exception err) { } return string.Empty; }