postman请求参数进行加密处理-以aes为例

postman Pre-request Script配置

将以下代码添加到collection或者接口的Pre-request Script

var aesKey = "key";
var aesIv = "iv";

let json = []
// 获取请求参数,加密后再放入json串中
for(key in request.data) {
    if(pm.environment.get(key)) {
        val = encrypt(pm.environment.get(key), aesKey, aesIv)
    } else {
        val = encrypt(request.data[key], aesKey, aesIv)
    }
    json.push({ "key": key, "value": val})
}

// formdata方式请求更新
pm.request.body.update({
    mode: 'formdata',
    formdata: json
});

// 其他请求方式示例
// Raw
// pm.request.body.update('Hello World!');

// URL Encoded
// pm.request.body.update({
//   mode: 'urlencoded',
//   urlencoded: [{
//        key: 'foo',
//        value: 'bar'
//    }]
//});

// postman自带CryptoJS模块进行aes加密
function encrypt(word, keyStr, ivStr) {
    var key = CryptoJS.enc.Utf8.parse(keyStr);
    var iv = CryptoJS.enc.Utf8.parse(ivStr);
    var encrypted = CryptoJS.AES.encrypt(word, key, {
        iv: iv,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.ZeroPadding
    });
    // encrypted = encodeURIComponent(encrypted)
    return encrypted.toString();
}

// postman自带CryptoJS模块进行aes解密
function aesDecrypt(word, keyStr, ivStr){
    var key = CryptoJS.enc.Utf8.parse(keyStr);
    var iv = CryptoJS.enc.Utf8.parse(ivStr);
    var srcs = CryptoJS.enc.Utf8.parse(word);
    var decrypted = CryptoJS.AES.decrypt(srcs, key, { 
        iv: iv,
        mode: CryptoJS.mode.CBC,
        padding:CryptoJS.pad.ZeroPadding 
    });
    return decrypted.toString(CryptoJS.enc.Utf8);
}

部分引用:https://github.com/postmanlabs/postman-app-support/issues/4808

Visits: 3046

发表评论

邮箱地址不会被公开。 必填项已用*标注