9 jupytext_version: 1.11.1
11 display_name: Python 3 (ipykernel)
16 ```python Collapsed="false"
17 from szyfrow.caesar import *
18 from szyfrow.affine import *
19 from szyfrow.keyword_cipher import *
20 from szyfrow.column_transposition import *
21 from szyfrow.vigenere import *
22 from szyfrow.polybius import *
23 from szyfrow.railfence import *
24 from szyfrow.support.text_prettify import *
28 import matplotlib.pyplot as plt
34 ```python Collapsed="false"
36 plaintext_a_filename = f'plaintext.{challenge_number}a.txt'
37 plaintext_b_filename = f'plaintext.{challenge_number}b.txt'
38 ciphertext_a_filename = f'ciphertext.{challenge_number}a.txt'
39 ciphertext_b_filename = f'ciphertext.{challenge_number}b.txt'
42 ```python Collapsed="false"
43 ca = open(ciphertext_a_filename).read()
44 ncb = open(ciphertext_b_filename).read()
46 numtrans = ''.maketrans('12345', 'abcde')
47 cb = ncb.translate(numtrans)
50 rsca = cat(reversed(sca))
52 rscb = cat(reversed(scb))
56 sca_counts = collections.Counter(sca)
61 pd.Series(sca_counts).sort_index().plot.bar()
65 key_a, score_a = vigenere_frequency_break(sca, fitness=Ptrigrams)
70 pa = vigenere_decipher(sca, key_a)
78 ```python Collapsed="false" tags=[]
79 open(plaintext_a_filename, 'w').write(prettify(pa))
83 word_a, wrap_a, col_a, row_a, col_first_a = key_a
84 polybius_decipher(sca, keyword=word_a, column_order=col_a, row_order=row_a,
85 column_first=col_first_a, wrap_alphabet=wrap_a)
89 xcb = polybius_decipher(scb, keyword='a', column_order='abcde', row_order='abcde',
90 column_first=False, wrap_alphabet=KeywordWrapAlphabet.from_a,
91 letters_to_merge={'z': 'y'})
96 sncb = cat(l for l in ncb if l in '12345')
97 xcb = polybius_decipher(sncb, keyword='a', column_order='12345', row_order='12345',
98 column_first=False, wrap_alphabet=KeywordWrapAlphabet.from_a,
99 letters_to_merge={'z': 'y'})
104 scb_counts = collections.Counter(xcb)
109 pd.Series(scb_counts).sort_index().plot.bar()
113 pd.Series(english_counts).sort_index().plot.bar()
121 key_b, _ = column_transposition_break(xcb, fitness=Ptrigrams)
122 word_b, fill_b, empty_b = key_b
123 word_b, fill_b, empty_b
127 pb = column_transposition_decipher(xcb, word_b,
128 fillcolumnwise=fill_b, emptycolumnwise=empty_b)
136 ```python Collapsed="false" tags=[]
137 open(plaintext_a_filename, 'w').write(prettify(pa))
149 scb = cat(c for c in cb if c in '01')
154 ncb = [int(g, 2) for g in cb.split()]
159 tcb = cat(unpos(n) for n in ncb)
164 word_b, score_b = monoalphabetic_sa_break(tcb)
169 word_b, score_b = vigenere_frequency_break(tcb, fitness=Ptrigrams)
171 pb = vigenere_decipher(tcb, word_b)
179 ```python Collapsed="false"
180 open(plaintext_b_filename, 'w').write(prettify(pb))
183 ```python Collapsed="false"