def key_extractor(m, c, pad = 0): """ As c xor m is the key, it just applies one-time pad and returns the result. If m is not a sequence of bits, it needs the size (pad) in bits of every char used to convert the string to bits. """ if re.match('[01]+', m) is None: m = cc.string_of_bits(m, pad) if re.match('[01]+', c) is None: c = cc.string_of_bits(c, pad) return one_time_pad(m, c)
def string_guess(c1, c2, guess, pad = 7): guess = cc.string_of_bits(guess, pad) possible_strings = [] message_size = len(c1) / pad print message_size guess_char_length = len(guess) / pad guess_bit_length = len(guess) for i in range(message_size - guess_char_length + 1): # Every possible position pos = i * pad section1 = c1[pos:pos + guess_bit_length] section2 = c2[pos:pos + guess_bit_length] k = one_time_pad(section1, guess) # s1 XOR m1 (my prediction of m1) guessed = one_time_pad(section2, k) # s2 XOR K guessed = cc.bits_to_string(guessed, pad) if re.match("(\w|\s|:|,|\.|;|-|&|\(|\)|'){%d}" % (len(guessed)), guessed): j = len(guessed) + i possible_strings.append([i, j, guessed]) return possible_strings