def_long2str(v, w): n = (len(v) - 1) << 2 if w: m = v[-1] if (m < n - 3) or (m > n): return'' n = m s = struct.pack('<%iL' % len(v), *v) return s[0:n] if w else s
def_str2long(s, w): n = len(s) m = (4 - (n & 3) & 3) + n s = s.ljust(m, b"\0") v = list(struct.unpack('<%iL' % (m >> 2), s)) if w: v.append(n) return v
defencrypt(str, key):#加密部分 ifstr == '': returnstr v = _str2long(str, True) k = _str2long(key.ljust(16, b"\0"), False) n = len(v) - 1 z = v[n] y = v[0] sum = 0 q = 6 + 52 // (n + 1) #得出轮数 while q > 0: sum = (sum + _DELTA) & 0xffffffff#进行叠加 e = sum >> 2 & 3#固定运算 for p inrange(n): y = v[p + 1] v[p] = (v[p] + ((z >> 5 ^ y << 2) + (y >> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z))) & 0xffffffff z = v[p] y = v[0] v[n] = (v[n] + ((z >> 5 ^ y << 2) + (y >> 3 ^ z << 4) ^ (sum ^ y) + (k[n & 3 ^ e] ^ z))) & 0xffffffff#xxtea加密的标识性加密步骤 z = v[n] q -= 1 return _long2str(v, False)
defdecrypt(str, key):#解密部分 ifstr == '': returnstr v = _str2long(str, False) k = _str2long(key.ljust(16, b"\0"), False) n = len(v) - 1 z = v[n] y = v[0] q = 6 + 52 // (n + 1) sum = (q * _DELTA) & 0xffffffff while (sum != 0): e = sum >> 2 & 3 for p inrange(n, 0, -1): z = v[p - 1] v[p] = (v[p] - ((z >> 5 ^ y << 2) + (y >> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z))) & 0xffffffff y = v[p] z = v[n] v[0] = (v[0] - ((z >> 5 ^ y << 2) + (y >> 3 ^ z << 4) ^ (sum ^ y) + (k[0 & 3 ^ e] ^ z))) & 0xffffffff y = v[0]