vue 接口参数加密

使用AES加密

新建crypto.js文件 放在utils文件中,挂在到vue原型链里

npm install crypto-js

安装 crypto-js
import CryptoJs from 'crypto-js' //引用AES源码js
import CryptoJs from 'crypto-js' //引用AES源码js

const KEY = CryptoJs.enc.Utf8.parse("-------"); //密钥 密码
const IV = CryptoJs.enc.Utf8.parse("-------");
const CryptoInfo = {
//加密
encryptByAES: data => {
let key = KEY;
let iv = IV;
let srcs = CryptoJs.enc.Utf8.parse(data);
let encrypted = CryptoJs.AES.encrypt(srcs, key, {
iv: iv,
mode: CryptoJs.mode.CBC,
padding: CryptoJs.pad.ZeroPadding
});
// console.log("-=-=-=-", encrypted.ciphertext)
return CryptoJs.enc.Base64.stringify(encrypted.ciphertext) + "-" + new Date().getTime();
},
// 解密, 调用该方法时,传入的data是base64的密文
decryptByAES: data => {
let key = KEY;
let iv = IV;
let base64 = CryptoJs.enc.Base64.parse(data);
let src = CryptoJs.enc.Base64.stringify(base64);

let decrypt = CryptoJs.AES.decrypt(src, key, {
iv: iv,
mode: CryptoJs.mode.CBC,
padding: CryptoJs.pad.ZeroPadding
});

let decryptedStr = decrypt.toString(CryptoJs.enc.Utf8);
return decryptedStr.toString();
}
};
export {
CryptoInfo
}

在页面中使用

this.util.CryptoInfo.encryptByAES('加密信息')

------------------------------记录java配套解决--------------------

1、引入base64依赖

<dependency>
  <groupId>commons-net</groupId>
  <artifactId>commons-net</artifactId>
  <version>3.0.1</version>
</dependency>

 2、添加公共类

 package com.bj58.crm.sqdc.util;  
  
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
 
import org.apache.commons.net.util.Base64;  
  
  
/** 
 * AES工具类 
 * <pre> 
 *   替换的文件:%JDK_HOME%jrelibsecuritylocal_policy.jar 
 * 参考: http://czj4451.iteye.com/blog/1986483 
 */  
public class AESUtil {  
    // 密钥  
    public static String key = "zhelixie16weimim";  
    private static String charset = "utf-8";  
    // 偏移量  
    private static int offset = 16;  
    private static String transformation = "AES/CBC/NoPadding";  
    private static String algorithm = "AES";  
  
    /** 
     * 加密 
     *  
     * @param content 
     * @return 
     */  
    public static String encrypt(String content) {  
        try {
            return encrypt(content, key);
        } catch (Exception e) {
        }
        return null;  
    }  
  
    /** 
     * 解密 
     *  
     * @param content 
     * @return 
     */  
    public static String decrypt(String content) {  
        return decrypt(content, key);  
    }  
  
    /** 
     * 加密 
     *  
     * @param content 
     *            需要加密的内容 
     * @param key 
     *            加密密码 
     * @return 
     */  
    public static String encrypt(String data, String key) throws Exception {
        try {
 
            Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");//"算法/模式/补码方式"NoPadding PkcsPadding
            int blockSize = cipher.getBlockSize();
 
            byte[] dataBytes = data.getBytes();
            int plaintextLength = dataBytes.length;
            if (plaintextLength % blockSize != 0) {
                plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize));
            }
 
            byte[] plaintext = new byte[plaintextLength];
            System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);
 
            SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
            IvParameterSpec ivspec = new IvParameterSpec(key.getBytes());
 
            cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
            byte[] encrypted = cipher.doFinal(plaintext);
 
            return new Base64().encodeToString(encrypted);
 
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
 
    /** 
     * AES(256)解密 
     *  
     * @param content 
     *            待解密内容 
     * @param key 
     *            解密密钥 
     * @return 解密之后 
     * @throws Exception 
     */  
    public static String decrypt(String content, String key) {  
        try {  
  
            SecretKeySpec skey = new SecretKeySpec(key.getBytes(), algorithm);  
            IvParameterSpec iv = new IvParameterSpec(key.getBytes(), 0, offset);  
            Cipher cipher = Cipher.getInstance(transformation);  
            cipher.init(Cipher.DECRYPT_MODE, skey, iv);// 初始化  
            byte[] result = cipher.doFinal(new Base64().decode(content));  
            return new String(result); // 解密  
        } catch (Exception e) {  
        }  
        return null;  
    }  
  
    public static void main(String[] args) throws Exception {  
        String s = "65de2594-9ace-e211-915e-00155d607702";  
        // 加密  
        System.out.println("加密前:" + s);  
        String encryptResultStr = encrypt(s);  
        System.out.println("加密后:" + encryptResultStr);  
        // 解密  
        System.out.println("解密后:" + decrypt("CehzNznR6gQfgwahwUuoog=="));  
    }  
}  
原文地址:https://www.cnblogs.com/king94Boy/p/12987905.html