Decrypting selling partner api reports
Asked Answered
L

0

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);
Lettie answered 7/7, 2021 at 14:0 Comment(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.Laskowski
The encoding is AES. Where do I need to add 'base64'?decipher.final('base64')?Lettie
I mean the encoding. AES is not an encoding but an encryption. I refer to res.Laskowski
I don't know much about decryption. The key and iv are base64 encoded. Can you have a look at this github.com/amzn/selling-partner-api-docs/blob/main/guides/en-US/…Lettie
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.Laskowski
This processRequest is getting the encoded data from the url they have sent. How do I check whether it is base64 encoded or not?Lettie
Did you output res to the console? Is ArrayBuffer, Uint8Array [...], <Buffer...>, an alphanumeric string (plus +/=) or some gibberish displayed?Laskowski
No, its not a buffer. Its like this �ځ��P}Z��☺ow�e':g>��z>��@♥9��♂e4z�=��☼♣�O&Lettie
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').Laskowski
Buffer.from(res, 'binary') this is also giving the same errorLettie
Maybe, but Buffer.from() with a string as ciphertext is wrong in any case. Probably there are other problems.Laskowski
Did you figure this out? I'm struggling with the same issueUnfair
Yes, I was able to but I don't remember it now. You can take help of this npmjs.com/package/amazon-sp-apiLettie

© 2022 - 2024 — McMap. All rights reserved.