1

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);
nats
  • 187
  • 2
  • 13
  • Where do you use the URL or have you already downloaded the report and just want to decrypt it? What encoding does the report use? If it is contained in a buffer, `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. – Topaco Jul 07 '21 at 18:33
  • The encoding is AES. Where do I need to add 'base64'?decipher.final('base64')? – nats Jul 08 '21 at 03:40
  • I mean the encoding. AES is not an encoding but an encryption. I refer to `res`. – Topaco Jul 08 '21 at 05:59
  • I don't know much about decryption. The key and iv are base64 encoded. Can you have a look at this https://github.com/amzn/selling-partner-api-docs/blob/main/guides/en-US/use-case-guides/reports-api-use-case-guide/reports-api-use-case-guide-2020-09-04.md#step-2-download-and-decrypt-the-report – nats Jul 08 '21 at 06:32
  • My question is not about encryption/decryption, but encoding. I mean is `res` an `ArrayBuffer`, Base64 encoded etc. or what does `processRequest()` return. Maybe you are converting wrong, but this is only a guess. The link does not answer that. – Topaco Jul 08 '21 at 07:09
  • This processRequest is getting the encoded data from the url they have sent. How do I check whether it is base64 encoded or not? – nats Jul 08 '21 at 08:09
  • Did you output `res` to the console? Is ArrayBuffer, Uint8Array [...], , an alphanumeric string (plus +/=) or some gibberish displayed? – Topaco Jul 08 '21 at 08:18
  • No, its not a buffer. Its like this �ځ��P}Z��☺ow�e':g>��z>��@♥9��♂e4z�=��☼♣�O& – nats Jul 08 '21 at 09:39
  • This looks more like a binary string . If so, `Buffer.from(res)` would corrupt the ciphertext, because UTF-8 is used as encoding by default, which damages the ciphertext. You could try `Buffer.from(res, 'binary')`. – Topaco Jul 08 '21 at 10:00
  • Buffer.from(res, 'binary') this is also giving the same error – nats Jul 08 '21 at 10:41
  • Maybe, but `Buffer.from()` with a string as ciphertext is wrong in any case. Probably there are other problems. – Topaco Jul 08 '21 at 10:59
  • Did you figure this out? I'm struggling with the same issue – Jul Oct 26 '21 at 17:06
  • Yes, I was able to but I don't remember it now. You can take help of this https://www.npmjs.com/package/amazon-sp-api – nats Oct 27 '21 at 05:49

0 Answers0