Skip to content

Instantly share code, notes, and snippets.

@wzyboy
Last active April 22, 2017 11:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wzyboy/3792c61473f93ca13ad3 to your computer and use it in GitHub Desktop.
Save wzyboy/3792c61473f93ca13ad3 to your computer and use it in GitHub Desktop.
XOR
#!/usr/bin/env python3
import os
import numpy
from zlib import crc32
from shutil import copy
from argparse import ArgumentParser
def xor(data, key):
buff = numpy.frombuffer(data, dtype=numpy.dtype('B'))
return numpy.bitwise_xor(buff, key)
def crc(filename):
MAX_FILE_SIZE = 1024 * 1024 * 1024 * 2 # 2 GiB
if os.path.getsize(filename) > MAX_FILE_SIZE:
return NotImplemented
data = open(filename, 'rb').read()
c = crc32(data)
return '{:08X}'.format(c)
def main():
a, b = args.filename, args.output
copy(a, b)
aa = open(a, 'rb')
bb = open(b, 'r+b')
chunk = aa.read(args.offset)
print('Doing XOR, key = {} ...'.format(hex(args.key)))
bb.write(xor(chunk, args.key))
print('Caculating CRC32 for {} and {} ...'.format(a, b))
crc_a, crc_b = crc(a), crc(b)
print('{} -> {}'.format(crc_a, crc_b))
aa.close()
bb.close()
if __name__ == '__main__':
argparser = ArgumentParser()
argparser.add_argument('filename')
argparser.add_argument('-O', '--output')
argparser.add_argument('-o', '--offset', default=-1, type=int)
argparser.add_argument('-k', '--key', default=0x18, type=int)
args = argparser.parse_args()
if not args.output:
args.output = '{}.xor'.format(args.filename)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment