来源:https://blog.csdn.net/Zhangchen9091/article/details/46278463
https://blog.51cto.com/cbcw/6289416
using System; using System.IO; using System.Security.Cryptography; using System.Text; namespace ConsoleApp1 { public class DES3Util { public static String desKey = "BFYB_KEY";//键 public static String desIV = "BFYB_IIV";//向量 /// <summary> /// DES加密 /// </summary> /// <param name="originText"></param> /// <returns></returns> public static String desEncode(string originText) { byte[] tmpDesKey = Encoding.ASCII.GetBytes(desKey); byte[] tmpDesIV = Encoding.ASCII.GetBytes(desIV); DESCryptoServiceProvider desProvider = new DESCryptoServiceProvider(); MemoryStream memStream = new MemoryStream(); CryptoStream cryptoStream = new CryptoStream(memStream, desProvider.CreateEncryptor(tmpDesKey, tmpDesIV), CryptoStreamMode.Write); StreamWriter streamWriter = new StreamWriter(cryptoStream); streamWriter.Write(originText); streamWriter.Flush(); cryptoStream.FlushFinalBlock(); streamWriter.Flush(); String retStr = Convert.ToBase64String(memStream.GetBuffer(), 0, (int)memStream.Length); return retStr; } /// <summary> /// DES解密 /// </summary> /// <param name="originText"></param> /// <returns></returns> public static String desDecode(String originText) { byte[] tmpDesKey = System.Text.ASCIIEncoding.ASCII.GetBytes(desKey); byte[] tmpDesIV = System.Text.ASCIIEncoding.ASCII.GetBytes(desIV); // byte[] originBytes; try { originText.Replace("_%_", "/"); originText.Replace("-%-", "#"); originBytes = Convert.FromBase64String(originText); } catch (Exception exception) { return null; } DESCryptoServiceProvider desProvider = new DESCryptoServiceProvider(); MemoryStream memStream = new MemoryStream(originBytes); CryptoStream cryptoStream = new CryptoStream(memStream, desProvider.CreateDecryptor(tmpDesKey, tmpDesIV), CryptoStreamMode.Read); StreamReader steamReader = new StreamReader(cryptoStream); String retString = steamReader.ReadToEnd(); return retString; } //原文链接:https://blog.csdn.net/Zhangchen9091/article/details/46278463 /// <summary> /// DES加密字符串 /// </summary> /// <param name="encryptString">待加密的字符串</param> /// <param name="Key">密钥</param> /// <returns>加密成功返回加密后的字符串,失败返回源串</returns> public static string EncryptDES(string encryptString, string key) { try { byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); des.Key = Encoding.UTF8.GetBytes(key); des.Mode = CipherMode.ECB; MemoryStream mStream = new MemoryStream(); CryptoStream cStream = new CryptoStream(mStream, des.CreateEncryptor(), CryptoStreamMode.Write); cStream.Write(inputByteArray, 0, inputByteArray.Length); cStream.FlushFinalBlock(); return Convert.ToBase64String(mStream.ToArray()); } catch { return encryptString; } } /// <summary> /// DES解密字符串 /// </summary> /// <param name="decryptString">待解密的字符串</param> /// <returns>解密成功返回解密后的字符串,失败返源串</returns> public static string DecryptDES(string decryptString, string key) { try { byte[] inputByteArray = Convert.FromBase64String(decryptString); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); des.Key = Encoding.UTF8.GetBytes(key); des.Mode = CipherMode.ECB; MemoryStream mStream = new MemoryStream(); CryptoStream cStream = new CryptoStream(mStream, des.CreateDecryptor(), CryptoStreamMode.Write); cStream.Write(inputByteArray, 0, inputByteArray.Length); cStream.FlushFinalBlock(); return Encoding.UTF8.GetString(mStream.ToArray()); } catch { return decryptString; } } //.Net DES加密解密(不带向量) //https://blog.51cto.com/cbcw/6289416 } }
标签:des,加密,MemoryStream,DES,解密,DESCryptoServiceProvider,return,new,CryptoStream From: https://www.cnblogs.com/xsj1989/p/17751943.html