搭建网站都需要什么,网站建设新闻 常识,网站空间支持功能,网站用户需求文章目录 一、概述 一、概述
Postman内置的Js不支持进行RSA加解密#xff0c;所以需要引入forgeJS来实现。在 Pre-request Script使用以下脚本#xff1a;
// ------ 导入RSA ------
if (!pm.globals.has(forgeJS)) {pm.sendRequest(https://raw.githubu… 文章目录 一、概述 一、概述
Postman内置的Js不支持进行RSA加解密所以需要引入forgeJS来实现。在 Pre-request Script使用以下脚本
// ------ 导入RSA ------
if (!pm.globals.has(forgeJS)) {pm.sendRequest(https://raw.githubusercontent.com/loveiset/RSAForPostman/master/forge.js, (err, res) {if (!err) {pm.globals.set(forgeJS, res.text());executeRSAOperations();}});
} else {executeRSAOperations();
}function executeRSAOperations() {// 引入 forge 库eval(pm.globals.get(forgeJS));// 原始 JSON 数据const jsonData {key1: value1,key2: value2};// 将 JSON 数据转换为字符串const jsonString JSON.stringify(jsonData);// RSA 公钥const publicKeyPem -----BEGIN PUBLIC KEY-----公钥内容-----END PUBLIC KEY-----;// RSA 私钥const privateKeyPem -----BEGIN PRIVATE KEY-----私钥内容-----END PRIVATE KEY-----;// 使用公钥进行 RSA 加密const publicKey forge.pki.publicKeyFromPem(publicKeyPem);const encryptedBytes publicKey.encrypt(forge.util.encodeUtf8(jsonString));const encryptedData forge.util.encode64(encryptedBytes);// 使用私钥进行 RSA 解密const privateKey forge.pki.privateKeyFromPem(privateKeyPem);const decryptedBytes privateKey.decrypt(forge.util.decode64(encryptedData));const decryptedData forge.util.decodeUtf8(decryptedBytes);// 输出加密和解密结果console.log(加密后数据:, encryptedData);console.log(解密后数据:, decryptedData);// 更新请求的 Body 数据为加密后的内容pm.request.body.raw encryptedData;
}