# Problem with stacked encryption and decryption XTEA in Python

-2

I have a problem with my code. I need to stack encryption 100 000 times and then decrypt that thing 100 000 times. I cannot find an answer, why my code works properly with 11 repeats, but when I put in range the number bigger than 11, console returns incorrect decrypted text.

``````import struct
import time
from binascii import hexlify, unhexlify

key = 'd9dce3cf7f5a7d406c169daf4e2f9ef3'
print("key: " + key)
#IV = list() #IV need to be a 64-bit
#for i in range(8):
#    IV.append(secrets.randbelow(256))
DELTA = 0x9E3779B9

def encrypt(plaintext, key, rounds = 32):
v0 = int(plaintext[0:8], 16)
v1 = int(plaintext[8:16], 16)
k = struct.unpack("4I", bytes.fromhex(key))
sum = 0
for round in range(rounds):
v0 = (v0 + (((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]))) & MASK
sum = (sum + DELTA) & MASK
v1 = (v1 + (((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum >> 11 & 3]))) & MASK
x1 = str(hex(v1)[2:])
x0 = str(hex(v0)[2:])
xf = x1+x0
return xf

def decrypt(cipher, key, rounds=32):
v1 = int(cipher[0:8], 16)
v0 = int(cipher[8:16], 16)
k = struct.unpack("4I", bytes.fromhex(key))

sum = (DELTA * rounds) & MASK
for round in range(rounds):
v1 = (v1 - (((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[(sum >> 11) & 3]))) & MASK
sum = (sum - DELTA) & MASK
v0 = (v0 - (((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]))) & MASK
x1 = str(hex(v1)[2:])
x0 = str(hex(v0)[2:])
xf = x0 + x1
return xf

#ENC_TEST
t1_start = time.process_time()
plaintext = '2a6abf7158809cf4'
print("Plaintext: " + plaintext)
for n in range(0, 100000):
encrypted = encrypt(plaintext, key)
plaintext = encrypted

t1_stop = time.process_time()
deltat1 = t1_stop - t1_start
print("encrypted text: " + encrypted + " Elapsed time: " + str(deltat1))

#DEC_TEST
t2_start = time.process_time()
for n in range(0, 100000):
decrypted = decrypt(plaintext, key)
plaintext = decrypted
#decrypted = decrypt(encrypted, key)

t2_stop = time.process_time()
deltat2 = t2_stop - t2_start
print("decrypted text: " + decrypted + " Elapsed time: " + str(deltat2))
``````
python
python-3.x
encryption
xtea