C#.NET MVC 前端JS的AES加密,JQUERY,AJAX,AES ECB ,AES CBC。

 

前端先引用 CryptoJS 库。




封装一个JS函数:

加密后,向后台API POST请求即可。

完整页面:

@{
    ViewBag.Title = "测试 JS AES 加密";
}

class="jumbotron">

ASP.NET

"button" id="submitBtn" value="测试JS AES" class="btn btn-primary btn-lg" />

后端实体:

namespace WebAppJsAES.Models
{
    public class JsAesReq
    {
        /// 
        /// JS AES 加密后的串
        /// 
        public string ciphertext { get; set; }
    }
}

后端解密工具类:

using System.Security.Cryptography;

namespace WebAppJsAES.Utils
{
    public class ButtonAesUtil
    {
        /// 
        /// AES ECB PKCS7 加密
        /// 
        /// 
        /// 
        /// 
        public static byte[] EncryptECB(byte[] dataByte, byte[] key)
        {
            var _aes = new RijndaelManaged();
            _aes.Key = key;
            _aes.Padding = PaddingMode.PKCS7;
            _aes.Mode = CipherMode.ECB;

            var _crypto = _aes.CreateEncryptor();
            byte[] encrypted = _crypto.TransformFinalBlock(dataByte, 0, dataByte.Length);

            _crypto.Dispose();
            return encrypted;
        }
        /// 
        /// AES ECB PKCS7 解密
        /// 
        /// 
        /// 
        /// 
        public static byte[] DecryptECB(byte[] encryptByte, byte[] key)
        {
            var _aes = new RijndaelManaged();
            _aes.Key = key;
            _aes.Padding = PaddingMode.PKCS7;
            _aes.Mode = CipherMode.ECB;

            var _crypto = _aes.CreateDecryptor();
            byte[] decrypted = _crypto.TransformFinalBlock(
                encryptByte, 0, encryptByte.Length);
            _crypto.Dispose();
            return decrypted;
        }
        /// 
        /// AES CBC PKCS7 加密
        /// 
        /// 
        /// 
        /// 
        /// 
        public static byte[] EncryptCBC(byte[] encryptByte, byte[] key, byte[] iv)
        {
            var _aes = new RijndaelManaged();
            _aes.Key = key;
            _aes.IV = iv;
            _aes.Padding = PaddingMode.PKCS7;
            _aes.Mode = CipherMode.CBC;

            var _crypto = _aes.CreateEncryptor();
            byte[] decrypted = _crypto.TransformFinalBlock(encryptByte, 0, encryptByte.Length);
            _crypto.Dispose();
            return decrypted;
        }
        /// 
        /// AES CBC PKCS7 解密
        /// 
        /// 
        /// 
        /// 
        /// 
        public static byte[] DecryptCBC(byte[] encryptByte, byte[] key, byte[] iv)
        {
            var _aes = new RijndaelManaged();
            _aes.Key = key;
            _aes.IV = iv;
            _aes.Padding = PaddingMode.PKCS7;
            _aes.Mode = CipherMode.CBC;

            var _crypto = _aes.CreateDecryptor();
            byte[] decrypted = _crypto.TransformFinalBlock(
                encryptByte, 0, encryptByte.Length);
            _crypto.Dispose();
            return decrypted;
        }
    }
}

后端API 解密:

[HttpPost]
        public ActionResult About(JsAesReq model)
        {
            string bodyStr = "";
            if (model != null && !string.IsNullOrWhiteSpace(model.ciphertext))
            {
                //aesKey与前端协商好
                string aesKey = "1234567890123456";
                var dataByte = ButtonAesUtil.DecryptECB(Convert.FromBase64String(model.ciphertext), Encoding.UTF8.GetBytes(aesKey));
                bodyStr = Encoding.UTF8.GetString(dataByte);
            }
            return Json(new
            {
                code = "1000",
                msg = "请求成功:" + bodyStr,
            });
        }

效果截图:

image

 -

总结:

AES 的算法:ECB还是CBC、KEY 和 IV 要与后端商量好。

CryptoJS.AES.encrypt 加密出来的是base64字符串。


原文地址: https://www.cveoy.top/t/topic/qF0b 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录