c# 与java对应的 aes加密(带向量的)
using System; using System.Security.Cryptography; using System.Text; public class AESUtil { /// <summary> /// AES加密 /// </summary> /// <param name="str">需要加密的字符串</param> /// <param name="key">32位密钥</param> /// <param name="iv">向量(一定要问问对面要用什么向量)</param> /// <returns>加密后的字符串</returns> public static string Encrypt(string str, string key, string iv) { byte[] keyArray = Encoding.UTF8.GetBytes(key); byte[] toEncryptArray = Encoding.UTF8.GetBytes(str); var rijndael = new RijndaelManaged(); rijndael.Key = keyArray; rijndael.Mode = CipherMode.CBC; rijndael.Padding = PaddingMode.PKCS7; rijndael.IV = Encoding.UTF8.GetBytes(iv); ICryptoTransform cTransform = rijndael.CreateEncryptor(); byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); return Convert.ToBase64String(resultArray, 0, resultArray.Length); } /// <summary> /// AES解密 /// </summary> /// <param name="str">需要解密的字符串</param> /// <param name="key">32位密钥</param> /// <param name="iv">向量(一定要问问对面要用什么向量)</param> /// <returns>解密后的字符串</returns> public static string Decrypt(string str, string key, string iv) { byte[] keyArray = Encoding.UTF8.GetBytes(key); byte[] toEncryptArray = Convert.FromBase64String(str); var rijndael = new RijndaelManaged(); rijndael.Key = keyArray; rijndael.Mode = CipherMode.CBC; rijndael.Padding = PaddingMode.PKCS7; rijndael.IV = Encoding.UTF8.GetBytes(iv); ICryptoTransform cTransform = rijndael.CreateDecryptor(); byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); return Encoding.UTF8.GetString(resultArray); } }
代码赋值黏贴直接用,楼主已经和对应的java对比了无数遍了,加密后的字符串是一样的