I am trying to decrypt selling partner api reports but while decrypting I am getting this error near decipher.final() [Node] Error: error:0606506D:digital envelope routines:EVP_DecryptFinal_ex:wrong final block length
. The api returns the key, iv and the url of the report.
Selling Partner api reference
I tried the solutions mentioned in other threads but still facing issues. I checked the length of both key and iv and it is 32 and 16 respectively.
Here is the code:
var AESCrypt: any = {
decrypt: function (cryptkey: any, iv: any, encryptdata: any) {
var decipher = crypto.createDecipheriv('aes-256-cbc', cryptkey, iv);
// decipher.setAutoPadding(true);
return Buffer.concat([
decipher.update(encryptdata),
decipher.final()
]);
}}
const res = await processRequest({
url: details.url
});
let encrypted_buffer = Buffer.from(res);
const key = Buffer.from(details.encryptionDetails.key, "base64");
const iv = Buffer.from(details.encryptionDetails.initializationVector, "base64");
const decryptedBuff = AESCrypt.decrypt(key, iv, encrypted_buffer);
console.log(decryptedBuff);
Buffer.from()
is not necessary. If it is Base64 encoded, the specification of'base64'
is missing. Currently with UTF8 (the default) the wrong encoding is applied. – Laskowskires
. – Laskowskires
anArrayBuffer
, Base64 encoded etc. or what doesprocessRequest()
return. Maybe you are converting wrong, but this is only a guess. The link does not answer that. – Laskowskires
to the console? Is ArrayBuffer, Uint8Array [...], <Buffer...>, an alphanumeric string (plus +/=) or some gibberish displayed? – LaskowskiBuffer.from(res)
would corrupt the ciphertext, because UTF-8 is used as encoding by default, which damages the ciphertext. You could tryBuffer.from(res, 'binary')
. – LaskowskiBuffer.from()
with a string as ciphertext is wrong in any case. Probably there are other problems. – Laskowski