I once needed to an XTEA snippet to from C++ to Python and it did not work properly. I then found a function on ActiveState.com and noticed that it looks almost exactly like mine, with one exception - for some reason, its "delta" variable was changed. Here's the test code:
import struct
def XTEA_encrypt(buf, k):
ret = bytearray()
for offset in range(int(len(buf)/8)):
v0 = struct.unpack("<I", bytes(buf[offset*8:offset*8+4]))[0]
v1 = struct.unpack("<I", bytes(buf[offset*8+4:offset*8+8]))[0]
delta = 0x9E3779B9
sum_ = 0
for _ in range(32):
v0 = (v0 + ((v1<<4 ^ v1>>5) + v1) ^ (sum_ + k[sum_ & 3])) & 0xFFFFFFFF
sum_ = (sum_ + delta) & 0xFFFFFFFF
v1 = (v1 + ((v0<<4 ^ v0>>5) + v0) ^ (sum_ + k[sum_>>11 & 3])) & 0xFFFFFFFF
ret += struct.pack("<I", v0) + struct.pack("<I", v1)
return ret
print(repr(XTEA_encrypt('\xfc\xd9\xd8A\x0b\xc4~\x82',
[4060739823, 3225438839, 2808461571, 1241583342])))
With the new delta = 0x9E3779B9
, the code prints \xec\xe8\xeaZ!\xec7\xde
, as expected. On the other hand, the original C++ implementation (https://github.com/otland/forgottenserver/blob/788198e1afd96a0b83ddeb025a9f3910e72626d5/src/protocol.cpp#L87) had delta = 0x61C88647
, which results in \x87Z\xed})\t:\xbe
being printed. Why do the implementations differ?