Python Forum
Attempting to port XTea Encryption from C to Python - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Attempting to port XTea Encryption from C to Python (/thread-3604.html)



Attempting to port XTea Encryption from C to Python - sonic1015 - Jun-06-2017

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;
}
}



RE: Attempting to port XTea Encryption from C to Python - sonic1015 - Jun-06-2017

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.