using System.Security.Cryptography; using System.Text; namespace LMS.Common.RSAKey { public static class AESGenerate { public static string Encrypt(string plainText, byte[] key, byte[] iv) { using Aes aesAlg = Aes.Create(); aesAlg.Key = key; aesAlg.IV = iv; // 使用全零的IV,实际使用时应该使用随机IV ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV); byte[] encrypted = encryptor.TransformFinalBlock(Encoding.UTF8.GetBytes(plainText), 0, plainText.Length); return Convert.ToBase64String(encrypted); } public static string Decrypt(string cipherText, byte[] key, byte[] iv) { using Aes aesAlg = Aes.Create(); aesAlg.Key = key; aesAlg.IV = iv; // 使用全零的IV,需要与加密时使用的IV一致 ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV); byte[] cipher = Convert.FromBase64String(cipherText); byte[] decrypted = decryptor.TransformFinalBlock(cipher, 0, cipher.Length); return Encoding.UTF8.GetString(decrypted); } } }