赞
踩
/*
* Copyright (c), 2023, gnozt.cn All rights reserved.
*
*/
import lombok.extern.slf4j.Slf4j;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.DigestException;
import java.security.MessageDigest;
import java.util.Arrays;
@Slf4j
public class AesUtils {
private static final String ALGORITHM = "AES";
/**
* AES 对称加密(RSA非对称加密)
* CBC 有向量 (ECB 无向量)
* PKCS5Padding 填充模式(NoPadding 无填充)
*/
private static final String ALG_AES_CBC_PKCS5 = "AES/CBC/PKCS5Padding";
/**
* 加密
*
* @param dataStr
* @param secret
* @return java.lang.String
* @author slf
* @date 2023/11/7 11:13
*/
public static String encrypt(String dataStr, String secret) {
try {
byte[] input = dataStr.getBytes();
// 以下两个变量不可修改,否则java加密的,node无法解密
byte[] saltData = "DFބf$t:".getBytes();
byte[] preData = "Salted__".getBytes();
MessageDigest md5 = MessageDigest.getInstance("MD5");
final byte[][] keyAndIV = GenerateKeyAndIV(32, 16, 1, saltData, secret.getBytes(StandardCharsets.UTF_8), md5);
SecretKeySpec key = new SecretKeySpec(keyAndIV[0], ALGORITHM);
IvParameterSpec iv = new IvParameterSpec(keyAndIV[1]);
Cipher aesCBC = Cipher.getInstance(ALG_AES_CBC_PKCS5);
aesCBC.init(Cipher.ENCRYPT_MODE, key, iv);
byte[] encrypt = aesCBC.doFinal(input);
byte[] encryptedData = addBytes(saltData, encrypt);
return cn.hutool.core.codec.Base64.encode(addBytes(preData, encryptedData));
} catch (Exception e) {
log.error("aesEncrypt error.param={}", dataStr, e);
return null;
}
}
/**
* 解密
*
* @param encryptedText
* @param secret
* @return java.lang.String
* @author slf
* @date 2023/11/7 11:13
*/
public static String decrypt(String encryptedText, String secret) {
try {
byte[] cipherData = cn.hutool.core.codec.Base64.decode(encryptedText);
byte[] saltData = Arrays.copyOfRange(cipherData, 8, 16);
MessageDigest md5 = MessageDigest.getInstance("MD5");
final byte[][] keyAndIV = GenerateKeyAndIV(32, 16, 1, saltData, secret.getBytes(StandardCharsets.UTF_8), md5);
SecretKeySpec key = new SecretKeySpec(keyAndIV[0], ALGORITHM);
IvParameterSpec iv = new IvParameterSpec(keyAndIV[1]);
byte[] encrypted = Arrays.copyOfRange(cipherData, 16, cipherData.length);
Cipher aesCBC = Cipher.getInstance(ALG_AES_CBC_PKCS5);
aesCBC.init(Cipher.DECRYPT_MODE, key, iv);
byte[] decryptedData = aesCBC.doFinal(encrypted);
return new String(decryptedData, StandardCharsets.UTF_8);
} catch (Exception e) {
log.error("aesDecrypt error.param={}", encryptedText, e);
return null;
}
}
public static byte[][] GenerateKeyAndIV(int keyLength, int ivLength, int iterations, byte[] salt, byte[] password, MessageDigest md) {
int digestLength = md.getDigestLength();
int requiredLength = (keyLength + ivLength + digestLength - 1) / digestLength * digestLength;
byte[] generatedData = new byte[requiredLength];
int generatedLength = 0;
try {
md.reset();
while (generatedLength < keyLength + ivLength) {
if (generatedLength > 0)
md.update(generatedData, generatedLength - digestLength, digestLength);
md.update(password);
if (salt != null)
md.update(salt, 0, 8);
md.digest(generatedData, generatedLength, digestLength);
for (int i = 1; i < iterations; i++) {
md.update(generatedData, generatedLength, digestLength);
md.digest(generatedData, generatedLength, digestLength);
}
generatedLength += digestLength;
}
byte[][] result = new byte[2][];
result[0] = Arrays.copyOfRange(generatedData, 0, keyLength);
if (ivLength > 0)
result[1] = Arrays.copyOfRange(generatedData, keyLength, keyLength + ivLength);
return result;
} catch (DigestException e) {
throw new RuntimeException(e);
} finally {
Arrays.fill(generatedData, (byte) 0);
}
}
public static byte[] addBytes(byte[] data1, byte[] data2) {
byte[] data3 = new byte[data1.length + data2.length];
System.arraycopy(data1, 0, data3, 0, data1.length);
System.arraycopy(data2, 0, data3, data1.length, data2.length);
return data3;
}
public static void main(String[] args) {
String secret = "rw2ZsD5dovIzserdy5LwSw==";
String datastr = "{\n" +
" \"studyInfoId\": \"10203538012\",\n" +
" \"countHours\": 188,\n" +
" \"sdkId_1\": \"e821dd4cd9424bbd86b4b2ab143c0930\"\n" +
"}";
String t = encrypt(datastr, secret);
System.out.println(t);
System.out.println(decrypt(t, secret));
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。