473,574 Members | 2,953 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

python number handling - tiny encryption algorithm



Hey-ho,

I'm getting a bit out of my depth porting the 'tiny encryption algorithm'
from C to python.

Ref:
http://en.wikipedia.org/wiki/Tiny_Encryption_Algorithm
http://www.simonshepherd.supanet.com...e.htm#new_ansi

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
y=v[0],z=v[1],sum=0xC6EF3720 ,delta=0x9E3779 B9,n=32;

while(n-->0)
{
z -= (y << 4 ^ y >> 5) + y ^ sum + k[sum>>11 & 3];
sum -= delta;
y -= (z << 4 ^ z >> 5) + z ^ sum + k[sum&3];
}
w[0]=y; w[1]=z;
}
Which gives me a (broken) python version:

def teaDecipher(inp ut,key):
y = input[0]
z = input[1]
n = 32
sum = 0xC6EF3720
delta=0x9E3779B 9

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.

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
character
shifted into the correct position to make the C-long.

Something like:
input[0] = ord(encrypted[i])<<24 + ord(encrypted[i+1])<<16 +
ord(encrypted[i+2])<<8 + ord(encrypted[i+3])
input[1] = 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
character...
Anyway, that's about it.
Any help much appreciated.
thanks,
-kt







(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.
Nov 30 '05 #1
4 3692
Kinsley Turner <ki************ @westpac.com.au > 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(inp ut,key):
y = input[0]
z = input[1]
n = 32
sum = 0xC6EF3720
delta=0x9E3779B 9
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(inp ut, 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
sc***********@a cm.org
Nov 30 '05 #3
One systematic, if maybe clumsy way, is to mimic the C arithmetic
operations more closely in Python. If you do, for example, a left shift
in C, the result is always returned in a certain precision, 64 bits in
the example below. In python, no bits are lost, so you have to force the
result into the 64 bits back to obtain the same result.

Instead of (y << 4) you can write something like (untested!) (y << 4) &
0xFF...FF.

Once you understand what the algorithm is doing, you may want to go back
and express the algorithm in a more pythonic way (not by programming C
in Python).

Kinsley Turner wrote:

Hey-ho,

I'm getting a bit out of my depth porting the 'tiny encryption algorithm'
from C to python.

Ref:
http://en.wikipedia.org/wiki/Tiny_Encryption_Algorithm
http://www.simonshepherd.supanet.com...e.htm#new_ansi

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
y=v[0],z=v[1],sum=0xC6EF3720 ,delta=0x9E3779 B9,n=32;

while(n-->0)
{
z -= (y << 4 ^ y >> 5) + y ^ sum + k[sum>>11 & 3];
sum -= delta;
y -= (z << 4 ^ z >> 5) + z ^ sum + k[sum&3];
}
w[0]=y; w[1]=z;
}
Which gives me a (broken) python version:

def teaDecipher(inp ut,key):
y = input[0]
z = input[1]
n = 32
sum = 0xC6EF3720
delta=0x9E3779B 9

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.

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
character
shifted into the correct position to make the C-long.

Something like:
input[0] = ord(encrypted[i])<<24 + ord(encrypted[i+1])<<16 +
ord(encrypted[i+2])<<8 + ord(encrypted[i+3])
input[1] = 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
character...
Anyway, that's about it.
Any help much appreciated.
thanks,
-kt







(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.

Nov 30 '05 #4
One systematic, if maybe clumsy way, is to mimic the C arithmetic
operations more closely in Python. If you do, for example, a left shift
in C, the result is always returned in a certain precision, 64 bits in
the example below. In python, no bits are lost, so you have to force the
result into the 64 bits back to obtain the same result.

Instead of (y << 4) you can write something like (untested!) (y << 4) &
0xFF...FF.

Once you understand what the algorithm is doing, you may want to go back
and express the algorithm in a more pythonic way (not by programming C
in Python).

Kinsley Turner wrote:

Hey-ho,

I'm getting a bit out of my depth porting the 'tiny encryption algorithm'
from C to python.

Ref:
http://en.wikipedia.org/wiki/Tiny_Encryption_Algorithm
http://www.simonshepherd.supanet.com...e.htm#new_ansi

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
y=v[0],z=v[1],sum=0xC6EF3720 ,delta=0x9E3779 B9,n=32;

while(n-->0)
{
z -= (y << 4 ^ y >> 5) + y ^ sum + k[sum>>11 & 3];
sum -= delta;
y -= (z << 4 ^ z >> 5) + z ^ sum + k[sum&3];
}
w[0]=y; w[1]=z;
}
Which gives me a (broken) python version:

def teaDecipher(inp ut,key):
y = input[0]
z = input[1]
n = 32
sum = 0xC6EF3720
delta=0x9E3779B 9

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.

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
character
shifted into the correct position to make the C-long.

Something like:
input[0] = ord(encrypted[i])<<24 + ord(encrypted[i+1])<<16 +
ord(encrypted[i+2])<<8 + ord(encrypted[i+3])
input[1] = 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
character...
Anyway, that's about it.
Any help much appreciated.
thanks,
-kt







(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.


Nov 30 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

38
3708
by: kbass | last post by:
In different articles that I have read, persons have constantly eluded to the productivity gains of Python. One person stated that Python's productivity gain was 5 to 10 times over Java in some in some cases. The strange thing that I have noticed is that there were no examples of this productivity gain (i.e., projects, programs, etc.,...). ...
22
3208
by: Kamilche | last post by:
I've looked at a few alternatives for encryption with Python, and didn't come up anything very speedy. I've written an encryption algorithm in pure Python that can process 22 megs of data a second. I know it's not secure, but it should be enough to ward off casual hacking. Does someone know of something speedier? --Kamilche
47
3631
by: Michael Scarlett | last post by:
There is an amazing article by paul graham about python, and an even better discussion about it on slashdot. The reason I point this out, is the more I read both articles, the more I realised how we would be mutilating the language with that god forsaken @ decorator. I don't know about the rest of you, but I learned python and fell in love...
34
4078
by: Blake T. Garretson | last post by:
I want to save some sensitive data (passwords, PIN numbers, etc.) to disk in a secure manner in one of my programs. What is the easiest/best way to accomplish strong file encryption in Python? Any modern block cipher will do: AES, Blowfish, etc. I'm not looking for public key stuff; I just want to provide a pass-phrase. I found a few...
34
2509
by: jlocc | last post by:
Hi! I was wondering if someone can recommend a good encryption algorithm written in python. My goal is to combine two different numbers and encrypt them to create a new number that cann't be traced back to the originals. It would be great if there exists a library already written to do this, and if there is, can somebody please point me...
14
2062
by: ccdetail | last post by:
http://www.tiobe.com/index.htm?tiobe_index Python is the 7th most commonly used language, up from 8th. The only one gaining ground besides VB in the top 10. We're glad, our app is written in python. It's free at http://pnk.com and it is a web timesheet for project accounting
6
8378
by: ogtheterror | last post by:
Hi I have a very limited understanding of Python and have given this the best shot i have but still have not been able to get it working. Is there anyone that knows how to get this into a .net assembly? _________________________________________________________ def encrypt_password(challenge, password): if challenge == 'md5': ...
0
7805
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8053
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7817
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8098
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5622
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5301
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3741
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3752
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1341
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.