Snippets

Alexander Hanel XOR Bytes

You are viewing an old version of this snippet. View the current version.
Revised by Alexander Hanel abfe50f
import sys
from collections import Counter
from itertools import cycle
from itertools import product

def xor_mb(message, key):
    '''Multi-byte XOR of a string message and string key'''
    return''.join(chr(ord(m_byte)^ord(k_byte)) for m_byte,k_byte in zip(message, cycle(key)))


def run():
    data = open(sys.argv[1],'rb').read()
    c = 0 
    # source http://stackoverflow.com/a/25071991
    for n in range(1, len(data)):
        substr_counter = Counter(data[i: i+n] for i in range(len(data) - n))
        key, count = substr_counter.most_common(1)[0]
        if count == 1:
                break
        temp = xor_mb(data, key)
        if "program" in temp:
                print "Length: %s, Count: %s Key: %s" % (len(key), count, key)
                f = open(str(c) + ".bin", "wb")
                fk = open( "key-" + str(c) + ".bin", "wb")
                f.write(temp)
                fk.write(key)
                f.close()
                fk.close()
                c +=1

run()
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.