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 }
base64位加密代码实现
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; namespace ConsoleApp2 { public class Base64 { /// <summary> /// AES base64 加密算法 /// Key 为16位 /// </summary> /// <param name="Data">需要加密的字符串</param> /// <param name="Key">Key 为16位 密钥</param> /// <param name="Vector">向量</param> /// <returns></returns> public static string RST_AesEncrypt_Base64(string Data, string Key) { if (string.IsNullOrEmpty(Data)) { return null; } if (string.IsNullOrEmpty(Key)) { return null; } var Vector = Key.Substring(0, 16); Byte[] plainBytes = Encoding.UTF8.GetBytes(Data); Byte[] bKey = new Byte[32]; Array.Copy(Encoding.UTF8.GetBytes(Key.PadRight(bKey.Length)), bKey, bKey.Length); Byte[] bVector = new Byte[16]; Array.Copy(Encoding.UTF8.GetBytes(Vector.PadRight(bVector.Length)), bVector, bVector.Length); Byte[] Cryptograph = null; // 加密后的密文 Rijndael Aes = Rijndael.Create(); //add Aes.Mode = CipherMode.CBC;//兼任其他语言的des Aes.BlockSize = 128; Aes.Padding = PaddingMode.PKCS7; //add end try { // 开辟一块内存流 using (MemoryStream Memory = new MemoryStream()) { // 把内存流对象包装成加密流对象 using (CryptoStream Encryptor = new CryptoStream(Memory, Aes.CreateEncryptor(bKey, bVector), CryptoStreamMode.Write)) { // 明文数据写入加密流 Encryptor.Write(plainBytes, 0, plainBytes.Length); Encryptor.FlushFinalBlock(); Cryptograph = Memory.ToArray(); } } } catch { Cryptograph = null; } return Convert.ToBase64String(Cryptograph); } /// <summary> /// AES base64 解密算法 /// Key为16位 /// </summary> /// <param name="Data">需要解密的字符串</param> /// <param name="Key">Key为16位 密钥</param> /// <param name="Vector">向量</param> /// <returns></returns> public static string RST_AesDecrypt_Base64(string Data, string Key) { try { if (string.IsNullOrEmpty(Data)) { return null; } if (string.IsNullOrEmpty(Key)) { return null; } var Vector = Key.Substring(0, 16); Byte[] encryptedBytes = Convert.FromBase64String(Data); Byte[] bKey = new Byte[32]; Array.Copy(Encoding.UTF8.GetBytes(Key.PadRight(bKey.Length)), bKey, bKey.Length); Byte[] bVector = new Byte[16]; Array.Copy(Encoding.UTF8.GetBytes(Vector.PadRight(bVector.Length)), bVector, bVector.Length); Byte[] original = null; // 解密后的明文 Rijndael Aes = Rijndael.Create(); //add Aes.Mode = CipherMode.CBC;//兼任其他语言的des Aes.BlockSize = 128; Aes.Padding = PaddingMode.PKCS7; //add end try { // 开辟一块内存流,存储密文 using (MemoryStream Memory = new MemoryStream(encryptedBytes)) { //把内存流对象包装成加密流对象 using (CryptoStream Decryptor = new CryptoStream(Memory, Aes.CreateDecryptor(bKey, bVector), CryptoStreamMode.Read)) { // 明文存储区 using (MemoryStream originalMemory = new MemoryStream()) { Byte[] Buffer = new Byte[1024]; Int32 readBytes = 0; while ((readBytes = Decryptor.Read(Buffer, 0, Buffer.Length)) > 0) { originalMemory.Write(Buffer, 0, readBytes); } original = originalMemory.ToArray(); } } } } catch { original = null; } return Encoding.UTF8.GetString(original); } catch { return null; } } } }
BASE64加密解密
public sealed class Base64 { /// <summary> /// Base64加密 /// </summary> /// <param name="codeName">加密采用的编码方式</param> /// <param name="source">待加密的明文</param> /// <returns></returns> public static string EncodeBase64(Encoding encode, string source) { byte[] bytes = encode.GetBytes(source); try { encode = Convert.ToBase64String(bytes); } catch { encode = source; } return encode; } /// <summary> /// Base64加密,采用utf8编码方式加密 /// </summary> /// <param name="source">待加密的明文</param> /// <returns>加密后的字符串</returns> public static string EncodeBase64(string source) { return EncodeBase64(Encoding.UTF8, source); } /// <summary> /// Base64解密 /// </summary> /// <param name="codeName">解密采用的编码方式,注意和加密时采用的方式一致</param> /// <param name="result">待解密的密文</param> /// <returns>解密后的字符串</returns> public static string DecodeBase64(Encoding encode, string result) { string decode = ""; byte[] bytes = Convert.FromBase64String(result); try { decode = encode.GetString(bytes); } catch { decode = result; } return decode; } /// <summary> /// Base64解密,采用utf8编码方式解密 /// </summary> /// <param name="result">待解密的密文</param> /// <returns>解密后的字符串</returns> public static string DecodeBase64(string result) { return DecodeBase64(Encoding.UTF8, result); } }