The Foxy forums are on the move!

We're in the process of moving our forums over to a new system, and so these forums are now read-only.
If you have a question about your store in the meantime, please don't hesitate to reach out to us via email.

Datafeed decryption using JavaScript

dariodario Member
in Help edited December 2014
Hello
I'm trying to decrypt the transaction datafeed using the php xml test code but I'm not able to correctly decrypt the post.
I'm using NodeJS Crypto module (http://nodejs.org/api/crypto.html#crypto_class_decipher) passing the right key and the content received from foxycart but the decrypted text is still binary. I tried to encode it in hex but no way. What is the encoding of the post body? Is it ascii/utf8 or what? Anyone managed to correctly decrypt the feed using only js functions?
Thanks
Comments
  • fc_adamfc_adam FoxyCart Team
    @dario - just to confirm with you, you're decrypting the datafeed using RC4 as the algorithm? If so, could you share the code you're using to decrypt the feed? Feel free to whisper if you'd prefer to keep it private - and mask out your API key too to keep that private.
  • Hello Adam
    This is my code:
    decipher = crypto.createDecipher('RC4', <key> )
        decrypted = decipher.update(encryptedXml, "utf8", "utf8")
        decrypted += decipher.final("utf8")
    
  • fc_adamfc_adam FoxyCart Team
    @dario,

    Thanks for sharing your code. We'll do some testing on our side and get back to you as soon as we can.
  • evgevg FoxyCart Team
    Hi @dario,

    A decipher instance should be created this way:
    decipher = crypto.createDecipheriv('RC4', key, '');
    
  • dariodario Member
    edited December 2014
    Thanks guys but still not working...
    I paste the whole code, including the url-decoding:

     
       
      function urlDecode(str){
          return decodeURIComponent((str).replace(/%(?![\da-f]{2})/gi, function() {
            return '%25';
          }).replace(/\+/g, '%20'));
      }
    
    
    function Decrypt (encryptedXml) {
       
          var decipher, decoded, decrypted, deferred;
          deferred = Q.defer();
          decoded = urlDecode(escape(encryptedXml));
          decipher = crypto.createDecipheriv('RC4', <key>, '');
          decrypted = decipher.update(decoded, "utf8", "utf8");
          decrypted += decipher.final("utf8");
          deferred.resolve(decrypted);
          return deferred.promise;
    }
     
    }
    

    The function called is saveOnTheFly with the data coming from the POST of data feed (foxyData properties). First I decode it, then I decrypt it but what I get is only binary data.
    I don't have necessarily use crytpo module of NodeJs, any js function/library would be suitable
  • evgevg FoxyCart Team
    edited December 2014
    Hi @dario, I used just `unescape` to get it working.
    I tested it on a real FoxyCart datafeed and it worked.
    This is an example:
    var request_body = '%C9C%9F';
    var encrypted = unescape(request_body);
    var key = 'foo';
    var decipher = crypto.createDecipheriv('rc4', key, '');
    var message = decipher.update(encrypted) + decipher.final();
    console.log(message); // bar
    
  • Hello
    I tried with your code but still the decrypted XML has some problems (look at the tags djtafeed_version, cusomer_phone,customer_efail,custofer_ip)...I think this is due to js unescape function which is not perfectly equivalent to PHP urlEncode.


    <?xml version='1.0' standalone='yes'?>
    <foxydata>
    	<datafeed_version>XML FoxyCart Version 0.6</djtafeed_version>
    	<transactions>
    		<transaction>
    			<id>6:6</id>
    			<transaction_date>2007-05-04 20:53:57</transaction_date>
    			<customer_id>122</customer_id>
    			<customer_first_name>John</customer_first_name>
    			<customer_last_name>Doe</customer_last_name>
    			<customer_address1>12345 Any Street</customer_address1>
    			<customer_address2></customer_address2>
    			<customer_city>Any City</customer_city>
    			<customer_state>TN</customer_state>
    			<customer_postal_code>3<013</customer_postal_code>
    			<customer_country>US</customer_country>
    			<cusomer_phone>(123) 456-7890</customer_phone>
    			<customer_efail>someone@somewhere.com</customer_email>
    			<custofer_ip>71.228.237.177</customer_ip>
    
  • I finally managed to decrpyt the xml using the following code to replace the + with ' ' in the encrypted text and then unescaping it:
    var encrypted = unescape(request_body.replace(/\+/g, ' '));
    
  • evgevg FoxyCart Team
    @dario: great!
    We definitely need to add it to our wiki. Thank you!
  • The input encoding needs to be set to 'binary' for this to work with recent Node.js versions (I'm on 6.3.0). Here is the full snippet. Only the last line has changed.

    var crypto = require('crypto');
    var request_body = '%C9C%9F';
    var encrypted = unescape(request_body.replace(/\+/g, ' '));
    var key = 'foo';
    var decipher = crypto.createDecipheriv('rc4', key, '');
    var message = decipher.update(encrypted, 'binary', 'utf8') + decipher.final('utf8');
    console.log(message); // bar
Sign In or Register to comment.