Done challenge 7
[cipher-challenge.git] / 2020 / 2020-challenge7.md
1 ---
2 jupyter:
3 jupytext:
4 formats: ipynb,md
5 text_representation:
6 extension: .md
7 format_name: markdown
8 format_version: '1.2'
9 jupytext_version: 1.3.4
10 kernelspec:
11 display_name: Python 3
12 language: python
13 name: python3
14 ---
15
16 ```python Collapsed="false"
17 from szyfrow.keyword_cipher import *
18 from szyfrow.column_transposition import *
19 from szyfrow.vigenere import *
20 from szyfrow.support.text_prettify import *
21 from szyfrow.polybius import *
22 from szyfrow.caesar import *
23 import collections
24 from itertools import *
25 ```
26
27 ```python Collapsed="false"
28 challenge_number = 7
29 plaintext_a_filename = f'plaintext.{challenge_number}a.txt'
30 plaintext_b_filename = f'plaintext.{challenge_number}b.txt'
31 ciphertext_a_filename = f'ciphertext.{challenge_number}a.txt'
32 ciphertext_b_filename = f'ciphertext.{challenge_number}b.txt'
33 ```
34
35 ```python Collapsed="false"
36 ca = open(ciphertext_a_filename).read()
37 sca = sanitise(ca)
38 rsca = cat(reversed(sca))
39 cb = open(ciphertext_b_filename).read()
40 scb = cat(c for c in cb if c in (string.ascii_letters + '#+'))
41 ```
42
43 ```python Collapsed="false"
44 key_a, score_a = vigenere_frequency_break(sca, fitness=Ptrigrams)
45 print(key_a, '\n')
46 pa = vigenere_decipher(sca, key_a)
47 print(pa)
48 ```
49
50 ```python Collapsed="false"
51 pa = prettify(vigenere_decipher(sca, key_a))
52 print(pa)
53 ```
54
55 ```python Collapsed="false"
56 open(plaintext_a_filename, 'w').write(pa)
57 ```
58
59 ```python Collapsed="false"
60 crib = sanitise("tential stop the tripwire option is also deprecated since it is more likely that the expl")
61 crib
62 ```
63
64 ```python Collapsed="false"
65 cat(sorted(set(scb)))
66 ```
67
68 ```python Collapsed="false"
69 p_alpha = string.ascii_lowercase
70 c_alpha = string.ascii_uppercase + '#+'
71 ```
72
73 ```python Collapsed="false"
74 cipherstream = zip(cycle(p_alpha), cycle(c_alpha))
75 ```
76
77 ```python Collapsed="false"
78 def decipher_letter(cipher_letter, stream):
79 stream = dropwhile(lambda p: p[1] != cipher_letter, stream)
80 stream, temp_stream = tee(stream, 2)
81 (plain_letter, c) = list(islice(temp_stream, 1))[0]
82 return (plain_letter, islice(stream, 1, None))
83 ```
84
85 ```python Collapsed="false"
86 def encipher_letter(plain_letter, stream):
87 stream = dropwhile(lambda p: p[0] != plain_letter, stream)
88 stream, temp_stream = tee(stream, 2)
89 (p, cipher_letter) = list(islice(temp_stream, 1))[0]
90 return (cipher_letter, islice(stream, 1, None))
91 ```
92
93 ```python Collapsed="false"
94 cipherstream = zip(cycle(p_alpha), cycle(c_alpha))
95 p1, s1 = decipher_letter('B', cipherstream)
96 p2, s2 = decipher_letter('D', s1)
97 p3, s3 = decipher_letter('Z', s2)
98 p4, s4 = decipher_letter('+', s3)
99 p5, s5 = decipher_letter('B', s4)
100 p6, s6 = decipher_letter('D', s5)
101 p7, s7 = decipher_letter('D', s6)
102
103 p1, p2, p3, p4, p5, p6, p7
104 ```
105
106 ```python Collapsed="false"
107 cipherstream = zip(cycle(p_alpha), cycle(c_alpha))
108 p1, s1 = decipher_letter('B', cipherstream)
109 p2, s2 = decipher_letter('B', s1)
110
111 p1, p2
112 ```
113
114 ```python Collapsed="false"
115 def decipher_message(ciphertext, stream):
116 plaintext = ''
117 for l in ciphertext:
118 p, stream = decipher_letter(l, stream)
119 plaintext += p
120 return plaintext, stream
121 ```
122
123 ```python Collapsed="false"
124 def encipher_message(plaintext, stream):
125 ciphertext = ''
126 for l in plaintext:
127 c, stream = encipher_letter(l, stream)
128 ciphertext += c
129 return ciphertext, stream
130 ```
131
132 ```python Collapsed="false"
133 cipherstream = zip(cycle(p_alpha), cycle(c_alpha))
134 pb, s = decipher_message(scb[:20], cipherstream)
135 pb
136 ```
137
138 ```python Collapsed="false"
139 cipherstream = zip(cycle(p_alpha), cycle(c_alpha))
140 c_gen, s = encipher_message(crib, cipherstream)
141 c_gen
142 ```
143
144 ```python Collapsed="false"
145 co = [(c_alpha.find(l), l) for i, l in enumerate(c_gen)]
146 co
147 ```
148
149 ```python Collapsed="false"
150 scb[:20]
151 ```
152
153 ```python Collapsed="false"
154 def offsets(ciphertext):
155 mappings = {}
156 letters_seen = 0
157 offsets = []
158 for l in ciphertext:
159 if l not in mappings:
160 mappings[l] = letters_seen
161 letters_seen += 1
162 offsets.append(mappings[l])
163 return offsets
164 ```
165
166 ```python Collapsed="false"
167 crib_offsets = offsets(c_gen)
168 len(crib_offsets)
169 ```
170
171 ```python Collapsed="false"
172 def plausible_samples(generated_text):
173 plausible_samples = {}
174 for i in range(len(scb)):
175 sample = scb[i:(i + len(crib))]
176 if len(sample) == len(crib):
177 ofs = offsets(sample)
178 if ofs == crib_offsets:
179 plausible_samples[i] = sample
180 return plausible_samples
181 ```
182
183 ```python Collapsed="false"
184 c_gen
185 ```
186
187 ```python Collapsed="false"
188 def plausible_mapping(crib_output):
189 mapping = {}
190 for n, l in crib_output:
191 if n in mapping:
192 if mapping[n] != l:
193 return {}
194 else:
195 mapping[n] = l
196 return mapping
197 ```
198
199 ```python Collapsed="false"
200
201 ```
202
203 ```python Collapsed="false"
204 plausible_mapping(co[:3])
205 ```
206
207 ```python Collapsed="false"
208 solutions = {}
209 for i in range(26):
210 cipherstream = zip(islice(cycle(p_alpha), i, None),
211 cycle(c_alpha))
212 c_gen, s = encipher_message(crib, cipherstream)
213 solutions[i] = plausible_samples(c_gen)
214 solutions
215 ```
216
217 ```python Collapsed="false"
218 1026 % 26
219 ```
220
221 ```python Collapsed="false"
222 cipherstream = zip(cycle(p_alpha), cycle(c_alpha))
223 c_gen0, s = encipher_message(crib, cipherstream)
224 c_gen0
225 ```
226
227 ```python Collapsed="false"
228 c_alpha
229 ```
230
231 ```python Collapsed="false"
232 soln = solutions[0][1026]
233 alpha_pos = [c_alpha.find(c) for c in c_gen0]
234 c_alpha_mapped = {}
235
236 for p, c in zip(alpha_pos, soln):
237 c_alpha_mapped[p] = c
238 c_alpha_mapped
239 ```
240
241 ```python Collapsed="false"
242 discovered_c_alpha = cat(c_alpha_mapped[i] if i in c_alpha_mapped else ' ' for i in range(28))
243 discovered_c_alpha
244 ```
245
246 ```python Collapsed="false"
247 len(c_alpha_mapped)
248 ```
249
250 ```python Collapsed="false"
251 rotated_c_alpha = discovered_c_alpha[12:] + discovered_c_alpha[:12]
252 rotated_c_alpha
253 ```
254
255 ```python Collapsed="false"
256 cipherstream = zip(cycle(p_alpha), cycle(discovered_c_alpha))
257 c_gen, s = decipher_message(scb, cipherstream)
258 c_gen
259 ```
260
261 ```python Collapsed="false"
262 caesar_break(c_gen)
263 ```
264
265 ```python Collapsed="false"
266 caesar_decipher(c_gen, 12)
267 ```
268
269 ```python Collapsed="false"
270 cipherstream = zip(cycle(p_alpha), cycle(rotated_c_alpha))
271 p_gen, s = decipher_message(scb, cipherstream)
272 p_gen
273 ```
274
275 ```python Collapsed="false"
276 pb = prettify(p_gen)
277 ```
278
279 ```python Collapsed="false"
280 open(plaintext_b_filename, 'w').write(pb)
281 ```
282
283 ```python Collapsed="false"
284
285 ```