amzn / selling-partner-api-models

This repository contains OpenAPI models for developers to use when developing software to call Selling Partner APIs.
Apache License 2.0
618 stars 742 forks source link

Does anyone have a working VB.net/C# AES encryption function for feed upload? #1600

Closed nightcoding2021 closed 3 years ago

nightcoding2021 commented 3 years ago

This is getting confusing. Amazon are returning a base64 version of key and IV. I cannot figure out how to present those to an encryption function. The Amazon documentation is totally useless on this point. How do they even want the data uploaded? Base64 encoded or what? It's total guesswork. This is a complete regression from MWS, now we have all these dumb extra steps which are obviously not user friendly at all!

I've been using the below, but something is wrong - the file uploads report The uploaded document has an illegal block size.. The strKey and strIV values below are the same base64 encoded strings the createFeedDocument call returns.

Could someone help correct the below code?

Public Function AES_Encrypt(ByVal strData As String, ByVal strKey As String, ByVal strIV As String) As String
    Try
        Dim aes As New RijndaelManaged
        aes.KeySize = 256

        Dim key() As Byte = Convert.FromBase64String(strKey)
        Dim IV() As Byte = Convert.FromBase64String(strIV)

        aes.Key = key
        aes.IV = IV

        Using ms As New MemoryStream
            ms.WriteByte(aes.Key.Length)
            ms.Write(aes.Key, 0, aes.Key.Length)
            ms.WriteByte(aes.IV.Length)
            ms.Write(aes.IV, 0, aes.IV.Length)

            Using cs As New CryptoStream(ms, aes.CreateEncryptor(), CryptoStreamMode.Write)
                Dim bytes() As Byte = Encoding.UTF8.GetBytes(strData)
                cs.Write(bytes, 0, bytes.Length)
            End Using

            Return Convert.ToBase64String(ms.ToArray())
        End Using
    Catch err As Exception
        AES_Encrypt = err.Message
    End Try
End Function
satyacoder commented 3 years ago

Same issue here, have been asking amazon for help for a month. Please give access to to older mws and in the mean time fix sp api. i do think its intentional

satyacoder commented 3 years ago

fount this use this . https://github.com/amzn/selling-partner-api-models/issues/1096

nightcoding2021 commented 3 years ago

The answer for anyone with this problem is use 2021-06-30 version instead - which wasn't yet live when I created post.

I've no idea why Amazon would design it originally with the encryption step, which would obviously be a complete disaster.

File upload now working here using 2021-06-30! :)

jamesaq12wsx commented 3 years ago

@nightcoding2021 Do you have to encrypt or encode the feed data? I try to use 2021 version with C# but keep having 403 response with The request signature we calculated does not match the signature you provided. Check your key and signing method

But I assume those signature already in the response from createFeedDocument url property which I use for upload base url. Do you have this issue before?

Here is my example

public IRestResponse Upload(Byte[] content, string url)
{
var client = new RestClient(url);

var request = new RestRequest(Method.PUT);

request.AddParameter("text/plain; charset=utf-8", content,  ParameterType.RequestBody);

var response = client.Execute(request);

Console.WriteLine(response.Content);

return response;
}