def train(self, iterations, verbose, mode, pause): if mode in ['monogram', 'both']: super().train() if mode in ['bigram', 'both']: self.ciphertext_bigram_counts = text_bigram_counts( self.deciphered_text, self.alpha_to_idx) self.ciphertext_bigram_freqs = array_l1_normalise( self.ciphertext_bigram_counts) self.ciphertext_likelihood = bigram_likelihood( self.ciphertext_bigram_counts, self.training_bigram_freqs) for i in range(iterations): transposition = self.select_transposition() if self.is_improvement(transposition): self.update(transposition) if verbose: stdscr = start_screen() self.print_progress(transposition, i, stdscr, pause) if verbose: exit_screen(stdscr) self.deciphered_text = apply_masc(self.ciphertext, self.decryption_key)
def encrypt(self, plaintext): ciphertext = apply_masc(plaintext, self.encryption_key) if self.anagram: ciphertext = anagramify_text(ciphertext) return ciphertext
def train(self): train_mono_freqs = monogram_freqs(self.traintext, self.plaintext_alphabet) cipher_mono_freqs = monogram_freqs(self.ciphertext, self.ciphertext_alphabet) train_mono_freqs.sort(key=lambda x: x[1], reverse=True) cipher_mono_freqs.sort(key=lambda x: x[1], reverse=True) keys, _ = zip(*train_mono_freqs) vals, _ = zip(*cipher_mono_freqs) self.encryption_key = {k: v for k, v in zip(keys, vals)} self.decryption_key = {v: k for k, v in zip(keys, vals)} self.deciphered_text = apply_masc(self.ciphertext, self.decryption_key)
def print_progress(self, i, stdscr): self.deciphered_text = apply_masc(self.ciphertext, self.decryption_key) line1 = '%i: deciphered_text_likelihood = %f' % ( i, self.deciphered_text_likelihood) line2 = 'deciphered text:' line3 = self.deciphered_text[:CHAR_LIMIT] freq_diff = np.abs(self.training_bigram_freqs - self.deciphered_text_bigram_freqs) hmap_bigrams = hmap_display_bigrams(freq_diff, HMAP, self.plain_idx_to_alpha) lines = [line1, line2, line3] blanks = [''] * len(lines) lines = list(itertools.chain(*zip(lines, blanks))) + hmap_bigrams print_block(lines, stdscr) time.sleep(0.002)
def print_progress(self, transposition, i, stdscr, pause): a, b = transposition c, d = self.idx_to_alpha[a], self.idx_to_alpha[b] self.deciphered_text = apply_masc(self.ciphertext, self.decryption_key) line1 = '%i: deciphered_text_likelihood = %f' % ( i, self.ciphertext_likelihood) line2 = 'transposition = (%s, %s)' % (c, d) line3 = 'deciphered text:' line4 = self.deciphered_text[:CHAR_LIMIT] freq_diff = np.abs(self.training_bigram_freqs - self.ciphertext_bigram_freqs) hmap_bigrams = hmap_display_bigrams(freq_diff, HMAP, self.idx_to_alpha) lines = [line1, line2, line3, line4] blanks = [''] * len(lines) lines = list(itertools.chain(*zip(lines, blanks))) + hmap_bigrams print_block(lines, stdscr) time.sleep(pause)
def train(self, iterations, verbose): self.deciphered_text_bigram_counts = self.compute_deciphered_text_bigram_counts( ) self.deciphered_text_bigram_freqs = array_l1_normalise( self.deciphered_text_bigram_counts) self.deciphered_text_likelihood = bigram_likelihood( self.deciphered_text_bigram_counts, self.training_bigram_freqs) for i in range(iterations): self.modify_encryption_key() if self.is_improvement(): self.update() if verbose: stdscr = start_screen() self.print_progress(i, stdscr) if verbose: exit_screen(stdscr) self.deciphered_text = apply_masc(self.ciphertext, self.decryption_key)
def decrypt(self, ciphertext): plaintext = apply_masc(ciphertext, self.decryption_key) return plaintext
def encrypt(self, plaintext): ciphertext = apply_masc(plaintext, self.encryption_key) return ciphertext