/// <summary> /// AESRijndael /// </summary> public class AESRijndael { /// <summary> /// 检验密钥是否有效长度【16|24|32】 /// </summary> /// <param name="key">密钥</param> /// <returns>bool</returns> public static bool CheckRijndaelKey(string key) { if (string.IsNullOrWhiteSpace(key)) return false; if (16.Equals(key.Length) || 24.Equals(key.Length) || 32.Equals(key.Length)) return true; else return false; } /// <summary> /// 检验向量是否有效长度【16】 /// </summary> /// <param name="iv">向量</param> /// <returns>bool</returns> public static bool CheckRijndaelIV(string iv) { if (string.IsNullOrWhiteSpace(iv)) return false; if (16.Equals(iv.Length)) return true; else return false; } /// <summary> /// Rijndael加密 /// </summary> /// <param name="data">被加密的明文</param> /// <param name="dataEncoding">要加密字符串加载时编码方式</param> /// <param name="key">密钥</param> /// <param name="vector">向量</param> /// <param name="cipherMode">对称算法的运算模式</param> /// <param name="paddingMode">对称算法中使用的填充模式</param> /// <returns>返回Base64String密文</returns> public static string RijndaelEncrypt(string data, Encoding dataEncoding, string key, string vector, CipherMode cipherMode = CipherMode.CBC, PaddingMode paddingMode = PaddingMode.PKCS7) { return RijndaelEncrypt(dataEncoding.GetBytes(data), key, vector, cipherMode, paddingMode); } /// <summary> /// Rijndael加密 /// </summary> /// <param name="dataBytes">被加密的明文</param> /// <param name="key">密钥</param> /// <param name="vector">向量</param> /// <param name="cipherMode">对称算法的运算模式</param> /// <param name="paddingMode">对称算法中使用的填充模式</param> /// <returns>返回Base64String密文</returns> public static string RijndaelEncrypt(byte[] dataBytes, string key, string vector, CipherMode cipherMode = CipherMode.CBC, PaddingMode paddingMode = PaddingMode.PKCS7) { byte[] cryptoBytes = null; try { using (RijndaelManaged aes = new RijndaelManaged()) { aes.Key = Encoding.UTF8.GetBytes(key); aes.IV = Encoding.UTF8.GetBytes(vector); aes.Mode = cipherMode; aes.Padding = paddingMode; using (MemoryStream ms = new MemoryStream()) { using (CryptoStream cs = new CryptoStream(ms, aes.CreateEncryptor(), CryptoStreamMode.Write)) { cs.Write(dataBytes, 0, dataBytes.Length); cs.FlushFinalBlock(); cryptoBytes = ms.ToArray(); } } } } catch { cryptoBytes = null; } return Convert.ToBase64String(cryptoBytes); } /// <summary> /// Rijndael解密 /// </summary> /// <param name="data">被解密的密文</param> /// <param name="dataEncoding">解密字符串返回的编码方式</param> /// <param name="key">密钥</param> /// <param name="vector">向量</param> /// <param name="cipherMode">对称算法的运算模式</param> /// <returns>返回指定编码明文</returns> public static string RijndaelDecrypt(string data, Encoding dataEncoding, string key, string vector, CipherMode cipherMode = CipherMode.CBC, PaddingMode paddingMode = PaddingMode.PKCS7) { return dataEncoding.GetString(RijndaelDecrypt(data, key, vector, cipherMode, paddingMode)); } /// <summary> /// Rijndael解密 /// </summary> /// <param name="data">被解密的密文</param> /// <param name="key">密钥</param> /// <param name="vector">向量</param> /// <param name="cipherMode">对称算法的运算模式</param> /// <param name="paddingMode">对称算法中使用的填充模式</param> /// <returns>返回指定编码明文</returns> public static byte[] RijndaelDecrypt(string data, string key, string vector, CipherMode cipherMode = CipherMode.CBC, PaddingMode paddingMode = PaddingMode.PKCS7) { byte[] dataBytes = Convert.FromBase64String(data); byte[] cryptoBytes = null; try { using (RijndaelManaged aes = new RijndaelManaged()) { aes.Key = Encoding.UTF8.GetBytes(key); aes.IV = Encoding.UTF8.GetBytes(vector); aes.Mode = cipherMode; aes.Padding = paddingMode; using (MemoryStream ms = new MemoryStream(dataBytes)) { using (CryptoStream cs = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Read)) { using (MemoryStream originalMemory = new MemoryStream()) { byte[] bufferBytes = new byte[1024]; int bytesRead = 0; while ((bytesRead = cs.Read(bufferBytes, 0, bufferBytes.Length)) > 0) { originalMemory.Write(bufferBytes, 0, bytesRead); } cryptoBytes = originalMemory.ToArray(); } } } } } catch (Exception ex) { cryptoBytes = null; } return cryptoBytes; } }