api-test/1.js
2024-10-14 00:16:08 +08:00

230 lines
6.8 KiB
JavaScript

const crypto = require('crypto');
const axios = require('axios');
const inquirer = require('inquirer');
// AES CBC 加密函数,返回 Base64
function aesEncrypt(plainText, key) {
const keyBuffer = Buffer.from(key, 'hex'); // 将16进制的密钥转换为 Buffer
const blockSize = 16; // AES 块大小
const iv = crypto.randomBytes(blockSize); // 生成随机 IV
// PKCS7 填充
function pkcs7Padding(text, blockSize) {
const padding = blockSize - (text.length % blockSize);
const padtext = Buffer.alloc(padding, padding);
return Buffer.concat([text, padtext]);
}
const cipher = crypto.createCipheriv('aes-128-cbc', keyBuffer, iv); // 创建加密器
cipher.setAutoPadding(true);
let encrypted = cipher.update(plainText);
encrypted = Buffer.concat([iv, encrypted, cipher.final()]); // 拼接 IV 和加密数据
return encrypted.toString('base64'); // 返回 Base64 编码的加密结果
}
// AES CBC 解密函数,返回解密后的明文
function aesDecrypt(encryptedText, key) {
const encryptedBuffer = Buffer.from(encryptedText, 'base64'); // 将Base64字符串转换为Buffer
const keyBuffer = Buffer.from(key, 'hex'); // 将16进制密钥转换为 Buffer
const blockSize = 16; // AES 块大小
const iv = encryptedBuffer.slice(0, blockSize); // 提取出IV
const encryptedData = encryptedBuffer.slice(blockSize); // 剩下的是加密数据
const decipher = crypto.createDecipheriv('aes-128-cbc', keyBuffer, iv); // 创建解密器
decipher.setAutoPadding(true);
let decrypted = decipher.update(encryptedData);
decrypted = Buffer.concat([decrypted, decipher.final()]); // 解密后的数据
return decrypted.toString(); // 返回解密后的明文字符串
}
// 模拟请求参数
const mockData = {
"FLXG3D56": {
mobile_no: "18276151590",
id_card: "45212220000827423X",
name: "张荣宏",
time_range: "5"
},
"FLXG54F5": {
mobile_no: "13812345678"
},
"FLXG162A": {
mobile_no: "13812345678",
id_card: "110101199003076534",
name: "张三"
},
"FLXG970F": {
id_card: "110101199003076534",
name: "李四"
},
"FLXG5876": {
mobile_no: "13812345678"
},
"FLXG9687": {
mobile_no: "13812345678",
id_card: "110101199003076534",
name: "王五"
},
"FLXGC9D1": {
mobile_no: "13812345678",
id_card: "110101199003076534",
name: "赵六"
},
"FLXGCA3D": {
id_card: "110101199003076534",
name: "李四"
},
"FLXGDEC7": {
id_card: "110101199003076534",
name: "张三"
},
"IVYZ0B03": {},
"IVYZ385E": {
id_card: "110101199003076534",
name: "张三"
},
"IVYZ5733": {
name: "李四",
id_card: "110101199003076534"
},
"IVYZ9363": {
man_name: "张三",
man_id_card: "110101199003076534",
woman_name: "王五",
woman_id_card: "110101199003076535"
},
"JRZQ0A03": {
mobile_no: "13812345678",
id_card: "110101199003076534",
name: "张三"
},
"JRZQ4AA8": {
mobile_no: "13812345678",
id_card: "110101199003076534",
name: "王五"
},
"JRZQ8203": {
mobile_no: "13812345678",
id_card: "110101199003076534",
name: "赵六"
},
"JRZQDBCE": {
mobile_no: "13812345678",
id_card: "110101199003076534",
bank_card: "6212261901001234567",
name: "张三"
},
"QYGL2ACD": {
ent_name: "某企业",
legal_person: "李四",
ent_code: "91310000123456789X"
},
"QYGL6F2D": {
id_card: "110101199003076534"
},
"QYGL45BD": {
ent_name: "某公司",
legal_person: "王五",
ent_code: "91310000123456789X",
id_card: "110101199003076534"
},
"QYGL8261": {
ent_name: "某公司"
},
"QYGLB4C0": {
id_card: "110101199003076534"
},
"YYSY4B37": {
mobile_no: "13812345678"
},
"YYSY6F2E": {
mobile_no: "13812345678",
mobile_type: "移动",
id_card: "110101199003076534",
name: "张三"
},
"YYSY09CD": {
mobile_no: "13812345678",
mobile_type: "联通",
id_card: "110101199003076534",
name: "李四"
},
"YYSYBE08": {
mobile_no: "13812345678",
name: "张三"
},
"YYSYD50F": {
mobile_no: "13812345678",
id_card: "110101199003076534"
},
"YYSYF7DB": {
mobile_no: "13812345678",
start_date: "2022-01-01"
}
};
const interfaceChoices = Object.keys(mockData);
// 选择接口和加密数据
async function requestAPI(interfaceName) {
const url = `http://api.tianyuanapi.com/api/v1/${interfaceName}`;
// const url = `http://127.0.0.1:10003/api/v1/${interfaceName}`;
const accessId = 'aa16cc6e9da90461';
const key = 'ff83609b2b24fc73196aac3d3dfb874f'; // AES 16进制密钥
// 模拟请求参数
const data = mockData[interfaceName];
if (!data) {
console.log(`接口 ${interfaceName} 的模拟数据未定义`);
return;
}
// 将数据转换为 JSON 并加密
const jsonStr = JSON.stringify(data);
console.log("jsonStr", jsonStr)
const encryptedData = aesEncrypt(jsonStr, key);
// 发送请求
try {
const response = await axios.post(url, {
data: encryptedData
}, {
headers: {
'Access-Id': accessId,
'Content-Type': 'application/json'
}
});
console.log(`接口 ${interfaceName} 返回:`, response.data);
const encryptedResponseData = response.data.data;
const decryptedData = aesDecrypt(encryptedResponseData, key);
// 解析解密后的字符串并结构化输出
try {
const jsonData = JSON.parse(decryptedData); // 将解密后的字符串转换为 JSON 对象
console.log(`接口 ${interfaceName} 返回解密后的数据:`);
console.log(JSON.stringify(jsonData, null, 2)); // 格式化输出,缩进为 2
} catch (parseError) {
console.error("解密后的数据无法解析为 JSON:", parseError);
console.log("解密后的数据:", decryptedData);
}
} catch (error) {
console.error(`请求接口 ${interfaceName} 失败:`, error);
}
}
// 使用 inquirer 进行接口选择
async function selectInterface() {
const answers = await inquirer.prompt([
{
type: 'list',
name: 'selectedInterface',
message: '请选择要请求的接口:',
choices: interfaceChoices
}
]);
const interfaceName = answers.selectedInterface;
await requestAPI(interfaceName);
}
// 运行接口选择
selectInterface();