Jche
qwqqqqqq

XXTEA python

2021-12-30

XXTEA python 加密/解密脚本

Start

normal24里面终于遇到了xxtea了,新加密get,找比较好的解密脚本也花了不少时间。另外还有tea和xtea的,这个写的头疼等摆一会有空补上。

参考http://www.yaowenming.com/A/8Bz8qZ6XJx/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import struct

_DELTA = 0x9E3779B9 #固定常量

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

def encrypt(str, key): #加密部分
if str == '': return str
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 in range(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)

def decrypt(str, key): #解密部分
if str == '': return str
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 in range(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]

sum = (sum - _DELTA) & 0xffffffff

return _long2str(v, True)

解密使用示例

1
2
3
4
5
flag = [188, 165, 206, 64, 244, 178, 178, 231, 169, 18, 157, 18, 174, 16, 200, 91, 61, 215, 6, 29, 220, 112, 248, 220]

x = decrypt(bytes(flag),'flag'.encode()) flag就是四位数的key

print(x)

Author: John Doe

Link: http://example.com/2021/12/30/XXTEA-python/

Copyright: All articles in this blog are licensed under CC BY-NC-SA 3.0 unless stating additionally.

< PreviousPost
pwn入门体验
NextPost >
csapp-pra1
CATALOG
  1. 1. XXTEA python 加密/解密脚本
  2. 2. Start