根据项目要求需要对一些敏感特殊的字段进行加密、解密,经过研究决定使用crypto-js中的aes来操作
前端封装一个aes方法
vue安装
npm install crypto-js
或
yarn add crypto-js
封装一个aes方法
import CryptoJS from "crypto-js";
// 后端进行加密解密时,key与iv需要和后端保持一致,key、iv自己定义的公钥
const key = CryptoJS.enc.Utf8.parse('1234567891234568')
const iv = CryptoJS.enc.Utf8.parse('1234567891234568')
export default {
/**
* 加密
* @param {*} word 加密前字符串
* @param {*} keyStr key
* @param {*} ivStr iv
* @return 加密后内容
*/
encrypt(word) {
let srcs = CryptoJS.enc.Utf8.parse(word);
let encrypted = CryptoJS.AES.encrypt(srcs, key, {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.ZeroPadding
});
return CryptoJS.enc.Base64.stringify(encrypted.ciphertext)
},
/**
* 解密
* @param {*} word 已加密字符串
* @param {*} keyStr key
* @param {*} ivStr iv
* @return 解密结果
*/
decrypt(word) {
let base64 = CryptoJS.enc.Base64.parse(word)
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();
},
/**
* Base64 加密
* @param {*} src 明文
* @returns 密文
*/
base64Encrypt(src) {
const encodedWord = CryptoJS.enc.Utf8.parse(src)
return CryptoJS.enc.Base64.stringify(encodedWord)
},
/**
* Base64 解密
* @param {*} src 明文
* @returns 密文
*/
base64Decrypt(src) {
const encodedWord = CryptoJS.enc.Base64.parse(src)
return CryptoJS.enc.Utf8.stringify(encodedWord)
}
}
后端代码
pom文件引入包
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.60</version>
</dependency>
加解密工具类
package com.ljc.base.common.base.utils;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.codec.binary.Base64;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
/**
* @author ljc
* @version 1.0
* @date 2023/1/25 16:27
*/
@Slf4j
public class CryptoUtil {
/***
* key和iv值需要和前端一致
*/
public static final String KEY = "1234567891234568";
public static final String IV = "1234567891234568";
private CryptoUtil() {
}
/**
* 加密方法
*
* @param data 要加密的数据
* @param key 加密key
* @param iv 加密iv
* @return 加密的结果(加密失败返回null)
*/
public static String encrypt(String data, String key, String iv) {
try {
//"算法/模式/补码方式"NoPadding PkcsPadding
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
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);
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key.getBytes(), "AES"), new IvParameterSpec(iv.getBytes()));
byte[] encrypted = cipher.doFinal(plaintext);
return new Base64().encodeToString(encrypted);
} catch (Exception e) {
log.error("加密异常", e);
return null;
}
}
/**
* 解密方法
*
* @param data 要解密的数据
* @param key 解密key
* @param iv 解密iv
* @return 解密的结果(解密失败返回原始值)
*/
public static String desEncrypt(String data, String key, String iv) {
try {
byte[] encrypted = new Base64().decode(data);
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key.getBytes(), "AES"), new IvParameterSpec(iv.getBytes()));
byte[] original = cipher.doFinal(encrypted);
return new String(original).trim();
} catch (Exception e) {
log.error("解密异常", e);
return data;
}
}
}