一套简单的加密解密思路
加密解密的核心代码:
1 using System; 2 using System.IO; 3 using System.Security.Cryptography; 4 using System.Text; 5 6 public class AESChiper 7 { 8 /// <summary> 9 /// AES加密或者解密 10 /// </summary> 11 /// <param name="data"></param> 12 /// <param name="key"></param> 13 /// <param name="isEncrypt"></param> 14 /// <returns></returns> 15 public byte[] EncryptOrDecrypt(byte[] data, string key, bool isEncrypt) 16 { 17 if (null == data) 18 { 19 return null; 20 } 21 22 if (data.Length <= 0 || string.IsNullOrEmpty(key)) 23 { 24 return data; 25 } 26 27 int keyLen = key.Length; 28 29 RijndaelManaged aes = new RijndaelManaged(); 30 aes.KeySize = keyLen * 8; 31 aes.BlockSize = keyLen * 8; 32 aes.Mode = CipherMode.CBC; 33 aes.Padding = PaddingMode.Zeros; 34 aes.Key = Encoding.UTF8.GetBytes(key); 35 36 aes.IV = new byte[keyLen]; 37 for (int i = 0; i < keyLen; i++) 38 { 39 aes.IV[i] = 0; 40 } 41 42 var transform = isEncrypt ? aes.CreateEncryptor(aes.Key, aes.IV) : aes.CreateDecryptor(); 43 byte[] xBuff = null; 44 using (var ms = new MemoryStream()) 45 { 46 using (var cs = new CryptoStream(ms, transform, CryptoStreamMode.Write)) 47 { 48 cs.Write(data, 0, data.Length); 49 } 50 xBuff = ms.ToArray(); 51 } 52 53 return xBuff; 54 } 55 }
定义一个管理类,使得加密解密使用起来更方便,代码如下:
1 using System; 2 3 public class ChiperManager 4 { 5 ChiperManager() 6 { 7 8 } 9 10 static ChiperManager _instance; 11 12 public static ChiperManager Instance 13 { 14 get 15 { 16 if (null == _instance) 17 { 18 _instance = new ChiperManager(); 19 } 20 return _instance; 21 } 22 } 23 24 const string AES_DEFAULT_KEY = "1234567891234567"; 25 26 AESChiper mChiper = new AESChiper(); 27 28 public byte[] Encrypt(byte[] orgData, string key = AES_DEFAULT_KEY) 29 { 30 if (null == orgData) 31 { 32 return null; 33 } 34 35 if (orgData.Length <= 0) 36 { 37 return orgData; 38 } 39 40 return mChiper.EncryptOrDecrypt(orgData, key, true); 41 } 42 43 public byte[] Decrypt(byte[] encryptData, string key = AES_DEFAULT_KEY) 44 { 45 if (null == encryptData) 46 { 47 return null; 48 } 49 50 if (encryptData.Length <= 0) 51 { 52 return encryptData; 53 } 54 55 return mChiper.EncryptOrDecrypt(encryptData, key, false); 56 } 57 }
定义一个可以编码解码的类:
1 class People 2 { 3 public string name; 4 public int age; 5 6 public byte[] Encode() 7 { 8 using (MemoryStream ms = new MemoryStream()) 9 { 10 using (BinaryWriter bw = new BinaryWriter(ms)) 11 { 12 bw.Write(name); 13 bw.Write(age); 14 15 } 16 return ms.ToArray(); 17 } 18 } 19 20 public void Decode(byte[] bytes) 21 { 22 using (MemoryStream ms = new MemoryStream(bytes)) 23 { 24 using (BinaryReader br = new BinaryReader(ms)) 25 { 26 name = br.ReadString(); 27 age = br.ReadInt32(); 28 } 29 } 30 } 31 32 public override string ToString() 33 { 34 return $"Name = {name},Age = {age}"; 35 } 36 }
加密解密测试:
1 static void Main(string[] args) 2 { 3 try 4 { 5 People p = new People(); 6 p.name = "张三"; 7 p.age = 20; 8 9 byte[] orgData = p.Encode(); 10 11 //加密 12 orgData = ChiperManager.Instance.Encrypt(orgData); 13 14 //解密 15 orgData = ChiperManager.Instance.Decrypt(orgData); 16 17 //加密后如果不解密,这里将抛出异常 18 p.Decode(orgData); 19 20 Console.WriteLine(p); 21 } 22 catch (Exception e) 23 { 24 Console.WriteLine(e.Message); 25 } 26 27 Console.ReadKey(); 28 }
运行结果:
加密如果不解密,会抛出如下异常: