def break_repeating_key_xor(ct, key_size): blocks = [ct[i:i+key_size] for i in range(0, len(ct), key_size)] # "make a block that is the first byte of every block, and a block that # is the second byte of every block, and so on." transported_blocks = list(itertools.zip_longest(*blocks, fillvalue=0)) # Each block in transported_blocks was encrypted with the same byte # So we can just use the code that breaks single byte XOR from challenge 3 key = [c3.bruteforce(bytes(tb))[0] for tb in transported_blocks] return bytes(key)
def find_single_byte_xor(lines): most_likely_decryption_of_lines=[(i,c3.bruteforce(l)[2]) for i,l in lines] def score(t): return c3.freq_score(t[1]) return max(most_likely_decryption_of_lines, key=score)