/// <summary> /// AES 加密 /// </summary> /// <param name="input">待加密的字符串</param> /// <param name="key">加密密钥,要求为32位</param> /// <returns>加密成功返回加密后的字符串,失败 throw</returns> public static string EncryptAES(string CallPassword, string input, string key) { if (CallPassword != GetCallCert()) return ""; byte[] inputData = UTF8Encoding.UTF8.GetBytes(input); RijndaelManaged rijndaelProvider = new RijndaelManaged(); rijndaelProvider.Key = UTF8Encoding.UTF8.GetBytes(key.Substring(0, 32)); rijndaelProvider.IV = AES_IV; ICryptoTransform rijndaelEncrypt = rijndaelProvider.CreateEncryptor(); byte[] encryptedData = rijndaelEncrypt.TransformFinalBlock(inputData, 0, inputData.Length); return Convert.ToBase64String(encryptedData); } /// <summary> /// AES 解密 /// </summary> /// <param name="input">待解密的字符串</param> /// <param name="key">解密密钥,要求为32位,和加密密钥相同</param> /// <returns>解密成功返回解密后的字符串,失败 throw</returns> public static string DecryptAES(string CallPassword, string input, string key) { if (CallPassword != GetCallCert()) return ""; byte[] inputData = Convert.FromBase64String(input); RijndaelManaged rijndaelProvider = new RijndaelManaged(); rijndaelProvider.Key = UTF8Encoding.UTF8.GetBytes(key.Substring(0, 32)); rijndaelProvider.IV = AES_IV; ICryptoTransform rijndaelDecrypt = rijndaelProvider.CreateDecryptor(); byte[] decryptedData = rijndaelDecrypt.TransformFinalBlock(inputData, 0, inputData.Length); return UTF8Encoding.UTF8.GetString(decryptedData); }
AES 加密 解密
时间: 2024-10-09 04:58:53