前端进行AES加密接口进行解密

我这里使用的是"crypto-js": "4.0.0"

1.在 axios.interceptors.response.use 返回接口中进行解密

            const CryptoJS = require('crypto-js');  //引用AES源码js
                
            const key = CryptoJS.enc.Utf8.parse("key111");
            const iv = CryptoJS.enc.Utf8.parse('iv111');

            //AES解密方法
            function Decrypt(word) {
                let decrypt = CryptoJS.AES.decrypt(word, key, { iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 });
                let decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
                return decryptedStr;
            }
            // 返回结果只包含"data","success","abc"这3个key,并且"success"==true,"abc"==true 就把data解密,把解密后的结果返回
                        const keys = Object.keys(data);
                        if (keys.length === 3 && keys.includes('data') && keys.includes('success') && keys.includes('abc')) {
                            if (data.success && data.abc) {
                                response.data = JSON.parse(Decrypt(data.data));
                            }
                        }

使用AES加密

import CryptoJS from 'crypto-js'
cosnt data=this.aesEncrypt(params.data)//加密后返回的数据
aesEncrypt(message) {
            const ivs = CryptoJS.enc.Utf8.parse('1234567890111111');
            const encrypted = CryptoJS.AES.encrypt(message,
                CryptoJS.enc.Utf8.parse('d5e6b231d8c26g34'),
                {
                    iv: ivs,
                    padding: CryptoJS.pad.Pkcs7,
                    mode: CryptoJS.mode.CBC
                });
            return encrypted;//
        },
posted @ 2021-12-14 17:17  Empress&  阅读(779)  评论(0编辑  收藏  举报