-
Notifications
You must be signed in to change notification settings - Fork 4
(lower v2.02) NodeJS module in JScriptor
NGUYEN DUY QUOC KHANH edited this page Feb 28, 2024
·
1 revision
cd C:\Users\admin\test
npm install node-forge
npm install buffer
Import C:\Users\admin\test to Nodejs Library
const forge = require('node-forge')
var Buffer = require('buffer/').Buffer
const I = forge.random.getBytesSync(32), V = forge.random.getBytesSync(16);
var defaultPublicKey = ""
function encryptRequest(bodyJson) {
m = bodyJson
const w = forge.cipher.createCipher('AES-CTR', I);
w.start({
iv: V
}),
w.update(forge.util.createBuffer(forge.util.encodeUtf8(JSON.stringify(m)))),
w.finish();
const S = Buffer.concat([Buffer.from(V, 'binary'),
Buffer.from(w.output.data, 'binary')
]),
_ = forge.pki.publicKeyFromPem(forge.util.decode64(defaultPublicKey)).encrypt(forge.util.encode64(I));
return {
d: S.toString('base64'),
k: forge.util.encode64(_)
}
}
function modifiedRequest(){
bodyJson = JSON.parse(jsrequest.bodyToString());
modifiedBody = JSON.stringify(encryptRequest(bodyJson));
jsresult.request = jsrequest.withBody(modifiedBody);
return jsresult;
}
modifiedRequest()const forge = require('node-forge')
var Buffer = require('buffer/').Buffer
const clientPrivateKey = ""
function decryptResponse(k, d){
m = {
"k": k,
"d": d
};
const {
k: I,
d: V
} = m;
w = forge.pki.privateKeyFromPem(clientPrivateKey);
S = forge.util.decodeUtf8(w.decrypt(forge.util.decode64(I)));
D = Buffer.from(V, 'base64');
_ = D.slice(0, 16);
O = D.slice(16);
let g = forge.cipher.createDecipher('AES-CTR', Buffer.from(S, 'base64').toString('binary'));
g.start({
iv: _.toString('binary')
})
g.update(forge.util.createBuffer(O));
g.finish();
return g.output.data
}
function modifiedResponse(){
bodyJson = JSON.parse(jsresponse.bodyToString());
let modifiedBody = JSON.parse(decryptResponse(JSON.stringify(bodyJson.k), JSON.stringify(bodyJson.d)));
modifiedBody.EncryptStr = JSON.parse(modifiedBody.EncryptStr);
let new_raw_body = JSON.stringify(modifiedBody);
jsresult.response = jsresponse.withBody(new_raw_body);
return jsresult;
}
modifiedResponse();@Copyright ngduyquockhanh