Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

This server require fast decoding of packets from clients, as I am expecting 5000 concurrent clients that is sending packets possibly every second. This is a game server (5000 players walking send packets each n every step).

I want to see if my implementation is fast enough or there are still room for improvements. The encryption isn't on this code as I only want to tackle the decryption for now.

The decode function is taken from a JavaScript implementation, This is the best output I got after trying to translate it. (though the translation is correct because the results are same from JavaScript and now Python)

def dataReceived(self, data):
    packet = decode(data)
    (counter, packet_id, username, password, unknown) = unpack('<B H 16s 16s 16s', packet)
    print counter
    print packet_id
    print username
    print password
    print unknown

For example the above implementation is pretty straight forward but the concern here is the decode(data) part.

I am new to Python and Twisted so I had to do some maybe absurd way on implementing this.

This is my decode function:

def decode(packet_data):
    cl = 0x6e
    internal_rounds = 2
    i = 0

    seqX = bytearray.fromhex('HEXSTRINGREMOVED')
    seqY = bytearray.fromhex('HEXSTRINGREMOVED')
    result = []

    while i < (len(packet_data) - 4):
        dl = ord(packet_data[i])
        bl = seqX[(i * 2) + 1]
        al = seqX[i * 2]
        bl2 = seqY[(i * 2) + 1]
        dl2 = seqY[i * 2]
        j = internal_rounds - 1

        while j > 0:
            if bl <= dl:
                dl -= bl
            else:
                bl = (~bl) & 0xFF
                bl += 1
                dl += bl

            dl ^= bl2
            j -= 1
        if al <= dl:
            dl -= al
        else:
            al = (~al) & 0xFF
            al += 1
            dl += al
        dl ^= dl2
        dl ^= cl
        result.append(dl)
        cl ^= dl
        i += 1

    return "".join(map(chr, result))

I did this dl = ord(packet_data[i]) because I keep getting int -= char error.

Also did this return "".join(map(chr, result)) because I kept getting errors with unpack because the return was previously return result which result is an array.

So what do you think? Should I also have used unpack_from instead? But I don't know how to make a buffer in Python. Is the data received already a buffer?

share|improve this question
    
Can you say more about this algorithm? How did you choose it? Why did you not use a standard algorithm like AES? –  Gareth Rees Jul 7 '14 at 15:32
    
algorithm is something I have to (must) use. It's actually a rc4 xor block cipher. As that is the algorithm used by the client and I cannot modify the client. I'm just updating the server code from way back 2003 to newer technology, choices are node.js and python. Already done with the node.js prototype, now I'm trying python. –  majidarif Jul 7 '14 at 16:41
    
If it's RC4, did you consider using Crypto.Cipher.ARC4 in pycrypto? –  Gareth Rees Jul 7 '14 at 16:52
    
To be more specific this is the implementation used, with this ARC4 as PRNG. I tried using arc4 implementations but I just cant get the same results. Maybe there is something custom going on those? What do you think? –  majidarif Jul 7 '14 at 16:56
    
but actually my issue here is if handling the conversions and the datatypes properly. –  majidarif Jul 7 '14 at 17:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.