Python Forum
Attempting to port XTea Encryption from C to Python
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Attempting to port XTea Encryption from C to Python
#1
I'm having trouble translating some C Code into Python. I have tried my best to simulate the nature of integer overflow from C, but have so far been unsuccessful. If anybody has some insight, that would be greatly appreciated as I'm new to python. Encryption & decryption is as follows:

iterations = 32
delta = 0x9e3779b9
xTeaKey = [(an int32), (an int32), (an int32), (an int32)]

def int_overflow(val):
 if not -sys.maxint-1 <= val <= sys.maxint:
   val = (val + (sys.maxint + 1)) % (2 * (sys.maxint + 1)) - sys.maxint - 1

 if not -0xffffffff <= val <= 0xffffffff - 1:
   print "AAAAAH"

 return val

def xTeaShuffle(x, sum, sumOffset) :
 e1 = (x << 4) & 0xffffffff
 e2 = x >> 5
 e3 = e1 ^ e2
 e4 = int_overflow(e3 + x)

 e5a = int_overflow(sum + xTeaKey[(sum & 0x03)]);
 e5b = int_overflow(sum + xTeaKey[((sum >> 11) & 0x03)]);
 e5 = e5b if sumOffset else e5a

 result = e4 ^ e5

 return result

#data must be a short array and length must be divisible by 4
def xTeaEncode(data, length) :
 i = 0

 while i < length:
   sum = 0
   x1 = (data[i] << 16) + data[i + 1]
   x2 = (data[i + 2] << 16) + data[i + 3]

   iter = iterations

   while iter > 0 :
     x1 = int_overflow(x1 + xTeaShuffle(x2, sum, False))
     sum = int_overflow(sum + delta);
     x2 = int_overflow(x2 + xTeaShuffle(x1, sum, True))
     iter -= 1

   data[i] = (x1 >> 16)
   data[i + 1] = x1 & 0xffff
   data[i + 2] = (x1 >> 16)
   data[i + 3] = x2 & 0xffff

 i += 4

return

#data must be a short array and length must be divisible by 4
def xTeaDecode(data, length) :
 i = 0

 while i < length:
   sum = int_overflow(delta * iterations)
   x1 = (data[i] << 16) + data[i + 1]
   x2 = (data[i + 2] << 16) + data[i + 3]

   while (sum != 0) :

     x2 = int_overflow(x2 - xTeaShuffle(x1, sum, True))
     sum = int_overflow(sum - delta);
     x1 = int_overflow(x1 - xTeaShuffle(x2, sum, False))

   data[i] = (x1 >> 16)
   data[i + 1] = x1 & 0xffff
   data[i + 2] = (x1 >> 16)
   data[i + 3] = x2 & 0xffff

 i += 4

 return
and the original c-code:

DllExport void _stdcall XTEAEncode16(unsigned short *data, unsigned char dataLength) 
{
unsigned char i = 0;
int x1;
int x2;
int sum;
unsigned char iterationCount;

while (i < dataLength) 
{
sum = 0;
x1 = ((unsigned int)data[i] << 16) + (unsigned int)data[i+1];
x2 = ((unsigned int)data[i+2] << 16) + (unsigned int)data[i+3];
iterationCount = NUM_ITERATIONS;

while (iterationCount > 0) 
{
x1 += (((x2 << 4) ^ (x2 >> 5)) + x2) ^ (sum + XTEAKey[(sum & 0x03)]);
sum += DELTA;
x2 += (((x1 << 4) ^ (x1 >> 5)) + x1) ^ (sum + XTEAKey[((sum >> 11) & 0x03)]);
iterationCount--;
}
data[i]   = (unsigned short)((unsigned int)x1>>16); /* take upper half as an int*/
data[i+1] = (unsigned short)(unsigned int)x1; /* take lower half */
data[i+2] = (unsigned short)((unsigned int)x2>>16); /* take upper half as an int*/
data[i+3] = (unsigned short)(unsigned int)x2; /* take lower half */

i += 4;
}
}

/**
* Decodes (deciphers) data.
* Note that data length must be a multiple of 4 words (64 bit). 
*//* *< 16-bit data array *//* *< length of data array */
DllExport void _stdcall  XTEADecode16(unsigned short* data, unsigned char dataLength ) 
{
unsigned char i = 0;
int x1;
int x2;
int sum;
unsigned char iterations;

iterations = NUM_ITERATIONS;

while (i < dataLength) 
{
sum = DELTA * iterations;
x1 = ((unsigned int)data[i] << 16) + (unsigned int)data[i+1];
x2 = ((unsigned int)data[i+2] << 16) + (unsigned int)data[i+3];

while (sum != 0) 
{
x2 -= (((x1 << 4) ^ (x1 >> 5)) + x1) ^ (sum + XTEAKey[((sum >> 11) & 0x03)]);
sum -= DELTA;
x1 -= (((x2 << 4) ^ (x2 >> 5)) + x2) ^ (sum + XTEAKey[(sum & 0x03)]);
}
data[i]   = (unsigned short)((unsigned int)x1 >> 16); /* take upper half as an int*/
data[i+1] = (unsigned short)((unsigned int)x1); /* take lower half */
data[i+2] = (unsigned short)((unsigned int)x2 >> 16); /* take upper half as an int*/
data[i+3] = (unsigned short)((unsigned int)x2); /* take lower half */

i += 4;
}
}
Reply
#2
Can't edit, but there was a mistake while transposing the code to the forum. Line 47 & 71 should be x2, not x1. As far as I can tell, when comparing values with the output from the c-program, Encoding works, but decoding does not.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  need help one time pad encryption implementation ! nad556 1 202 Nov-28-2020, 06:11 PM
Last Post: nad556
  List index out of range error when attempting to make a basic shift code djwilson0495 4 462 Aug-16-2020, 08:56 PM
Last Post: deanhystad
  Port my python program to Raspberry pi seamlessly Hassibayub 1 371 Jun-29-2020, 12:58 PM
Last Post: snippsat
  encryption and decryption with python ibrahim 1 351 May-16-2020, 03:14 PM
Last Post: Larz60+
  File encryption itzik 5 501 Nov-05-2019, 12:29 PM
Last Post: Gribouillis
  Vernam encryption method for files JohnCTX 1 511 Sep-18-2019, 04:31 PM
Last Post: JohnCTX
  Invalid archive error when attempting to install dash bootstrap components meaydemi 0 2,707 Jul-11-2019, 05:49 PM
Last Post: meaydemi
  Regarding encryption and decryption naressh1994 1 723 Jan-25-2019, 07:26 AM
Last Post: buran
  attempting to run gimp-console from python in windows 10 matteusbeus 3 1,055 Oct-09-2018, 07:05 AM
Last Post: matteusbeus
  AES encryption - does not match between arduino and python 3 crypto guillaume55 0 1,936 Sep-23-2018, 11:14 AM
Last Post: guillaume55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020