示例#1
0
def break_mcmc_double(encryption, alphabet, steps, length,
                      monogram_log_distribution, bigram_log_distribution,
                      extended_alphabet):
    max_letter = max(alphabet.letters(),
                     key=lambda i: monogram_log_distribution[i]
                     if i in monogram_log_distribution else -1)
    max_position = alphabet.letters_to_position[max_letter]
    for j in range(length):
        dist_aposteriori = common.calculate_n_gram_frequencies(
            common.get_piece_on_i_coordinate(encryption, j, length), 1)
        current_guessed_encoding_key = get_optimal_mono_permutation(
            alphabet, monogram_log_distribution, dist_aposteriori)
        decoding_vigenere = []
        for i in range(0, length):
            dist_aposteriori = common.calculate_n_gram_frequencies(
                common.get_piece_on_i_coordinate(encryption, i, length), 1)
            ith_guessed_encoding_key = get_optimal_mono_permutation(
                alphabet, monogram_log_distribution, dist_aposteriori)
            decoding_vigenere.append(
                current_guessed_encoding_key.letters_to_position[
                    ith_guessed_encoding_key[max_position]] -
                current_guessed_encoding_key.letters_to_position[
                    current_guessed_encoding_key[max_position]])

        current_decryption = cipher.encrypt_decrypt_text(
            encryption, decoding_vigenere, current_guessed_encoding_key)
        current_order = sorted(alphabet.letters(),
                               key=lambda i: dist_aposteriori[i]
                               if i in dist_aposteriori else -1)
        current_guessed_decoding_key = copy.deepcopy(
            get_reverse_permutation(alphabet, current_guessed_encoding_key))
        current_decryption = encrypt_decrypt_text(
            current_decryption, alphabet, current_guessed_decoding_key)
        current_frequencies = common.calculate_n_gram_frequencies(
            current_decryption, 2)
        current_dist = common.calculate_n_gram_log_weight(
            current_frequencies, bigram_log_distribution)
        max_dist = current_dist
        max_state = copy.deepcopy(current_guessed_decoding_key)
        for i in range(steps):
            swapp = get_random_swapp(current_order)
            swap = get_swap(current_order, swapp)
            frequency_change = get_frequency_change(swap, extended_alphabet,
                                                    current_frequencies)
            dist_change = common.calculate_log_weight_change(
                frequency_change, bigram_log_distribution)
            if dist_change > log(random()):
                swap_permutation_and_update_decryption(
                    current_decryption, current_guessed_encoding_key, swap)
                common.update_frequency(current_frequencies, frequency_change)
                current_dist += dist_change
                aux = current_order[swapp[0]]
                current_order[swapp[0]] = current_order[swapp[1]]
                current_order[swapp[1]] = aux

                if current_dist > max_dist:
                    max_dist = current_dist
                    max_state = copy.deepcopy(current_guessed_decoding_key)
                    print(current_decryption.get_non_stripped_text())
    return get_reverse_permutation(alphabet, max_state)
示例#2
0
 def test_calculate_frequencies(self):
     init_frequencies = common.calculate_n_gram_frequencies("ABCCCDE", 2)
     self.assertEqual(init_frequencies["AB"], 1)
     self.assertEqual(init_frequencies["BC"], 1)
     self.assertEqual(init_frequencies["DE"], 1)
     self.assertEqual(init_frequencies["CC"], 2)
     self.assertEqual(init_frequencies["CD"], 1)
示例#3
0
def break_mcmc(encryption, alphabet, extended_alphabet, log_distribution,
               steps, monogram_log_distribution):

    dist_aposteriori = common.calculate_n_gram_frequencies(encryption, 1)
    current_guessed_decoding_key = get_reverse_permutation(
        alphabet,
        get_optimal_mono_permutation(alphabet, monogram_log_distribution,
                                     dist_aposteriori))
    current_decryption = encrypt_decrypt_text(encryption, alphabet,
                                              current_guessed_decoding_key)
    current_frequencies = common.calculate_n_gram_frequencies(
        current_decryption, 2)
    current_decoding_key = copy.deepcopy(current_guessed_decoding_key)
    current_dist = common.calculate_n_gram_log_weight(current_frequencies,
                                                      log_distribution)

    current_order = sorted(alphabet.letters(),
                           key=lambda i: monogram_log_distribution[i]
                           if i in monogram_log_distribution else -1)
    print(current_order)

    max_dist = current_dist
    max_state = copy.deepcopy(current_decoding_key)
    for i in range(steps):
        swapp = get_random_swapp(current_order)
        swap = get_swap(current_order, swapp)
        frequency_change = get_frequency_change(swap, extended_alphabet,
                                                current_frequencies)
        dist_change = common.calculate_log_weight_change(
            frequency_change, log_distribution)
        if dist_change > log(random()):
            swap_permutation_and_update_decryption(current_decryption,
                                                   current_decoding_key, swap)
            common.update_frequency(current_frequencies, frequency_change)
            current_dist += dist_change
            aux = current_order[swapp[0]]
            current_order[swapp[0]] = current_order[swapp[1]]
            current_order[swapp[1]] = aux

            if current_dist > max_dist:
                max_dist = current_dist
                max_state = copy.deepcopy(current_decoding_key)
                print(current_order)
    return max_state
示例#4
0
def generate_frequencies_and_state_weight(decryption, n_list, coefs,
                                          log_distributions):
    frequencies = []
    state_weight = 0
    index = 0
    for n in n_list:
        partial_frequencies = common.calculate_n_gram_frequencies(
            decryption, n)
        frequencies.append(partial_frequencies)
        state_weight += common.calculate_n_gram_log_weight(
            partial_frequencies, log_distributions[index]) * coefs[index]
        index += 1

    return frequencies, state_weight
示例#5
0
    data_generator.get_string_cleared(
        data_generator.generate_random_excerpt("../data/1984.txt",
                                               2000)).upper(), alphabett)

standard2 = data_generator.get_log_distribution_from_json(
    "../data/bigram_log_distributions_uppercase.json")
standard1 = data_generator.get_log_distribution_from_json(
    "../data/monogram_log_distributions_uppercase.json")

all_printable = data_generator.get_string_cleared(string.printable + " ")
upper_printable = all_printable[:10] + all_printable[36:]
complete_alphabet = alphabetic.Alphabet(
    data_generator.get_string_cleared(string.printable + " "))
upper_alphabet = alphabetic.Alphabet(upper_printable)

freqs = common.calculate_n_gram_frequencies(plain, 1)

weight = common.calculate_n_gram_log_weight(freqs, standard1)
print(weight)

permuted_alphabet = generate_random_permutation(alphabett)

encryption = encrypt_decrypt_text(plain, alphabett, permuted_alphabet)
# x = get_optimal_mono_permutation(alphabett, standard1,
#                                  common.calculate_n_gram_frequencies(encryption, 1))
# reverser = get_reverse_permutation(alphabett, x)
#
# z = plain.get_non_stripped_text()
# D = {}
# for i in z:
#     try: