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.

XML Datafeed and Python

jaushmanjaushman Member
in Help edited February 2009
Having some troubles getting the RC4 encrypted XML datafeed to interface with Python and am curious if any other users had any luck with it?

The site we're working on is Django driven, but I don't consider myself a Python expert. In attempting to port the PHP conversion script to Python I find myself banging my forehead on my desk quite often. Any help/suggestions would be appreciated.
Comments
  • brettbrett FoxyCart Team
    I like django.
    Good question though. Could you post the code perhaps on our http://wiki.foxycart.com/integration section somewhere and we'll see if we can take a look at it?

    Are you sure you're including the right libraries in order to get the RC4 functions in the first place? Perhaps a stupid question, but always good to start from the beginning.

    Have you tried porting the "test post yourself an encrypted XML" script to python? That'd probably be a good first step.
  • Call me slow...couldn't figure out how to add a new integration piece for the wiki.

    We are succesfully able to encrypt and decrypt locally using our port of the PHP script. When testing with the foxycart XML datafeed we're saving the post data to a sqlite database and then decrypting from there. The decryption comes back as a garbled series of strange characters and question marks.

    Here's the python code as we currently have it:
    # 
    #  RC4Crypt
    #  
    #  Created by Tanner Netterville on 2009-02-11.
    #  Ported from Mukul Sabharwal's PHP version [http://mjsabby.com]
    # 
    
    from pprint import pprint
    
    class RC4Crypt(object):
        def encrypt(self, pwd, data, ispwdHex=False):
            if ispwdHex:
                try:
                    int(pwd, 16)
                except ValueError:
                    return "failed, password is not hex but claims to be"
            
            key = []
            box = []
            cipher = ''
            
            pwd_length = len(pwd)
            data_length = len(data)
            
            for i in range(0, 256):
                key.append(ord(pwd[i % pwd_length]))
                box.append(i)
            
            j = 0
            for i in range(0, 256):
                try:
                    j = (j + box[i] + key[i]) % 256
                    tmp = box[i]
                    box[i] = box[j]
                    box[j] = tmp
                except IndexError:
                    print i
            
            a = 0
            j = 0
            for i in range(0, data_length):
                a = (a + 1) % 256
                j = (j + box[a]) % 256
                tmp = box[a]
                box[a] = box[j]
                box[j] = tmp
                k = box[((box[a] + box[j]) % 256)]
                cipher += chr(ord(data[i]) ^ k)
            
            return cipher
        
        def decrypt(self, pwd, data, ispwdHex=False):
            return self.encrypt(pwd, data, ispwdHex)
    
  • fredfred FoxyCart Team
    edited February 2009
    There's an ARC4 implementation at Wikipedia that I adapted and used in a Django site:
    http://en.wikipedia.org/wiki/RC4#Implementation

    I'll post the code to our wiki in moment; can't make our forum understand that, "In Python, whitespace is *very* important."
  • fredfred FoxyCart Team
    Here's the code:

    http://wiki.foxycart.com/integration:python

    Included also is a FoxyData class that wraps up the decryption and parsing of a FoxyCart datafeed; it may be of some use.

    Let us know if we can help further.
  • jaushmanjaushman Member
    edited February 2009
    Finally got my code above working. I wasn't paying attention and had forgotten to decode the URL encoded POST request. Calling urllib.unquote gets it working perfectly.

    Here's a quick and dirty view in Django that saves the decrypted XML feed to a database. This assumes I placed the above code in a rc4.py at the myproject root and that I have an app named "accounts" with a "Data" model containing two fields - data and created_at:
    from datetime import datetime
    import urllib
    
    from myproject.rc4 import RC4Crypt
    from myproject.accounts.models import Data
    
    def save_xml(request):
        now = datetime.now()
        cipher = RC4Crypt()
        pwd = 'YourKeyGoesHere'
        if request.method == "POST":
            data = request.POST["FoxyData"]
            data = urllib.unquote_plus(data)
            decrypted = cipher.decrypt(pwd,data)
            update = Data(data=decrypted, created_at=now)
            update.save()
        return HttpResponse("foxy")
    
Sign In or Register to comment.