如何使用nodejs加密和解密亚马逊卖家api报告文件

How to encrypt & decrypt amazon seller api report document using nodejs

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

回应

{ “有效载荷”:{ "reportDocumentId": "amzn1.tortuga.3.18666cbf-dfdf-dsfsd-bf4b-sdfdsfsdfsdf.TBWRJF481G44N", “加密细节”:{ “标准”:“AES”, "initializationVector": "nG2rSrj1Ra9e03IStEBkdg==", “关键”:“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%2F2012%2Feu-west3-% %2Faws4_request&X-Amz-Signature=45hjkkjkjdfdsfsd4545jkkjhkj4554j" } }


解密nodejs代码


const crypto = require('crypto');

常量算法='AES';

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

const decipher = crypto.createDecipheriv(算法,密钥,iv);


错误 - IV 长度无效


不知道节点 js,但我已经在 PHP 中实现了这个 API,它对我有用。这可能对您有所帮助。

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

NodeJS 代码

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); });

这可能对您有所帮助。

$key = base64_decode('xxxxx');
$iv = base64_decode('xxxxxx');
$decryptedData = openssl_decrypt(file_get_contents($url), 'AES-256-CBC', $key, OPENSSL_RAW_DATA, $iv);
 
if($compression_algorithm && $compression_algorithm == 'GZIP'){
    $decryptedData = gzdecode($decryptedData);
}