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
  Python error on mentioned Arduino port name dghosal 5 796 Aug-22-2023, 04:54 PM
Last Post: deanhystad
  Attempting to read keyboard outside of console Jimmy998 5 1,283 Nov-25-2022, 08:17 PM
Last Post: Larz60+
Question Help with function - encryption - messages - NameError: name 'message' is not defined MrKnd94 4 2,773 Nov-11-2022, 09:03 PM
Last Post: deanhystad
Bug beginner attempting to make chatbot MonikaDreemur 1 1,079 Feb-02-2022, 10:24 PM
Last Post: BashBedlam
  python serial port barryjo 2 1,601 Dec-27-2021, 11:09 PM
Last Post: barryjo
  is there a way to mention port range or search for port dynamically with qConnection Creepy 0 1,449 Sep-09-2021, 03:15 PM
Last Post: Creepy
  Newbie to Python (Attempting to install DJango on a VPS) jarjar95 0 1,506 May-04-2021, 03:51 PM
Last Post: jarjar95
  need help one time pad encryption implementation ! nad556 1 1,912 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 2,934 Aug-16-2020, 08:56 PM
Last Post: deanhystad
  Port my python program to Raspberry pi seamlessly Hassibayub 1 1,896 Jun-29-2020, 12:58 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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