Appearance
代码示例
java
package com.my.mytest.servcie.impl;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.my.mytest.utils.AESDeclareUtil;
public class DeclareServiceMain {
public static void main(String[] args) {
query();
s008();
}
public static void query() {
JSONObject jsonObject = new JSONObject();
jsonObject.put("appKey", "yourAppKey");
jsonObject.put("serviceId", "S004");
JSONObject jsonDataDTO = new JSONObject();
jsonDataDTO.put("serviceId", "S008");
jsonDataDTO.put("nsqxdm", "02");
jsonDataDTO.put("nsrsbh", "yourNsrsbh");
jsonDataDTO.put("skssqq", "2025-03-01");
jsonDataDTO.put("skssqz", "2025-03-31");
jsonDataDTO.put("sbzlbh", "10101");
jsonObject.put("jsonData", jsonDataDTO);
try {
HttpRequest httpRequest = HttpRequest.post("http://localhost:8080/declare/S004")
.header("appKey", "yourAppKey")
.header("Content-Type", "application/json")
.body(AESDeclareUtil.encryptAndUrlEncode(jsonObject.toJSONString(), "yourAppSecret"));
HttpResponse response = httpRequest.execute();
String result = response.body();
System.out.println("Response: " + result);
String data = JSON.parseObject(result).getString("data");
if (ObjectUtil.isNotEmpty(data)) {
System.out.println("data: " + AESDeclareUtil.decodeAndDecrypt(data, new StringBuilder("yourAppSecret").reverse().toString()));
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void s008() {
JSONObject jsonObject = new JSONObject();
jsonObject.put("appKey", "yourAppKey");
jsonObject.put("serviceId", "S008");
JSONObject jsonDataDTO = new JSONObject();
jsonDataDTO.put("nsqxdm", "02");
jsonDataDTO.put("nsrsbh", "yourNsrsbh");
jsonDataDTO.put("skssqq", "2025-03-01");
jsonDataDTO.put("skssqz", "2025-03-31");
jsonDataDTO.put("sbzlbh", "10101");
jsonDataDTO.put("zfbz", "N");
jsonDataDTO.put("wszmlx", "0");
jsonDataDTO.put("kjlx", "0");
JSONObject kqybs = new JSONObject();
kqybs.put("kqybslx", "2");
kqybs.put("zgswjg", " ");
kqybs.put("byglbh", "");
kqybs.put("scjydz", " ");
jsonDataDTO.put("kqybs", kqybs);
jsonObject.put("jsonData", jsonDataDTO);
try {
HttpRequest httpRequest = HttpRequest.post("http://localhost:8080/declare/S008")
.header("appKey", "yourAppKey")
.header("Content-Type", "application/json")
.body(AESDeclareUtil.encryptAndUrlEncode(JSON.toJSONString(jsonObject), "yourAppSecret"));
HttpResponse response = httpRequest.execute();
String result = response.body();
System.out.println("Response: " + result);
String data = JSON.parseObject(result).getString("data");
if (ObjectUtil.isNotEmpty(data)) {
System.out.println("data: " + AESDeclareUtil.decodeAndDecrypt(data, new StringBuilder("yourAppSecret").reverse().toString()));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
java
package com.my.mytest.utils;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
public class AESDeclareUtil {
// AES 加密(ECB 模式)
public static String encryptAES(String data, String key) throws Exception {
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); // 无 IV
SecretKeySpec secretKey = new SecretKeySpec(padKey(key).getBytes(StandardCharsets.UTF_8), "AES");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] encrypted = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8));
return Base64.getEncoder().encodeToString(encrypted);
}
// AES 解密
public static String decryptAES(String base64EncryptedData, String key) throws Exception {
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
SecretKeySpec secretKey = new SecretKeySpec(padKey(key).getBytes(StandardCharsets.UTF_8), "AES");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] decoded = Base64.getDecoder().decode(base64EncryptedData);
byte[] original = cipher.doFinal(decoded);
return new String(original, StandardCharsets.UTF_8);
}
// URLEncoder 包装
public static String encryptAndUrlEncode(String plainJson, String publicKey) throws Exception {
String encrypted = encryptAES(plainJson, publicKey);
return URLEncoder.encode(encrypted, StandardCharsets.UTF_8.toString());
}
// URLDecoder 解码 + 解密
public static String decodeAndDecrypt(String encryptedUrlEncoded, String publicKey) throws Exception {
String decoded = URLDecoder.decode(encryptedUrlEncoded, StandardCharsets.UTF_8.toString());
return decryptAES(decoded, publicKey);
}
// 确保 key 长度为 16/24/32 字节(补齐)
private static String padKey(String key) {
int len = key.length();
if (len == 16 || len == 24 || len == 32) {
return key;
}
if (len > 32) {
return key.substring(0, 32);
}
return String.format("%-32s", key).replace(' ', '0');
}
}