码迷,mamicode.com
首页 > Web开发 > 详细

AES .net 、JS 相互加密解密

时间:2016-01-14 18:51:33      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:

/// <summary>
    /// AES加密
    /// </summary>
    public class AES
    {
        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="toEncrypt"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string Encrypt(string toEncrypt, string key)
        {
            byte[] keyArray = Encoding.UTF8.GetBytes(key);
            byte[] ivArray = keyArray;
            byte[] toEncryptArray = Encoding.UTF8.GetBytes(toEncrypt);

            try
            {
                using (var rDel = new RijndaelManaged
                {
                    Key = keyArray,
                    IV = ivArray,
                    Mode = CipherMode.CBC,
                    Padding = PaddingMode.Zeros
                })
                {
                    using (var cTransform = rDel.CreateEncryptor())
                    {
                        byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
                        return Convert.ToBase64String(resultArray, 0, resultArray.Length);
                    }
                }
            }
            catch (Exception ex)
            {
                //log4net.LogManager.GetLogger(typeof(AES).FullName).Error(ex);
            }

            return null;
        }

        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="toDecrypt"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string Decrypt(string toDecrypt, string key)
        {
            byte[] keyArray = Encoding.UTF8.GetBytes(key);
            byte[] ivArray = keyArray;
            byte[] toEncryptArray = Convert.FromBase64String(toDecrypt);

            try
            {
                using (var rDel = new RijndaelManaged())
                {
                    rDel.Key = keyArray;
                    rDel.IV = ivArray;
                    rDel.Mode = CipherMode.CBC;
                    rDel.Padding = PaddingMode.Zeros;
                    using (var cTransform = rDel.CreateDecryptor())
                    {
                        byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
                        return Encoding.UTF8.GetString(resultArray);
                    }
                }
            }
            catch (Exception ex)
            {
                //log4net.LogManager.GetLogger(typeof(AES).FullName).Error(ex);
            }

            return null;

        }


        /*
         
          <script src="aes.js"></script>
          <script src="md5.js"></script>
          <script src="pad-zeropadding-min.js"></script>
         
            var data = "eVIcVEAnPZtVchn1Md85QQ==";
            var key = CryptoJS.enc.Latin1.parse(‘1111111111111111‘);
            var iv = key;
         *  解密
            var encrypted = CryptoJS.AES.decrypt(data, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.ZeroPadding });
            document.write(encrypted.toString(CryptoJS.enc.Utf8));
         *  加密
            var encrypted = CryptoJS.AES.encrypt(data, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.ZeroPadding });
            document.write(encrypted);
         */
    }

 

AES .net 、JS 相互加密解密

标签:

原文地址:http://www.cnblogs.com/rdst/p/5131043.html

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