python number handling - tiny encryption algorithm
Scott David Daniels
scott.daniels at acm.org
Wed Nov 30 10:36:40 EST 2005
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[0]
> z = input[1]
> 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[0]
z = input[1]
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
scott.daniels at acm.org
More information about the Python-list
mailing list