Skip to content

Commit

Permalink
Create xor_file.py
Browse files Browse the repository at this point in the history
  • Loading branch information
ahhh committed Jun 16, 2015
1 parent 219ee85 commit 2b157f6
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions xor_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,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)

0 comments on commit 2b157f6

Please sign in to comment.