Skip to main content

Callbacks

Code for decrypt in Node.js:

import CryptoJS from 'crypto-js';

export const decrypt = (encryptedSecret: string, salt: string): string => {
const bytes = CryptoJS.AES.decrypt(encryptedSecret, salt);
return bytes.toString(CryptoJS.enc.Utf8);
};
const { body, headers } = request;
let decryptedBody = null;
if (headers && headers.authorization && body.data) {
const { authorization } = headers;
const decryptedJwtAuthorization: any = this.jwtService.decode(
authorization.slice('Bearer '.length),
);
const { id: walletId, salt, exp } = decryptedJwtAuthorization;
if (!walletId || !salt || !exp) {
throw new UnauthorizedException();
}
if (exp * 1000 < Date.now()) {
throw new UnauthorizedException();
}

const finalSalt = decrypt(salt, walletId);
decryptedBody = JSON.parse(decrypt(body.data, finalSalt));

request.body.data = null;
request.body.decryptedBody = decryptedBody;

Code for decrypt on PHP:

$header = $request->header('Authorization', '');

$position = strrpos($header, 'Bearer ');

$token = null;
if ($position !== false) {
$header = substr($header, $position + 7);

$token = str_contains($header, ',') ? strstr($header, ',', true) : $header;
}

if (!$token) {
return json_encode(['status' => 'ERROR', 'message' => 'No token auth sent']);
}

$parser = new Parser(new JoseEncoder());

try {
$token = $parser->parse($token);
$claims = $token->claims();
$wallet_id = $claims->get('id');
$salt = $claims->get('salt');
$exp = $claims->get('exp');

$aes = new CryptoJsAES();

if (!$wallet_id || !$salt || !$exp) {
return json_encode(['status' => 'ERROR', 'message' => 'Token is not valid']);
}
if ($exp < new DateTime()) {
return json_encode(['status' => 'ERROR', 'message' => 'Token is expired']);
}
} catch (CannotDecodeContent | InvalidTokenStructure | UnsupportedHeaderFound $e) {
return json_encode(['status' => 'ERROR', 'message' => 'Token is not parsed']);
}

$query = (object) $request->all();
if (!$query->data) {
return json_encode(['status' => 'ERROR', 'message' => 'Data not found']);
}

$finalSalt = $aes->cryptoJs_aes_decrypt($salt, $wallet_id);
$body = $aes->cryptoJs_aes_decrypt($query->data, $finalSalt);

$body = json_decode($body);```



<?php

namespace App\Utils;

class CryptoJsAES
{
private function aes_evpKDF($password, $salt, $keySize = 8, $ivSize = 4, $iterations = 1, $hashAlgorithm = "md5")
{
$targetKeySize = $keySize + $ivSize;
$derivedBytes = "";
$numberOfDerivedWords = 0;
$block = NULL;
$hasher = hash_init($hashAlgorithm);
while ($numberOfDerivedWords < $targetKeySize) {
if ($block != NULL) {
hash_update($hasher, $block);
}
hash_update($hasher, $password);
hash_update($hasher, $salt);
$block = hash_final($hasher, TRUE);
$hasher = hash_init($hashAlgorithm);

// Iterations
for ($i = 1; $i < $iterations; $i++) {
hash_update($hasher, $block);
$block = hash_final($hasher, TRUE);
$hasher = hash_init($hashAlgorithm);
}

$derivedBytes .= substr($block, 0, min(strlen($block), ($targetKeySize - $numberOfDerivedWords) * 4));

$numberOfDerivedWords += strlen($block) / 4;
}
return array(
"key" => substr($derivedBytes, 0, $keySize * 4),
"iv" => substr($derivedBytes, $keySize * 4, $ivSize * 4)
);
}

function cryptoJs_aes_decrypt($data, $key)
{
$data = base64_decode($data);
if (substr($data, 0, 8) != "Salted__") {
return false;
}
$salt = substr($data, 8, 8);
$keyAndIV = $this->aes_evpKDF($key, $salt);
$decryptPassword = openssl_decrypt(
substr($data, 16),
"aes-256-cbc",
$keyAndIV["key"],
OPENSSL_RAW_DATA, // base64 was already decoded
$keyAndIV["iv"]
);
return $decryptPassword;
}

function cryptoJs_aes_encrypt($data, $key)
{
$salted = "Salted__";
$salt = openssl_random_pseudo_bytes(8);

$keyAndIV = $this->aes_evpKDF($key, $salt);
$encrypt = openssl_encrypt(
$data,
"aes-256-cbc",
$keyAndIV["key"],
OPENSSL_RAW_DATA, // base64 was already decoded
$keyAndIV["iv"]
);
return base64_encode($salted . $salt . $encrypt);
}
}

Code for decrypt on Python:

python.zip

Code for decrypt on JS:

javascript.zip