Created
November 30, 2015 12:37
-
-
Save hasherezade/2679f0f891918825f2c5 to your computer and use it in GitHub Desktop.
XOR decoder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/python | |
| import sys, argparse | |
| import re | |
| def get_bytes(buf): | |
| if not buf: | |
| return None | |
| t = re.findall ('[0-9a-fA-F]{2}', buf) | |
| byte_buf = [] | |
| for chunk in t: | |
| num = int (chunk, 16) | |
| byte_buf.append(num) | |
| return byte_buf | |
| def decode(data, key): | |
| maxlen = len(data) | |
| keylen = len(key) | |
| j = 0 #key index | |
| decoded = bytearray() | |
| for i in range(0, maxlen): | |
| dec = data[i] ^ key[j % keylen] | |
| j += 1 | |
| decoded.append(dec) | |
| return decoded | |
| def main(): | |
| parser = argparse.ArgumentParser(description="XOR decoder") | |
| parser.add_argument('--infile', dest="infile", default=None, help="Input file", required=True) | |
| parser.add_argument('--keyfile', dest="keyfile", default=None, help="Key file") | |
| parser.add_argument('--outfile', dest="outfile", default=None, help="Output file") | |
| args = parser.parse_args() | |
| infile = args.infile | |
| outfile = args.outfile | |
| data = open(infile, 'rb').read() | |
| bytes = bytearray(data) | |
| if args.keyfile is not None: | |
| kdata = open(keyfile, 'rb').read() | |
| key = bytearray(get_bytes(kdata)) | |
| else: | |
| key = bytearray([0xE9, 0x03, 0, 0]) #test | |
| out = decode(bytes, key) | |
| sys.stdout.write(out) | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment