How to decode a cipher text using aes-128-cbc algorithm in nginx js?

27 Views Asked by At

I want to decode some user authentication information in request using nginx js, but i found nginx js doesn't support crypto.createDecipheriv method, should i write the decipher by myself?

here is my code and i get a exception:js exception: TypeError: (intermediate value)["createDecipheriv"] is not a function and https://nginx.org/en/docs/njs/compatibility.html also say it only support crypto.createHash and crypto.createHmac

function decodeToken(token, timestamp, appsecret) {
  let hash = crypto.createHash('md5').update(timestamp + appsecret).digest('hex');
  let aesSecret = hash.slice(0, 16);
  let iv = hash.slice(-16);
  return decrypt(aesSecret, iv, token);
}

function decrypt (key, iv, cipher) {
  const cryptedBuffer = Buffer.from(cipher, 'base64').toString('binary');
  const decipher = crypto.createDecipheriv('aes-128-cbc', key, iv);
  let decoded = decipher.update(cryptedBuffer, 'binary', 'utf8');
  decoded += decipher.final('utf8');
  return decoded;
}
0

There are 0 best solutions below