JAVA代码
package com.hbck.common.utils;
import com.alibaba.fastjson.JSONObject;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.util.Objects;
public class AESUtil {
private final static String KEY = "c6b50dag9r645e63"; // key:必须16个字符,且要和前端保持一致
private final static String IV = "0b0ef659ehb8ea3a"; // 偏移量:必须16个字符,且要和前端保持一致
// public static void main(String[] args) {
// JSONObject map = new JSONObject();
// map.put("username","admin");
// map.put("password","123456");
// //加密
// String encrypted = encrypt(map.toString());
// //解密
// String decrypted = decrypt("密文");
// System.out.println("加密前:" + map);
// System.out.println("加密后:" + encrypted);
// System.out.println("解密后:" + decrypted);
// }
public static String encrypt(String content) {
return parseByte2HexStr(Objects.requireNonNull(aesCbcEncrypt(content.getBytes(), KEY.getBytes(), IV.getBytes())));
}
public static String decrypt(String content) {
return new String(Objects.requireNonNull(aesCbcDecrypt(parseHexStr2Byte(content), KEY.getBytes(), IV.getBytes())));
}
private static byte[] aesCbcEncrypt(byte[] content, byte[] keyBytes, byte[] iv) {
try {
SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
//设置模式,编码,后端为PKCS5Padding,对应前端是Pkcs7
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(iv));
return cipher.doFinal(content);
} catch (Exception e) {
System.out.println("exception:" + e.toString());
}
return null;
}
private static byte[] aesCbcDecrypt(byte[] content, byte[] keyBytes, byte[] iv) {
try {
SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv));
return cipher.doFinal(content);
} catch (Exception e) {
System.out.println("exception:" + e.toString());
}
return null;
}
public static String parseByte2HexStr(byte[] buf) {
StringBuilder sb = new StringBuilder();
for (byte b : buf) {
String hex = Integer.toHexString(b & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
sb.append(hex.toUpperCase());
}
return sb.toString();
}
public static byte[] parseHexStr2Byte(String hexStr) {
if (hexStr.length() < 1) {
return null;
}
byte[] result = new byte[hexStr.length() / 2];
for (int i = 0; i < hexStr.length() / 2; i++) {
int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
result[i] = (byte) (high * 16 + low);
}
return result;
}
}
VUE代码
import CryptoJS from 'crypto-js'
// 需要npm crypto-js
export default {
// AES加密
encrypt(data) {
let key = CryptoJS.enc.Utf8.parse('c6b50dag9r645e63') // key:必须16个字符
let iv = CryptoJS.enc.Utf8.parse('0b0ef659ehb8ea3a') // 偏移量:必须16个字符
let encrypted = CryptoJS.AES.encrypt(data, key, {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
// 返回的是base64格式的密文
return encrypted.ciphertext.toString().toUpperCase();
},
// AES解密
decrypt(data) {
let key = CryptoJS.enc.Utf8.parse('c6b50dag9r645e63') // key:必须16个字符
let iv = CryptoJS.enc.Utf8.parse('0b0ef659ehb8ea3a') // 偏移量:必须16个字符
let encryptedHexStr = CryptoJS.enc.Hex.parse(data);
let srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr);
let decrypted = CryptoJS.AES.decrypt(srcs, key, {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
let decryptedStr = decrypted.toString(CryptoJS.enc.Utf8);
return decryptedStr.toString();
}
}
总结


