[Python-au] python number handling - tiny encryption algorithm
kingsleyturner at westpac.com.au
Wed Nov 30 00:08:10 CET 2005
I'm getting a bit out of my depth porting the 'tiny encryption algorithm'
from C to python.
Specifically I don;t know how to handle a C-long block and perform the
mathmatical manipulations in python syntax. I played with pack and unpack
(from struct module) but that didn't seem to buy me anything.
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.
This is the C function:
/* v is 64-bits input, w is 64-bits output, k is the 128-bit key */
void decipher(const unsigned long *const v,unsigned long *const w, const
unsigned long * const k)
register unsigned long
z -= (y << 4 ^ y >> 5) + y ^ sum + k[sum>>11 & 3];
sum -= delta;
y -= (z << 4 ^ z >> 5) + z ^ sum + k[sum&3];
Which gives me a (broken) python version:
y = input
z = input
n = 32
sum = 0xC6EF3720
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];
That seems to return hugely-long integers (around 30? digits), whereas I'd
them to max-out at 2^32.
Perhaps I'm not packing the input or the key properly. My inputs are
just strings which I put into an integer with the ordinal value of each
shifted into the correct position to make the C-long.
input = ord(encrypted[i])<<24 + ord(encrypted[i+1])<<16 +
ord(encrypted[i+2])<<8 + ord(encrypted[i+3])
input = ord(encrypted[i+4])<<24 + ord(encrypted[i+5])<<16 +
ord(encrypted[i+6])<<8 + ord(encrypted[i+7])
The key is created as an array of numbers, each the ord() of the key
Anyway, that's about it.
Any help much appreciated.
(non-removable disclaimer below... *sigh*)
Please consider our environment before printing this email.
WARNING - This email and any attachments may be confidential. If received in error, please delete and inform us by return email. Because emails and attachments may be interfered with, may contain computer viruses or other defects and may not be successfully replicated on other systems, you must be cautious. Westpac cannot guarantee that what you receive is what we sent. If you have any doubts about the authenticity of an email by Westpac, please contact us immediately.
It is also important to check for viruses and defects before opening or using attachments. Westpac's liability is limited to resupplying any affected attachments.
This email and its attachments are not intended to constitute any form of financial advice or recommendation of, or an offer to buy or offer to sell, any security or other financial product. We recommend that you seek your own independent legal or financial advice before proceeding with any investment decision.
Westpac Institutional Bank is a division of Westpac Banking Corporation, a company registered in New South Wales in Australia under the Corporations Act 2001 (Cth). Westpac is authorised and regulated in the United Kingdom by the Financial Services Authority and is registered at Cardiff in the United Kingdom as Branch No. BR 106. Westpac operates in the United States of America as a federally chartered branch, regulated by the Office of the Comptroller of the Currency.
Westpac Banking Corporation ABN 33 007 457 141.
More information about the python-au