AES加密
1 public static string AESEncrypt(string toEncrypt, string key, string iv = "xxxxxxx") 2 { 3 byte[] keyArray = Encoding.UTF8.GetBytes(key); 4 byte[] ivArray = Encoding.UTF8.GetBytes(iv); 5 byte[] toEncryptArray = Encoding.UTF8.GetBytes(toEncrypt); 6 7 RijndaelManaged rDel = new RijndaelManaged(); 8 rDel.Key = keyArray; 9 rDel.IV = ivArray; 10 rDel.Mode = CipherMode.CBC; 11 rDel.Padding = PaddingMode.Zeros; 12 ICryptoTransform cTransform = rDel.CreateEncryptor(); 13 byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); 14 return Convert.ToBase64String(resultArray, 0, resultArray.Length); 15 }
AES解密
1 public static string AESDecrypt(string toDecrypt, string key, string iv = "xxxxxxxx") 2 { 3 try 4 { 5 byte[] keyArray = Encoding.UTF8.GetBytes(key); 6 byte[] ivArray = Encoding.UTF8.GetBytes(iv); 7 byte[] toEncryptArray = Convert.FromBase64String(toDecrypt); 8 RijndaelManaged rDel = new RijndaelManaged(); 9 rDel.Key = keyArray; 10 rDel.IV = ivArray; 11 rDel.Mode = CipherMode.CBC; 12 rDel.Padding = PaddingMode.Zeros; 13 ICryptoTransform cTransform = rDel.CreateDecryptor(); 14 byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); 15 return Encoding.UTF8.GetString(resultArray).TrimEnd(‘\0‘); 16 } 17 catch (Exception ex) 18 { 19 LogHelper.log.Error("解密出错~~: " + ex.Message, ex); 20 } 21 return ""; 22 }