-
Notifications
You must be signed in to change notification settings - Fork 0
/
xor_file.py
36 lines (31 loc) · 943 Bytes
/
xor_file.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
#/bin/python
# -*- coding: utf-8 -*-
# xor file w/ byte array
import sys
class xor():
def xor(self, orginal_file, new_file, xor_var):
l = len(xor_var)
data = bytearray(open(orginal_file, 'rb').read())
result = bytearray((
(data[i] ^ xor_var[i % l]) for i in range(0,len(data))
))
localFile = open(new_file, 'w')
localFile.write(result)
localFile.close()
def hexToByte(self, hexStr):
bytes = []
hexStr = ''.join( hexStr.split(" ") )
for i in range(0, len(hexStr), 2):
bytes.append( chr( int (hexStr[i:i+2], 16 ) ) )
return bytes
if __name__ == '__main__':
try:
transform = xor()
orginal_file = sys.argv[1]
new_file = sys.argv[2]
bytes = transform.hexToByte(sys.argv[3])
xor_var = bytearray(bytes)
transform.xor(orginal_file, new_file, xor_var)
except IndexError:
print('Usage: xor.py <input_file> <output_file> <"XOR hex bytes">')
sys.exit(1)