概述
1.引入crypto-js
import AES from 'crypto-js';
2.前端加密函数(附加一个解密函数)
//十六位十六进制数作为密钥
const key = AES.enc.Utf8.parse('0123456789ASDFGH');
//十六位十六进制数作为密钥偏移量
const iv = AES.enc.Utf8.parse('ASDFGH0123456789');
export const encrypt = word => {
const src = AES.enc.Utf8.parse(word);
const encrypted = AES.AES.encrypt(
src,
key,
{
iv: iv,
mode: AES.mode.CBC,
padding: AES.pad.Pkcs7
});
return encrypted.ciphertext.toString().toUpperCase();
};
export const decrypt = word => {
const encryptedHexStr = AES.enc.Hex.parse(word);
const src = AES.enc.Base64.stringify(encryptedHexStr);
const decrypt = AES.AES.decrypt(
src,
key,
{
iv: iv,
mode: AES.mode.CBC,
padding: AES.pad.Pkcs7
});
const decryptedStr = decrypt.toString(AES.enc.Utf8);
return decryptedStr.toString();
};
3.后端解密函数(附加一个加密函数)
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 AESHelper {
// key:必须16个字符,且要和前端保持一致
private final static String KEY = "0123456789ASDFGH";
// 偏移量:必须16个字符,且要和前端保持一致
private final static String IV = "ASDFGH0123456789";
/**
* 加密返回的数据转换成 String 类型
* @param content 明文
*/
public static String encrypt(String content) {
return parseByte2HexStr(Objects.requireNonNull(aesCbcEncrypt(content.getBytes(), KEY.getBytes(), IV.getBytes())));
}
/**
* 将解密返回的数据转换成 String 类型
* @param content Base64编码的密文
*/
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;
}
/**
* 将byte数组转换成16进制String
*/
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();
}
/**
* 将16进制String转换为byte数组
*/
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;
}
}
最后
以上就是忧虑悟空为你收集整理的AES 前后端加解密的全部内容,希望文章能够帮你解决AES 前后端加解密所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复