forked from ZhangFengze/NeoXResearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decrypt_buffer.py
63 lines (41 loc) · 1.27 KB
/
decrypt_buffer.py
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
import sys
import struct
def Clamp8(i):
return i % (1 << 8)
def Clamp32(i):
return i % (1 << 32)
def ROL32(n, d):
return ((n << d) % (1 << 32)) | (n >> (32 - d))
def ROR32(n, d):
return ROL32(n, 32-d)
def LShift8(n, d):
return (n << d) % (1 << 8)
def RShift8(n, d):
return n >> d
def BytesToInt32(bytes):
# return int.from_bytes(bytes, byteorder="little", signed=False)
return struct.unpack("<I", bytes)[0]
def Int32ToBytes(i):
# return i.to_bytes(4, byteorder="little", signed=False)
return struct.pack("<I", i)
def Decrypt(buffer):
key = 0x18B7A467
buffer = bytearray(buffer)
size = len(buffer)
dword = key
for i in range(size//4):
dword = ~(dword ^ ROR32(BytesToInt32(
buffer[i*4:i*4+4]), 32-(i & 0x1F)))
buffer[i*4:i*4+4] = Int32ToBytes(Clamp32(dword))
base = size//4*4
for i in range(size % 4):
byte = buffer[base+i]
byte = (RShift8(byte, 8-i) | LShift8(byte, i)) ^ RShift8(dword, i*8)
buffer[base+i] = Clamp8(~byte)
return bytes(buffer)
if __name__ == "__main__":
with open(sys.argv[1], "rb") as infile:
data = infile.read()
data = Decrypt(data)
with open(sys.argv[2], "wb") as outfile:
outfile.write(data)