Foxy Forum Status

We're no longer responding to questions via our forum, but we will keep it up for historical reasons. If you can't find the answer you're looking for, please visit our knowledge base or contact us. If there's enough interest in the future, we may bring the forum back.

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.
  • brettbrett FoxyCart Team
    I like django.
    Good question though. Could you post the code perhaps on our 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 []
    from pprint import pprint
    class RC4Crypt(object):
        def encrypt(self, pwd, data, ispwdHex=False):
            if ispwdHex:
                    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]))
            j = 0
            for i in range(0, 256):
                    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:

    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:

    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 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 =
        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)
        return HttpResponse("foxy")
Sign In or Register to comment.