为什么要加密传输参数?采用什么加密方式?AES有多少中加密类型?是读者大人们自行百度的哟。
话不多说,既然是踩坑记录,放心肯定是能解决问题的。上代码
后端工具类
import org.apache.commons.codec.binary.Base64;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
public class AESUtils {
public static String encrypt(String sSrc, String sKey) {
if (sKey == null) {
throw new IllegalArgumentException("sSrc不能为空");
}
// 判断Key是否为16位
if (sKey.length() != 16) {
throw new IllegalArgumentException("sKey长度需要为16位");
}
try {
byte[] raw = sKey.getBytes(StandardCharsets.UTF_8);
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
//"算法/模式/补码方式"
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = cipher.doFinal(sSrc.getBytes(StandardCharsets.UTF_8));
//此处使用BASE64做转码功能,同时能起到2次加密的作用。
return new Base64().encodeToString(encrypted);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static String decrypt(String sSrc, String sKey) {
if (sKey == null) {
throw new IllegalArgumentException("sSrc不能为空");
}
// 判断Key是否为16位
if (sKey.length() != 16) {
throw new IllegalArgumentException("sKey长度需要为16位");
}
try {
byte[] raw = sKey.getBytes(StandardCharsets.UTF_8);
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
//先用base64解密
byte[] encrypted1 = new Base64().decode(sSrc);
byte[] original = cipher.doFinal(encrypted1);
String originalString = new String(original, StandardCharsets.UTF_8);
return originalString;
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
public static void main(String[] args) {
//此处使用AES-128-ECB加密模式,key需要为16位。
String cKey = "1234567812345678";
//需要加密的字串
String cSrc = "{\n" +
"\t\"current\": 1,\n" +
"\t\"pageSize\": 10\n" +
"}";
System.out.println(cSrc);
// 加密
String enString = AESUtils.encrypt(cSrc, cKey);
System.out.println("加密后的字串是:" + enString);
// 解密
String DeString = AESUtils.decrypt(enString, cKey);
System.out.println("解密后的字串是:" + DeString);
}
}
前端工具类
export function encryptParam(paramJson) {
//引包的一种方式
let CryptoJS = require("crypto-js");
let str = paramJson;
// 密钥 16 位 与后端一致
let key = '1234567812345678';
//后端用的key是 byte数组,转一下
key = CryptoJS.enc.Utf8.parse(key);
let encrypted = CryptoJS.AES.encrypt(str, key,{
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
});
// encrypted生成的是一个对象,通过ciphertext拿到密文
encrypted = encrypted.ciphertext.toString();
//转成16进制
let encryptedHexStr = CryptoJS.enc.Hex.parse(encrypted);
//base64编码
let base64 = CryptoJS.enc.Base64.stringify(encryptedHexStr);
//返回的密文才可被后端解析
return base64;
}
crypto-js是一个第三方库,安装完node.js后
npm install crypto-js
即可在项目中引用。
这样就完成了简单的AES前后端加密匹配。
前端工具类怎么用?
把这段代码考到一个新建的js文件中,比如就叫EncryptUtils.js,在其他js文件中引入就可以用了。
import {encryptParam} from 'xx路径/EncryptUtils';
通常情况是,前端加密传输,后端解密。