english_monograms = MonogramMatrix() with open(training_text_file_location, 'r', -1, 'utf-8', 'replace') as training_text: text = training_text.read() normalized_text = utils.normalize(text) base_matrix.learn(normalized_text) english_monograms.learn(normalized_text) # generate initial guess key = SubstitutionKey("Guess key") english_monograms.setCharacterAsMostCommon(' ') guess = Guess(english_monograms.getListOfUniqueCharacters()) guess.randomGuessOneCharacter() guess_mapping = english_monograms.generateMappingBasedOnFrequencies(file_text) guess.setGuess(guess_mapping) key.set(guess.get()) if actual_key: pprint(actual_key) pprint(guess_mapping) print("score: " + str(utils.compare_keys(actual_key, guess_mapping))) current_decryption = key.decrypt(file_text) ciphertext_matrix = TrigramSpace(alphabet) ciphertext_matrix.learn(current_decryption) # compute initial difference & deep copy the key current_bigram_difference = base_matrix.compare_to(ciphertext_matrix) key_copy = copy.deepcopy(key) ciphertext_matrix_copy = copy.deepcopy(ciphertext_matrix)
def create_guess(self, file_name): mono_matrix = MonogramMatrix(file_name) guess_init = Guess(mono_matrix.getListOfUniqueCharacters()) guess_init.randomGuessOneCharacter() return guess_init.get()
print(" python3 encrypt.py <plaintext> <key> <ciphertext>") print(" - <plaintext> The location of the plaintext file to read from") print(" - <key> The location to write the key to") print(" - <ciphertext> The location to write the ciphertext to") sys.exit("") # collect output / input file locations plaintext_filename = sys.argv[1] key_filename = sys.argv[2] ciphertext_filename = sys.argv[3] # generate a random key unique_characters = list("abcdefghijklmnopqrstuvwxyz ") guess = Guess(unique_characters) guess.randomGuessAllCharacters() guess_dictionary = guess.get() key = SubstitutionKey('') key.set(guess_dictionary) # save the key to file with open(key_filename, 'wt') as out: pprint(guess_dictionary, stream=out) # encrypt plaintext plaintext_file = open(plaintext_filename, 'r') plaintext = plaintext_file.read() plaintext_file.close() ciphertext = key.encrypt(plaintext) # write ciphertext to file with open(ciphertext_filename, 'w') as out: