码迷,mamicode.com
首页 > 其他好文 > 详细

AES 加密 解密

时间:2014-07-29 12:25:26      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   for   div   ar   new   log   

        /// <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 加密 解密,布布扣,bubuko.com

AES 加密 解密

标签:style   blog   color   for   div   ar   new   log   

原文地址:http://www.cnblogs.com/Evaniko/p/3874712.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!