python number handling - tiny encryption algorithm
Carl Friedrich Bolz
cfbolz at gmx.de
Wed Nov 30 05:11:22 EST 2005
Hi!
Kinsley Turner wrote:
[snip]
>
> 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.
Yes, python integers overflow to longs, which is a good thing for your
use-case, since python's int type corresponds to a signed long on C
level. To make the code do the right thing you have to keep integers in
the range 0, 2^32 by hand by inserting something like
x = x % 2 ** 32
y = y % 2 ** 32
into the while loop.
Cheers,
Carl Friedrich
More information about the Python-list
mailing list