108 lines
4.2 KiB
Java
108 lines
4.2 KiB
Java
package com.zhgd.xmgl.util;
|
||
|
||
import cn.hutool.core.util.RandomUtil;
|
||
import cn.hutool.http.HttpRequest;
|
||
import cn.hutool.http.HttpUtil;
|
||
import com.alibaba.fastjson.JSON;
|
||
import com.alibaba.fastjson.JSONObject;
|
||
import org.apache.commons.codec.binary.Base64;
|
||
import org.apache.commons.codec.binary.Hex;
|
||
import org.apache.commons.lang3.StringUtils;
|
||
import org.slf4j.Logger;
|
||
import org.slf4j.LoggerFactory;
|
||
|
||
import javax.crypto.Cipher;
|
||
import javax.crypto.Mac;
|
||
import javax.crypto.SecretKey;
|
||
import javax.crypto.spec.IvParameterSpec;
|
||
import javax.crypto.spec.SecretKeySpec;
|
||
import java.nio.charset.StandardCharsets;
|
||
import java.util.HashMap;
|
||
import java.util.Locale;
|
||
import java.util.Map;
|
||
|
||
/**
|
||
* @author hx
|
||
* @date 2023/9/13 11:12
|
||
*/
|
||
public class EncryptUtils {
|
||
private static final Logger log = LoggerFactory.getLogger(EncryptUtils.class);
|
||
private static final String KEY_ALGORITHM = "AES";
|
||
/**
|
||
* 加密算法 算法名称/加密模式/数据填充方式
|
||
*/
|
||
private static final String DEFAULT_CIPHER_ALGORITHM = "AES/CBC/PKCS5Padding";
|
||
|
||
|
||
/**
|
||
* AES解密
|
||
*
|
||
* @param secret 密钥
|
||
* @param content 解密内容
|
||
* @return Json字符串
|
||
*/
|
||
public static String decryptAes(String secret, String content) {
|
||
try {
|
||
if (StringUtils.isBlank(content)) {
|
||
return null;
|
||
}
|
||
//解析BASE64字符串为byte数组
|
||
byte[] encryptedBytes = Base64.decodeBase64(content);
|
||
//密钥
|
||
byte[] keyBytes = secret.getBytes(StandardCharsets.UTF_8);
|
||
SecretKeySpec keySpec = new SecretKeySpec(keyBytes, KEY_ALGORITHM);
|
||
//AES偏移量
|
||
byte[] initParam = secret.substring(0, 16).getBytes(StandardCharsets.UTF_8);
|
||
IvParameterSpec ivParameterSpec = new IvParameterSpec(initParam);
|
||
Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
|
||
cipher.init(Cipher.DECRYPT_MODE, keySpec, ivParameterSpec);
|
||
byte[] byEnd = cipher.doFinal(encryptedBytes);
|
||
//加密后的byte数组直接转字符串
|
||
return new String(byEnd, StandardCharsets.UTF_8);
|
||
} catch (Exception e) {
|
||
log.error("AES解密错误:" + e.getCause());
|
||
log.error("error:", e);
|
||
}
|
||
return null;
|
||
}
|
||
|
||
public static void main(String[] args) {
|
||
try {
|
||
String projectCode = "5134000000000082";
|
||
String appSecret = "a5c8a5e8df2b4706bea408d0939410e3";
|
||
Map<String, String> params = new HashMap<>();
|
||
String nonce = RandomUtil.randomString(16);
|
||
String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
|
||
String appKey = "b17f24ff026d40949c85a24f4f375d42";
|
||
params.put("appKey", appKey);
|
||
params.put("nonce", nonce);
|
||
params.put("timestamp", timestamp);
|
||
params.put("projectCode", projectCode);
|
||
//签名
|
||
String splicingStr = (nonce + "&" + timestamp + "&" + appKey + "&" + projectCode).toUpperCase(Locale.ROOT);
|
||
SecretKey key = new SecretKeySpec(appSecret.getBytes(StandardCharsets.UTF_8), "HmacMD5");
|
||
Mac mac = Mac.getInstance(key.getAlgorithm());
|
||
mac.init(key);
|
||
byte[] code = mac.doFinal(splicingStr.getBytes(StandardCharsets.UTF_8));
|
||
String sign = Hex.encodeHexString(code);
|
||
JSONObject jsonObject = new JSONObject();
|
||
params.put("sign", sign);
|
||
String toParams = HttpUtil.toParams(params);
|
||
//发送请求
|
||
String bodyResult = HttpRequest.post("http://lznmg.xinyegk.com/api/v1/getCompanyInfo?" + toParams)
|
||
.header("Content-Type", "application/json")
|
||
.body(jsonObject.toJSONString())
|
||
.execute()
|
||
.body();
|
||
JSONObject result = JSON.parseObject(bodyResult);
|
||
//成功之后进行数据解密
|
||
if ("0".equals(result.getString("code")) && result.get("data") != null) {
|
||
String data = decryptAes(appSecret, result.getString("data"));
|
||
log.info("解密之后的数据:" + data);
|
||
}
|
||
} catch (Exception e) {
|
||
log.error("error:", e);
|
||
}
|
||
}
|
||
}
|