def TEA_decryption(vs, ks): delta = 0x9E3779B9 v0, v1 = map(uint32, unpack('>2I', vs)) k0, k1, k2, k3 = map(uint32, unpack('>4I', ks)) sm, delta = uint32(delta * 32), uint32(delta) for i in range(32): v1.value -= ((v0.value << 4) + k2.value) ^ (v0.value + sm.value) ^ ( (v0.value >> 5) + k3.value) v0.value -= ((v1.value << 4) + k0.value) ^ (v1.value + sm.value) ^ ( (v1.value >> 5) + k1.value) sm.value -= delta.value return pack('>2I', v0.value, v1.value)
def myhash(self, msg, identification): delta = 0x9E3779B9 v0, v1 = map(uint32, unpack('>2I', msg)) k0, k1, k2, k3 = map(uint32, unpack('>4I', identification)) sm, delta = uint32(0), uint32(delta) for i in range(32): sm.value += delta.value v0.value += ( (v1.value << 4) + k0.value) ^ (v1.value + sm.value) ^ ( (v1.value >> 5) + k1.value) v1.value += ( (v0.value << 4) + k2.value) ^ (v0.value + sm.value) ^ ( (v0.value >> 5) + k3.value) return pack('>2I', v0.value, v1.value)
def tea_decrypt(ciphertext, key, delta=0x9E3779B9): ''' Decrypt a ciphertext using TEA algorithm. ciphertext: 64 bits length bytes-like object. key: 128 bits length bytes-like object. Return a 64 bits length bytes object. ''' v0, v1 = map(uint32, unpack('>2I', ciphertext)) k0, k1, k2, k3 = map(uint32, unpack('>4I', key)) sm, delta = uint32(0xC6EF3720), uint32(delta) for i in range(32): v1.value -= ((v0.value << 4) + k2.value) ^ (v0.value + sm.value) ^ ( (v0.value >> 5) + k3.value) v0.value -= ((v1.value << 4) + k0.value) ^ (v1.value + sm.value) ^ ( (v1.value >> 5) + k1.value) sm.value -= delta.value return pack('>2I', v0.value, v1.value)
def tea_encrypt(plaintext, key, delta=0x9E3779B9): ''' Encrypt a plaintext using TEA algorithm. plaintext: 64 bits length bytes-like object. key: 128 bits length bytes-like object. Return a 64 bits length bytes object. ''' v0, v1 = map(uint32, unpack('>2I', plaintext)) k0, k1, k2, k3 = map(uint32, unpack('>4I', key)) sm, delta = uint32(0), uint32(delta) for i in range(32): sm.value += delta.value v0.value += ((v1.value << 4) + k0.value) ^ (v1.value + sm.value) ^ ( (v1.value >> 5) + k1.value) v1.value += ((v0.value << 4) + k2.value) ^ (v0.value + sm.value) ^ ( (v0.value >> 5) + k3.value) return pack('>2I', v0.value, v1.value)
def xtea_decrypt(ciphertext, key): ''' Decrypt a ciphertext using XTEA algorithm. ciphertext: 64 bits length bytes-like object. key: 128 bits length bytes-like object. Return a 64 bits length bytes object. ''' v0, v1 = map(uint32, unpack('>2I', ciphertext)) k = tuple(map(uint32, unpack('>4I', key))) sm, delta = uint32(0xC6EF3720), uint32(0x9E3779B9) for i in range(32): v1.value -= (((v0.value << 4) ^ (v0.value >> 5)) + v0.value) ^ (sm.value + k[(sm.value >> 11) & 3].value) sm.value -= delta.value v0.value -= ( ((v1.value << 4) ^ (v1.value >> 5)) + v1.value) ^ (sm.value + k[sm.value & 3].value) return pack('>2I', v0.value, v1.value)
def __next(self): t = uint32(self.state[3]) t = uint32(t.value ^ t.value << 11) t = uint32(t.value ^ t.value >> 8) self.state[3] = self.state[2] self.state[2] = self.state[1] self.state[1] = self.state[0] s = uint32(self.state[0]) t = uint32(t.value ^ s.value) t = uint32(t.value ^ s.value >> 19) self.state[0] = t.value return t.value
import random from ctypes import c_int32 as int32, c_uint32 as uint32 i = lambda k: h(g(t(k))) l = lambda k: k < 20 and 1518500249 or (k < 40 and 1859775393 or (k < 60 and -1894007588 or -899497514)) o = lambda k, z: int32(k << z).value | int32(uint32(k).value >> (32 - z)).value def t(A): l = len(A) k = ((l + 8) >> 6) + 1 B = [0] * (k * 16) for z in range(l): B[z >> 2] |= ord(A[z]) << (24 - (z & 3) * 8) z += 1 B[z >> 2] |= 128 << (24 - (z & 3) * 8) B[z >> 2] = int32(B[z >> 2]).value B[- 1] = l * 8 return B def g(N): K = N L = [0] * 80 J = 1732584193 I = -271733879 H = -1732584194 G = 271733878 F = -1009589776 for C in range(0, len(K), 16):
import random from ctypes import c_int32 as int32, c_uint32 as uint32 i = lambda k: h(g(t(k))) l = lambda k: k < 20 and 1518500249 or (k < 40 and 1859775393 or (k < 60 and -1894007588 or -899497514)) o = lambda k, z: int32(k << z).value | int32(uint32(k).value >> (32 - z)).value def t(A): l = len(A) k = ((l + 8) >> 6) + 1 B = [0] * (k * 16) for z in range(l): B[z >> 2] |= ord(A[z]) << (24 - (z & 3) * 8) z += 1 B[z >> 2] |= 128 << (24 - (z & 3) * 8) B[z >> 2] = int32(B[z >> 2]).value B[-1] = l * 8 return B def g(N): K = N L = [0] * 80 J = 1732584193 I = -271733879 H = -1732584194 G = 271733878 F = -1009589776 for C in range(0, len(K), 16):