AES算法是对称可逆加密算法,采用对称分组密码体制。AES所有的钥匙必须 128位(16字节),192位(24字节)或256位(32字节)长。AES算法已经代替DES算法。
public static class AES { /// <summary> /// 检验密钥是否有效长度【16|24|32】 /// </summary> /// <param name="key">密钥</param> /// <returns>bool</returns> public static bool CheckAESKey(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 CheckAESIV(string iv) { if (string.IsNullOrWhiteSpace(iv)) return false; if (16.Equals(iv.Length)) return true; else return false; } /// <summary> /// AES加密 /// </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 AESEncrypt(string data, Encoding dataEncoding, string key, string vector, CipherMode cipherMode = CipherMode.CBC, PaddingMode paddingMode = PaddingMode.PKCS7) { return AESEncrypt(dataEncoding.GetBytes(data), key, vector, cipherMode, paddingMode); } /// <summary> /// AES加密 /// </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 AESEncrypt(byte[] dataBytes, string key, string vector, CipherMode cipherMode = CipherMode.CBC, PaddingMode paddingMode = PaddingMode.PKCS7) { byte[] cryptoBytes = null; try { using (AesManaged aes = new AesManaged()) { 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> /// AES解密 /// </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 AESDecrypt(string data, Encoding dataEncoding, string key, string vector, CipherMode cipherMode = CipherMode.CBC, PaddingMode paddingMode = PaddingMode.PKCS7) { return dataEncoding.GetString(AESDecrypt(data, key, vector, cipherMode, paddingMode)); } /// <summary> /// AES解密 /// </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[] AESDecrypt(string data, string key, string vector, CipherMode cipherMode = CipherMode.CBC, PaddingMode paddingMode = PaddingMode.PKCS7) { byte[] dataBytes = Convert.FromBase64String(data); byte[] cryptoBytes = null; try { using (AesManaged aes = new AesManaged()) { 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; } }