467,080 Members | 947 Online

# python number handling - tiny encryption algorithm

• viewed: 3247
Share:
4 Replies
 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
 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
 38 posts views Thread by kbass | last post: by 22 posts views Thread by Kamilche | last post: by 47 posts views Thread by Michael Scarlett | last post: by 34 posts views Thread by Blake T. Garretson | last post: by 34 posts views Thread by jlocc@fau.edu | last post: by 14 posts views Thread by ccdetail@gmail.com | last post: by 6 posts views Thread by ogtheterror | last post: by reply views Thread by SwissProgrammer | last post: by reply views Thread by SwissProgrammer | last post: by 2 posts views Thread by SwissProgrammer | last post: by 16 posts views Thread by kara507 | last post: by 4 posts views Thread by Ariiza13 | last post: by 2 posts views Thread by RonWesley | last post: by 5 posts views Thread by SueHopson | last post: by reply views Thread by rhonda6373 | last post: by 6 posts views Thread by Petrol | last post: by