445,813 Members | 1,253 Online Need help? Post your question and get tips & solutions from a community of 445,813 IT Pros & Developers. It's quick & easy.

# python number handling - tiny encryption algorithm

4 Replies

 P: n/a Kinsley Turner writes: In my version, I end up with hugely long integers, which have obviously not be constrained into the 4-byte unsigned longs that TEA is expecting. Yeah, Python promotes to long int now. The simplest way to do the 32-bit arithmetic you need is probably with the array module. Nov 30 '05 #2

 P: n/a Kinsley Turner wrote: I'm getting a bit out of my depth porting the 'tiny encryption algorithm' from C to python.... In my version, I end up with hugely long integers, which have obviously not be constrained into the 4-byte unsigned longs that TEA is expecting. ... def teaDecipher(input,key): y = input z = input n = 32 sum = 0xC6EF3720 delta=0x9E3779B9 while (n > 0): n -= 1 z -= (y << 4 ^ y >> 5) + y ^ sum + key[sum>>11 & 3]; sum -= delta; y -= (z << 4 ^ z >> 5) + z ^ sum + key[sum&3]; return y,z That seems to return hugely-long integers (around 30? digits), whereas I'd expect them to max-out at 2^32. If you really want 32-bit arithmetic, you need to specify it. Do you have to rewrite the C for 64-bit machines? For example: MASK = (1 << 32) - 1 def teaDecipher(input, key): y = input z = input sum = 0xC6EF3720 delta = 0x9E3779B9 for n in range(32): z = MASK & (z - (y << 4 ^ y >> 5) - y ^ sum - key[sum>>11 & 3]) sum = MASK & (sum - delta) y = MASK & (y - (z << 4 ^ z >> 5) - z ^ sum - key[sum&3]) return y, z --Scott David Daniels sc***********@acm.org Nov 30 '05 #3 