-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathxorme.py
53 lines (48 loc) · 1.22 KB
/
xorme.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
#!/usr/bin/env python3
import argparse
import sys
def xorme(buf, k):
res = b''
for ch in buf:
res += chr(ch ^ k).encode('latin1')
return res
def print_output(buf, t):
if t == 'go':
fmt = r'0x{:02x}'
end=','
print('buf := []byte {')
elif t == 'c#':
fmt = r'0x{:02x}'
end=','
print('byte[] buf = {')
elif t == 'py':
fmt = r'\x{:02x}'
end=''
print('buf="""\\')
for i, ch in enumerate(buf[:-1]):
if i and not i % 16:
if t == 'py': print('\\',end='')
print('')
print(fmt.format(ch), end=end)
print(fmt.format(buf[-1]), end='')
if t == 'go':
print(' }')
elif t == 'c#':
print(' };')
elif t == 'py':
print('"""')
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
'-t', choices=['py', 'go', 'c#'],
default='py',
help='Language type to generate for'
)
parser.add_argument(
'-x', type=int, default=55,
help='XOR integer (defaults to 55)'
)
args = parser.parse_args()
buf = sys.stdin.buffer.read()
xorbuf = xorme(buf, args.x)
print_output(xorbuf, args.t)