130 lines
4.9 KiB
Java
130 lines
4.9 KiB
Java
|
|
import javax.crypto.Cipher;
|
||
|
|
import javax.crypto.spec.IvParameterSpec;
|
||
|
|
import javax.crypto.spec.SecretKeySpec;
|
||
|
|
import java.net.HttpURLConnection;
|
||
|
|
import java.net.URL;
|
||
|
|
import java.nio.charset.StandardCharsets;
|
||
|
|
import java.util.Base64;
|
||
|
|
import java.util.Random;
|
||
|
|
import java.io.OutputStream;
|
||
|
|
import java.io.BufferedReader;
|
||
|
|
import java.io.InputStreamReader;
|
||
|
|
import org.json.JSONObject;
|
||
|
|
|
||
|
|
public class Demo {
|
||
|
|
|
||
|
|
// 加密
|
||
|
|
public static String aesEncrypt(String plainText, String hexKey) throws Exception {
|
||
|
|
byte[] keyBytes = hexStringToByteArray(hexKey);
|
||
|
|
byte[] iv = new byte[16];
|
||
|
|
new SecureRandom().nextBytes(iv);
|
||
|
|
|
||
|
|
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
|
||
|
|
SecretKeySpec secretKey = new SecretKeySpec(keyBytes, "AES");
|
||
|
|
IvParameterSpec ivSpec = new IvParameterSpec(iv);
|
||
|
|
cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivSpec);
|
||
|
|
|
||
|
|
byte[] encrypted = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8));
|
||
|
|
|
||
|
|
// 拼接 IV 和密文
|
||
|
|
byte[] encryptedData = new byte[iv.length + encrypted.length];
|
||
|
|
System.arraycopy(iv, 0, encryptedData, 0, iv.length);
|
||
|
|
System.arraycopy(encrypted, 0, encryptedData, iv.length, encrypted.length);
|
||
|
|
|
||
|
|
return Base64.getEncoder().encodeToString(encryptedData);
|
||
|
|
}
|
||
|
|
|
||
|
|
// 解密
|
||
|
|
public static String aesDecrypt(String encryptedText, String hexKey) throws Exception {
|
||
|
|
byte[] encryptedBytes = Base64.getDecoder().decode(encryptedText);
|
||
|
|
byte[] keyBytes = hexStringToByteArray(hexKey);
|
||
|
|
|
||
|
|
byte[] iv = new byte[16];
|
||
|
|
System.arraycopy(encryptedBytes, 0, iv, 0, 16);
|
||
|
|
|
||
|
|
byte[] encryptedData = new byte[encryptedBytes.length - 16];
|
||
|
|
System.arraycopy(encryptedBytes, 16, encryptedData, 0, encryptedData.length);
|
||
|
|
|
||
|
|
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
|
||
|
|
SecretKeySpec secretKey = new SecretKeySpec(keyBytes, "AES");
|
||
|
|
IvParameterSpec ivSpec = new IvParameterSpec(iv);
|
||
|
|
cipher.init(Cipher.DECRYPT_MODE, secretKey, ivSpec);
|
||
|
|
|
||
|
|
byte[] decrypted = cipher.doFinal(encryptedData);
|
||
|
|
return new String(decrypted, StandardCharsets.UTF_8);
|
||
|
|
}
|
||
|
|
|
||
|
|
// Helper 方法,将 16 进制字符串转为字节数组
|
||
|
|
public static byte[] hexStringToByteArray(String s) {
|
||
|
|
int len = s.length();
|
||
|
|
byte[] data = new byte[len / 2];
|
||
|
|
for (int i = 0; i < len; i += 2) {
|
||
|
|
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i + 1), 16));
|
||
|
|
}
|
||
|
|
return data;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 发送 HTTP POST 请求
|
||
|
|
public static String sendPostRequest(String urlString, String data, String accessId) throws Exception {
|
||
|
|
URL url = new URL(urlString);
|
||
|
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||
|
|
connection.setRequestMethod("POST");
|
||
|
|
connection.setRequestProperty("Content-Type", "application/json");
|
||
|
|
connection.setRequestProperty("Access-Id", accessId);
|
||
|
|
connection.setDoOutput(true);
|
||
|
|
|
||
|
|
// 构造请求体
|
||
|
|
String jsonInputString = "{\"data\":\"" + data + "\"}";
|
||
|
|
try (OutputStream os = connection.getOutputStream()) {
|
||
|
|
byte[] input = jsonInputString.getBytes("utf-8");
|
||
|
|
os.write(input, 0, input.length);
|
||
|
|
}
|
||
|
|
|
||
|
|
// 读取响应
|
||
|
|
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
|
||
|
|
StringBuilder response = new StringBuilder();
|
||
|
|
String responseLine;
|
||
|
|
while ((responseLine = br.readLine()) != null) {
|
||
|
|
response.append(responseLine.trim());
|
||
|
|
}
|
||
|
|
|
||
|
|
return response.toString();
|
||
|
|
}
|
||
|
|
|
||
|
|
public static void main(String[] args) {
|
||
|
|
try {
|
||
|
|
// 设置 URL、密钥和请求参数
|
||
|
|
String url = "https://api.tianyuanapi.com/api/v1/IVYZ5733";
|
||
|
|
String accessId = "XXXXXXXXXXXXX";
|
||
|
|
String key = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
|
||
|
|
|
||
|
|
// 请求参数
|
||
|
|
JSONObject requestParams = new JSONObject();
|
||
|
|
requestParams.put("name", "李四");
|
||
|
|
requestParams.put("id_card", "110101199003076534");
|
||
|
|
|
||
|
|
// 将请求参数转为 JSON 字符串
|
||
|
|
String jsonStr = requestParams.toString();
|
||
|
|
|
||
|
|
// 加密请求数据
|
||
|
|
String encryptedData = aesEncrypt(jsonStr, key);
|
||
|
|
|
||
|
|
// 发送 HTTP POST 请求并获取响应
|
||
|
|
String response = sendPostRequest(url, encryptedData, accessId);
|
||
|
|
|
||
|
|
// 解析响应数据
|
||
|
|
JSONObject responseData = new JSONObject(response);
|
||
|
|
String encryptedResponseData = responseData.optString("data");
|
||
|
|
|
||
|
|
// 解密返回的加密数据
|
||
|
|
if (encryptedResponseData != null) {
|
||
|
|
String decryptedResponseData = aesDecrypt(encryptedResponseData, key);
|
||
|
|
System.out.println("Decrypted Response Data: " + decryptedResponseData);
|
||
|
|
}
|
||
|
|
|
||
|
|
} catch (Exception e) {
|
||
|
|
e.printStackTrace();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|