DES简介:参考知乎 https://www.zhihu.com/question/36767829 和博客https://www.cnblogs.com/idreamo/p/9333753.html
BASE64简介:
DES加密代码实现
1 using System; 2 using System.Collections.Generic; 3 using System.Configuration; 4 using System.IO; 5 using System.Linq; 6 using System.Security.Cryptography; 7 using System.Text; 8 using System.Threading.Tasks; 9 10 namespace ConsoleApp2 11 { 12 public static class DESJiaMi 13 { 14 15 /// <summary> 16 /// 使用配置中的KEY,进行DES加密。 17 /// </summary> 18 /// <param name="pToEncrypt">要加密的字符串。</param> 19 /// <returns>以Base64格式返回的加密字符串。</returns> 20 public static string Encrypt(string pToEncrypt) 21 { 22 return Encrypt(pToEncrypt, ConfigurationManager.AppSettings["DESKey"] ?? "88888888"); 23 } 24 25 /// <summary> 26 /// 使用配置中的KEY,进行DES解密。 27 /// </summary> 28 /// <param name="pToDecrypt">要解密的以Base64</param> 29 /// <returns>已解密的字符串。</returns> 30 public static string Decrypt(string pToDecrypt) 31 { 32 return Decrypt(pToDecrypt, ConfigurationManager.AppSettings["DESKey"] ?? "88888888"); 33 } 34 35 /// <summary> 36 /// 进行DES加密。 37 /// </summary> 38 /// <param name="pToEncrypt">要加密的字符串。</param> 39 /// <param name="sKey">密钥,且必须为8位。</param> 40 /// <returns>以Base64格式返回的加密字符串。</returns> 41 public static string Encrypt(string pToEncrypt, string sKey) 42 { 43 using (DESCryptoServiceProvider des = new DESCryptoServiceProvider()) 44 { 45 byte[] inputByteArray = Encoding.UTF8.GetBytes(pToEncrypt); 46 des.Key = ASCIIEncoding.UTF8.GetBytes(sKey); 47 des.IV = ASCIIEncoding.UTF8.GetBytes(sKey); 48 des.Mode = CipherMode.CBC; 49 des.Padding = PaddingMode.PKCS7; 50 des.BlockSize = 64; 51 System.IO.MemoryStream ms = new System.IO.MemoryStream(); 52 using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write)) 53 { 54 cs.Write(inputByteArray, 0, inputByteArray.Length); 55 cs.FlushFinalBlock(); 56 cs.Close(); 57 } 58 string str = Convert.ToBase64String(ms.ToArray()); 59 ms.Close(); 60 return str; 61 } 62 } 63 64 65 /// <summary> 66 /// C# DES加密方法 返回base64字符串 67 /// Mode=CBC,PaddingMode=PKCS7,BlockSize=KeySize=64,Iv=Key长度 68 /// </summary> 69 /// <param name="encryptedValue">要加密的字符串</param> 70 /// <param name="key">密钥 为8位</param> 71 /// <returns>加密后的字符串</returns> 72 public static string DESEncrypt_Base64(string originalValue, string key) 73 { 74 try 75 { 76 if (string.IsNullOrEmpty(originalValue)) 77 return ""; 78 using (DESCryptoServiceProvider sa = new DESCryptoServiceProvider { Key = Encoding.UTF8.GetBytes(key), IV = Encoding.UTF8.GetBytes(key) }) 79 { 80 using (ICryptoTransform ct = sa.CreateEncryptor()) 81 { 82 byte[] by = Encoding.UTF8.GetBytes(originalValue); 83 using (var ms = new MemoryStream()) 84 { 85 using (var cs = new CryptoStream(ms, ct, CryptoStreamMode.Write)) 86 { 87 cs.Write(by, 0, by.Length); 88 cs.FlushFinalBlock(); 89 } 90 return Convert.ToBase64String(ms.ToArray()); 91 } 92 } 93 } 94 } 95 catch { } 96 return ""; 97 } 98 99 100 /// <summary> 101 /// C# DES解密方法返回UTF-8格式的字符串 102 /// Mode=CBC,PaddingMode=PKCS7,BlockSize=KeySize=64,Iv=Key长度 103 /// </summary> 104 /// <param name="encryptedValue">待解密的字符串</param> 105 /// <param name="key">密钥 为8位</param> 106 /// <returns>解密后的字符串</returns> 107 public static string DESDecrypt_Base64(string encryptedValue, string key) 108 { 109 try 110 { 111 if (string.IsNullOrEmpty(encryptedValue)) 112 return null; 113 using (DESCryptoServiceProvider sa = new DESCryptoServiceProvider { Key = Encoding.UTF8.GetBytes(key), IV = Encoding.UTF8.GetBytes(key) }) 114 { 115 using (ICryptoTransform ct = sa.CreateDecryptor()) 116 { 117 byte[] byt = Convert.FromBase64String(encryptedValue); 118 119 using (var ms = new MemoryStream()) 120 { 121 using (var cs = new CryptoStream(ms, ct, CryptoStreamMode.Write)) 122 { 123 cs.Write(byt, 0, byt.Length); 124 cs.FlushFinalBlock(); 125 } 126 return Encoding.UTF8.GetString(ms.ToArray()); 127 } 128 } 129 } 130 } 131 catch (Exception ex) { } 132 return ""; 133 } 134 135 /**/ 136 /// <summary> 137 /// 进行DES解密。 138 /// </summary> 139 /// <param name="pToDecrypt">要解密的以Base64</param> 140 /// <param name="sKey">密钥,且必须为8位。</param> 141 /// <returns>已解密的字符串。</returns> 142 public static string Decrypt(string pToDecrypt, string sKey) 143 { 144 byte[] inputByteArray = Convert.FromBase64String(pToDecrypt); 145 using (DESCryptoServiceProvider des = new DESCryptoServiceProvider()) 146 { 147 des.Key = ASCIIEncoding.UTF8.GetBytes(sKey); 148 des.IV = ASCIIEncoding.UTF8.GetBytes(sKey); 149 des.Mode = CipherMode.CBC;//加密模式(Mode)=CBC。 150 des.Padding = PaddingMode.PKCS7;//填充模式(PaddingMode)=PKCS7。 151 System.IO.MemoryStream ms = new System.IO.MemoryStream(); 152 using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write)) 153 { 154 cs.Write(inputByteArray, 0, inputByteArray.Length); 155 cs.FlushFinalBlock(); 156 cs.Close(); 157 } 158 string str = Encoding.UTF8.GetString(ms.ToArray()); 159 ms.Close(); 160 return str; 161 } 162 } 163 } 164 }