0

I wrote together a working solution for uploading bills to Amazon with examples from internet in PHP and it uploads the invoices (I checked, works fine). In my attempt to rewrite it in C# code, I always get the following answer:

<?xml version="1.0"?>
<ErrorResponse xmlns="http://mws.amazonaws.com/doc/2009-01-01/">
    <Error>
        <Type>Sender</Type>
        <Code>SignatureDoesNotMatch</Code>
        <Message>The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method. Consult the service documentation for details.</Message>
    </Error>
    <RequestID>bc0409f1-881a-468e-9435-4c5b0a7c78b4</RequestID>
</ErrorResponse>

The PHP code as follow:

<?php
    $fileName                        = "Invoice12345656.pdf";
    $file                            = fopen($fileName, "r");
    $pdf_feed                        = '@'. fread($file, filesize($fileName));
    $hash                            = base64_encode(md5($pdf_feed, true));
    $param                           = array();
    $param['AWSAccessKeyId']         = '*******************'; 
    $param['Action']                 = 'SubmitFeed';
    $param['SellerId']               = '********************';
    $param['FeedType']               = '_UPLOAD_VAT_INVOICE_';
    $param['FeedOptions']            = 'metadata:OrderId=123-4567890-2949968;metadata:InvoiceNumber=20173723;metadata:documenttype=Invoice';
    $param['SignatureMethod']        = 'HmacSHA256';
    $param['SignatureVersion']       = '2';
    $param['ContentMD5Value']        = $hash;
    $param['Timestamp']              = gmdate("Y-m-d\TH:i:s.\\0\\0\\0\\Z", time());
    $param['Version']                = '2009-01-01';
    $param['MarketplaceIdList.Id.1'] = 'A1PA6795UKMFR9';
    $param['PurgeAndReplace']        = 'false'; 
    $secret                          = '*****************************************';
    $url                             = array();

    foreach ($param as $key => $val) {
        $key    = urlencode($key);
        $val    = urlencode($val);
        $url[]  = "{$key}={$val}";
    }

    fclose($file);
    sort($url);

    $arr            = implode('&', $url);
    $sign           = 'POST' . "\n";
    $sign          .= 'mws.amazonservices.de' . "\n";
    $sign          .= '/Feeds/'.$param['Version'].'' . "\n";
    $sign          .= $arr;
    $signature      = hash_hmac("sha256", $sign, $secret, true);
    $httpHeader     =   array();
    $httpHeader[]   =   'Content-Type: multipart/form-data';
    $httpHeader[]   =   'Content-MD5: ' . $hash;
    $signature      = urlencode(base64_encode($signature));
    $link           = "https://mws.amazonservices.de/Feeds/".$param['Version']."?" . $arr . "&Signature=" . $signature;
    $ch             = curl_init($link);

    curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeader);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $pdf_feed);
    curl_exec($ch);
    curl_getinfo($ch);
    curl_close($ch);

and this is how my unfortunate attempt looks like in c #:

    public static void Main()
    {
        var httpClient = new HttpClient();
        var requestMessage = new HttpRequestMessage();

        var pdfStream = File.OpenRead("../../Invoice12345656.pdf");
        var pdfMD5Hash = MD5.Create().ComputeHash(pdfStream);
        var pdfMD5HashString = Convert.ToBase64String(pdfMD5Hash);

        var query = new Dictionary<string, string>()
        {
            { "AWSAccessKeyId",         "*******************" },
            { "Action",                 "SubmitFeed" },
            { "SellerId",               "*******************" },
            { "FeedType",               "_UPLOAD_VAT_INVOICE_" },
            { "FeedOptions",            "metadata:DocumentType=Invoice;metadata:InvoiceNumber=20177311;metadata:OrderId=123-4567890-8041110" },
            { "SignatureMethod",        "HmacSHA256" },
            { "SignatureVersion",       "2" },
            { "ContentMD5Value",        pdfMD5HashString },
            { "Timestamp",              DateTime.UtcNow.ToString("yyyy-MM-dd\\THH:mm:ss.000\\Z", CultureInfo.InvariantCulture) },
            { "Version",                "2009-01-01" },
            { "MarketplaceIdList.Id.1", "A1PA6795UKMFR9" },
            { "PurgeAndReplace",        "false" },
        };

        var queryString = string.Join("&", query.Select(x => $"{x.Key}={WebUtility.UrlEncode(x.Value)}"));
        var textToSign = $"POST{Environment.NewLine}mws.amazonservices.de{Environment.NewLine}/Feeds/2009-01-01{Environment.NewLine}{queryString}";
        var textToSignBytes = Encoding.Default.GetBytes(textToSign);
        var hmacSHA256 = new HMACSHA256(Convert.FromBase64String("*******************"));
        var signature = WebUtility.UrlEncode(Convert.ToBase64String(hmacSHA256.ComputeHash(textToSignBytes)));

        var formDataContent = new MultipartFormDataContent();
        formDataContent.Headers.ContentType = new MediaTypeHeaderValue("multipart/form-data");
        formDataContent.Headers.Add("Content-MD5", pdfMD5HashString);
        formDataContent.Add(new StringContent("@" + Convert.ToBase64String(File.ReadAllBytes("../../Invoice12345656.pdf"))));

        requestMessage.RequestUri = new Uri($"https://mws.amazonservices.de/Feeds/2009-01-01?{queryString}&Signature={signature}");
        requestMessage.Method = HttpMethod.Post;
        requestMessage.Content = formDataContent;

        var response = httpClient.SendAsync(requestMessage).Result;

        Console.WriteLine(response.Content.ReadAsStringAsync().Result);
    }

Any ideas why this happens.

Any help is appreciated. Thanks in advance.

spzvtbg
  • 964
  • 5
  • 13
  • 2
    Usually this issue is do to the fact that JSON (PHP) encryption uses different padding than c#. See : https://stackoverflow.com/questions/11873878/c-sharp-encryption-to-php-decryption – jdweng Oct 07 '20 at 15:42
  • @jdweng - I agree, i have compared both signatures in php was each time different and in c# was each time the same, but how is this possible where i'm doing wrong? – spzvtbg Oct 07 '20 at 16:16
  • You are always seeding the encryption with same string : Convert.FromBase64String("*******************"). See msdn sample which is creating a random key uisng : RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider() https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.hmacsha256?view=netcore-3.1 – jdweng Oct 07 '20 at 16:32
  • @jdweng - it was my false, with the code above i receive differend signatures, i was tried something else – spzvtbg Oct 07 '20 at 16:42
  • i have been dealing with this problem for a few days and i think if i can't get it to work i will make an api with the php code and send the files there – spzvtbg Oct 07 '20 at 16:47
  • Use the sample MSDN code which should work except padding may need to be changed. – jdweng Oct 07 '20 at 16:58

0 Answers0