Done challenge 9
[cipher-challenge.git] / 2021 / 2021-challenge8.md
1 ---
2 jupyter:
3 jupytext:
4 formats: ipynb,md
5 text_representation:
6 extension: .md
7 format_name: markdown
8 format_version: '1.3'
9 jupytext_version: 1.11.1
10 kernelspec:
11 display_name: Python 3 (ipykernel)
12 language: python
13 name: python3
14 ---
15
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 *
25
26 import numpy as np
27 import pandas as pd
28 import matplotlib.pyplot as plt
29
30 import collections
31 %matplotlib inline
32 ```
33
34 ```python Collapsed="false"
35 challenge_number = 8
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'
40 ```
41
42 ```python Collapsed="false"
43 ca = open(ciphertext_a_filename).read()
44 ncb = open(ciphertext_b_filename).read()
45
46 numtrans = ''.maketrans('12345', 'abcde')
47 cb = ncb.translate(numtrans)
48
49 sca = sanitise(ca)
50 rsca = cat(reversed(sca))
51 scb = sanitise(cb)
52 rscb = cat(reversed(scb))
53 ```
54
55 ```python
56 sca_counts = collections.Counter(sca)
57 sca_counts
58 ```
59
60 ```python tags=[]
61 pd.Series(sca_counts).sort_index().plot.bar()
62 ```
63
64 ```python
65 key_a, score_a = vigenere_frequency_break(sca, fitness=Ptrigrams)
66 key_a, score_a
67 ```
68
69 ```python
70 pa = vigenere_decipher(sca, key_a)
71 pa
72 ```
73
74 ```python
75 print(prettify(pa))
76 ```
77
78 ```python Collapsed="false" tags=[]
79 open(plaintext_a_filename, 'w').write(prettify(pa))
80 ```
81
82 ```python
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)
86 ```
87
88 ```python
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'})
92 xcb
93 ```
94
95 ```python
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'})
100 xcb
101 ```
102
103 ```python
104 scb_counts = collections.Counter(xcb)
105 scb_counts
106 ```
107
108 ```python tags=[]
109 pd.Series(scb_counts).sort_index().plot.bar()
110 ```
111
112 ```python tags=[]
113 pd.Series(english_counts).sort_index().plot.bar()
114 ```
115
116 ```python
117 len(set(xcb))
118 ```
119
120 ```python
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
124 ```
125
126 ```python
127 pb = column_transposition_decipher(xcb, word_b,
128 fillcolumnwise=fill_b, emptycolumnwise=empty_b)
129 pb
130 ```
131
132 ```python
133 print(prettify(pb))
134 ```
135
136 ```python Collapsed="false" tags=[]
137 open(plaintext_a_filename, 'w').write(prettify(pa))
138 ```
139
140 ```python
141 cb[:11]
142 ```
143
144 ```python
145 int('10101', 2)
146 ```
147
148 ```python
149 scb = cat(c for c in cb if c in '01')
150 len(scb)
151 ```
152
153 ```python
154 ncb = [int(g, 2) for g in cb.split()]
155 min(ncb), max(ncb)
156 ```
157
158 ```python
159 tcb = cat(unpos(n) for n in ncb)
160 tcb
161 ```
162
163 ```python
164 word_b, score_b = monoalphabetic_sa_break(tcb)
165 word_b
166 ```
167
168 ```python
169 word_b, score_b = vigenere_frequency_break(tcb, fitness=Ptrigrams)
170 print(word_b, '\n')
171 pb = vigenere_decipher(tcb, word_b)
172 print(pb)
173 ```
174
175 ```python
176 print(prettify(pb))
177 ```
178
179 ```python Collapsed="false"
180 open(plaintext_b_filename, 'w').write(prettify(pb))
181 ```
182
183 ```python Collapsed="false"
184
185 ```