/// <summary> /// AES加解密(BouncyCastle模式) /// </summary> /// <param name="aes"></param> /// <param name="forEncryption">是否加密(false为解密)</param> /// <param name="data">待加解密的数据</param> /// <param name="mode">加解密模式</param> /// <param name="padding">填充算法</param> /// <param name="key">密钥</param> /// <param name="iv">向量</param> /// <returns>加解密后数据</returns> public static byte[] CryptoBC(this System.Security.Cryptography.Aes aes, bool forEncryption, byte[] data, CipherModeBC mode, CipherPaddingBC padding, byte[] key, byte[] iv) { return(aes.CryptoBC(forEncryption, data, $"AES/{mode}/{padding}", key, iv)); }
/// <summary> /// AES解密(BouncyCastle模式) /// </summary> /// <param name="aes"></param> /// <param name="str">待解密的字符串(base64编码格式)</param> /// <param name="encoding">待解密的字符串编码格式</param> /// <param name="mode">解密模式</param> /// <param name="padding">填充算法</param> /// <param name="key">密钥</param> /// <param name="iv">向量</param> /// <returns>解密字符串</returns> public static string DecryptBC(this System.Security.Cryptography.Aes aes, string str, Encoding encoding, CipherModeBC mode, CipherPaddingBC padding, byte[] key = null, byte[] iv = null) { var data = Convert.FromBase64String(str); var result = aes.DecryptBC(data, mode, padding, key, iv); return(encoding.GetString(result)); }
/// <summary> /// AES解密(BouncyCastle模式) /// </summary> /// <param name="aes"></param> /// <param name="data">待解密的数据</param> /// <param name="mode">解密模式</param> /// <param name="padding">填充算法</param> /// <param name="key">密钥</param> /// <param name="iv">向量</param> /// <returns>解密后数据</returns> public static byte[] DecryptBC(this System.Security.Cryptography.Aes aes, byte[] data, CipherModeBC mode, CipherPaddingBC padding, byte[] key = null, byte[] iv = null) { return(aes.CryptoBC(false, data, mode, padding, key, iv)); }
/// <summary> /// AES解密(BouncyCastle模式) /// </summary> /// <param name="aes"></param> /// <param name="str">待解密的字符串(base64编码格式)</param> /// <param name="mode">解密模式</param> /// <param name="padding">填充算法</param> /// <param name="key">密钥</param> /// <param name="iv">向量</param> /// <returns>UTF8编码解密字符串</returns> public static string DecryptBC(this System.Security.Cryptography.Aes aes, string str, CipherModeBC mode, CipherPaddingBC padding, byte[] key = null, byte[] iv = null) { return(aes.DecryptBC(str, Encoding.UTF8, mode, padding, key, iv)); }