key要16位的字符串。
public class AESHelper { /// <summary> /// AES加密 /// </summary> /// <param name="text">加密字符</param> /// <param name="key">密钥</param> /// <returns></returns> public static string AESEncrypt(string text, string key) { byte[] keyArray = Encoding.UTF8.GetBytes(key); byte[] toEncryptArray = Encoding.UTF8.GetBytes(text); RijndaelManaged rDel = new RijndaelManaged(); rDel.Key = keyArray; rDel.Mode = CipherMode.ECB; rDel.Padding = PaddingMode.PKCS7; ICryptoTransform cTransform = rDel.CreateEncryptor(); byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); return Convert.ToBase64String(resultArray, 0, resultArray.Length); } /// <summary> /// AES解密 /// </summary> /// <param name="text"></param> /// <param name="key">密钥</param> /// <returns></returns> public static string AESDecrypt(string text, string key) { byte[] keyArray = Encoding.UTF8.GetBytes(key); byte[] toEncryptArray = Convert.FromBase64String(text); RijndaelManaged rDel = new RijndaelManaged(); rDel.Key = keyArray; rDel.Mode = CipherMode.ECB; rDel.Padding = PaddingMode.PKCS7; ICryptoTransform cTransform = rDel.CreateDecryptor(); byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); return Encoding.UTF8.GetString(resultArray); } }
标签:AES,加密,string,解密,resultArray,rDel,key,byte,toEncryptArray From: https://www.cnblogs.com/vichin/p/17736154.html