AES加密类
代码(该AES加密解密代码可以和Java互通):
using System; using System.IO; using System.Security.Cryptography; using System.Text; namespace Utils { /// <summary> /// AES加密解密 /// </summary> public class AESHelper { #region 加密 /// <summary> /// AES 加密(高级加密标准,是下一代的加密算法标准,速度快,安全级别高,目前 AES 标准的一个实现是 Rijndael 算法) /// </summary> /// <param name="text">明文</param> /// <param name="key">密钥</param> /// <param name="iv">向量</param> public static string Encrypt(string text, string key, string iv) { if (text == null || text.Length == 0) { throw (new Exception("明文不得为空")); } if (string.IsNullOrWhiteSpace(key)) { throw (new Exception("密钥不得为空")); } if (string.IsNullOrWhiteSpace(iv)) { throw (new Exception("向量不得为空")); } if (key.Length < 16) { throw (new Exception("密钥长度不能小于16")); } if (iv.Length < 16) { throw (new Exception("向量长度不能小于16")); } byte[] textBytes = Encoding.UTF8.GetBytes(text); byte[] keyBytes = Encoding.UTF8.GetBytes(key); byte[] ivBytes = Encoding.UTF8.GetBytes(iv); byte[] salt = Encoding.UTF8.GetBytes(iv); byte[] encryptedBytes; RijndaelManaged rijndaelManaged = new RijndaelManaged(); ICryptoTransform transform = null; try { rijndaelManaged.Mode = CipherMode.CBC; rijndaelManaged.Padding = PaddingMode.PKCS7; rijndaelManaged.KeySize = 128; rijndaelManaged.BlockSize = 128; rijndaelManaged.Key = keyBytes; rijndaelManaged.IV = ivBytes; transform = rijndaelManaged.CreateEncryptor(); encryptedBytes = transform.TransformFinalBlock(textBytes, 0, textBytes.Length); } catch (Exception ex) { throw ex; } finally { rijndaelManaged.Clear(); rijndaelManaged.Dispose(); if (transform != null) transform.Dispose(); } return Convert.ToBase64String(encryptedBytes); } #endregion #region 解密 /// <summary> /// AES 解密(高级加密标准,是下一代的加密算法标准,速度快,安全级别高,目前 AES 标准的一个实现是 Rijndael 算法) /// </summary> /// <param name="encryptedString">密文</param> /// <param name="key">密钥</param> /// <param name="iv">向量</param> public static string Decrypt(string encryptedString, string key, string iv) { if (encryptedString == null || encryptedString.Length == 0) { throw (new Exception("密文不得为空")); } if (string.IsNullOrWhiteSpace(key)) { throw (new Exception("密钥不得为空")); } if (string.IsNullOrWhiteSpace(iv)) { throw (new Exception("向量不得为空")); } if (key.Length < 16) { throw (new Exception("密钥长度不能小于16")); } if (iv.Length < 16) { throw (new Exception("向量长度不能小于16")); } byte[] decryptedBytes; byte[] keyBytes = Encoding.UTF8.GetBytes(key); byte[] ivBytes = Encoding.UTF8.GetBytes(iv); byte[] salt = Encoding.UTF8.GetBytes(iv); byte[] encryptedBytes = Convert.FromBase64String(encryptedString); RijndaelManaged rijndaelManaged = new RijndaelManaged(); ICryptoTransform transform = null; try { rijndaelManaged.Mode = CipherMode.CBC; rijndaelManaged.Padding = PaddingMode.PKCS7; rijndaelManaged.KeySize = 128; rijndaelManaged.BlockSize = 128; rijndaelManaged.Key = keyBytes; rijndaelManaged.IV = ivBytes; transform = rijndaelManaged.CreateDecryptor(); decryptedBytes = transform.TransformFinalBlock(encryptedBytes, 0, encryptedBytes.Length); } catch (Exception ex) { throw ex; } finally { rijndaelManaged.Clear(); rijndaelManaged.Dispose(); if (transform != null) transform.Dispose(); } return Encoding.UTF8.GetString(decryptedBytes); } #endregion } }