2
votes

seller api /reports/2020-09-04/documents/amzn1.tortuga.3.18666cbf-dfdf-dsfsd-bf4b-sdfdsfsdfsdf.TBWRJF481G44N

response

{ "payload": { "reportDocumentId": "amzn1.tortuga.3.18666cbf-dfdf-dsfsd-bf4b-sdfdsfsdfsdf.TBWRJF481G44N", "encryptionDetails": { "standard": "AES", "initializationVector": "nG2rSrj1Ra9e03IStEBkdg==", "key": "9CT0qwtzUHLXlFTh0aLxk4qSQYAJ7texG8KDIZ0JSy8=" }, "url": "https://tortuga-prod-eu.s3-eu-west-1.amazonaws.com/%2FNinetyDays/amzn1.tortuga.3.18666cbf-e671-asds-sdfs-dsfdsfdsfds.sdvfdsdssdfsd?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Date=20201208T192515Z&X-Amz-SignedHeaders=host&X-Amz-Expires=300&X-Amz-Credential=dscfdsfsdfsdfdsfsdf%2F20201208%2Feu-west-1%2Fs3%2Faws4_request&X-Amz-Signature=45hjkkjkjdfdsfsd4545jkkjhkj4554j" } }


decryption nodejs code


const crypto = require('crypto');

const algorithm = 'AES';

const key = '9CT0qwtzUHLXlFTh0aLxk4qSQYAJ7texG8KDIZ0JSy8='; const iv = 'nG2rSrj1Ra9e03IStEBkdg==';

const decipher = crypto.createDecipheriv(algorithm, key, iv);


Error - Invalid IV length


2

2 Answers

5
votes

No idea about node js but I have implemented this API in PHP and it works for me. This might be helpful for you.

$key = base64_decode('xxxxx');
$iv = base64_decode('xxxxxx');
$decryptedData = openssl_decrypt(file_get_contents($url), 'AES-256-CBC', $key, OPENSSL_RAW_DATA, $iv);
0
votes

NodeJS code

const zlib = require('zlib');
const fs = require('fs');


let key = ((new Buffer.from((document.encryptionDetails.key), 'base64')));
let initializationVector = ((new Buffer.from((document.encryptionDetails.initializationVector), 'base64')));

let params = { method: `get`, url: `${document.url}`, responseType: 'stream', headers: { "Accept-Encoding": "gzip, deflate, br" }, encoding: null }

let documentResponse = await axios.request(params);

var decipher = crypto.createDecipheriv('aes-256-cbc', key, initializationVector);
decipher.setAutoPadding(false);
var gunzip = zlib.createGunzip();

            
const filePath = `/<path>/<fileName>`;

var writerOne = fs.createWriteStream(filePath);
if (document.compressionAlgorithm) {   (documentResponse.data).pipe(decipher).pipe(gunzip).pipe(writerOne); }
else { (documentResponse.data).pipe(decipher).pipe(writerOne); }

gunzip.on('error', error => { console.error(error.message || error); writerOne.close(); });
writerOne.on('error', error => { console.error(error.message || error); writerOne.close(); reject(`${error.message || error}`); });
writerOne.on('close', () => { resolve(filePath); });