首页 > 代码库 > AES 加密 解密

AES 加密 解密

        /// <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 =http://www.mamicode.com/ 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 = http://www.mamicode.com/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 =http://www.mamicode.com/ 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 = http://www.mamicode.com/rijndaelDecrypt.TransformFinalBlock(inputData, 0, inputData.Length);            return UTF8Encoding.UTF8.GetString(decryptedData);        }