Updated for challenge 9
[cipher-tools.git] / 2020-early / 2020-a-challenge9.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
17 import os,sys,inspect
18 currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
19 parentdir = os.path.dirname(currentdir)
20 sys.path.insert(0,parentdir)
21 ```
22
23 ```python
24 from cipher.caesar import *
25 from cipher.affine import *
26 from cipher.keyword_cipher import *
27 from cipher.column_transposition import *
28 from cipher.vigenere import *
29 from cipher.autokey import *
30
31 from support.text_prettify import *
32 from support.utilities import *
33 from support.plot_frequency_histogram import *
34 %matplotlib inline
35 ```
36
37 ```python
38 challenge_number = 9
39 plaintext_a_filename = f'{challenge_number}a.plaintext'
40 plaintext_b_filename = f'{challenge_number}b.plaintext'
41 ciphertext_a_filename = f'{challenge_number}a.ciphertext'
42 ciphertext_b_filename = f'{challenge_number}b.ciphertext'
43 ```
44
45 ```python
46 ca = open(ciphertext_a_filename).read()
47 cb = open(ciphertext_b_filename).read()
48
49 sca = sanitise(ca)
50 pca = letters(ca)
51 pta = depunctuate(ca)
52
53 scb = sanitise(cb)
54 pcb = letters(cb)
55 ptb = depunctuate(cb)
56 ```
57
58 ```python
59 fc = collections.Counter(sca)
60 plot_frequency_histogram(fc, sort_key=fc.get)
61 ```
62
63 ```python
64 kworda, score = vigenere_frequency_break(sca, fitness=Ptrigrams)
65 kworda
66 ```
67
68 ```python
69 pa = vigenere_decipher(sca, kworda)
70 pa
71 ```
72
73 ```python
74 fpa = lcat(tpack(segment(pa)))
75 print(fpa)
76 ```
77
78 ```python
79 open(plaintext_a_filename, 'w').write(fpa)
80 ```
81
82 ```python
83 morse_letters = {}
84 morse_codes = {}
85 with open('morse.txt') as f:
86 for line in f.readlines():
87 l, c = line.split()
88 morse_letters[c] = l
89 morse_codes[l] = c
90 morse_letters
91 ```
92
93 ```python
94 mcb = wcat(cat(morse_letters[l] for l in w.split()) for w in cb.split(' / '))
95 smcb = sanitise(mcb)
96 mcb
97 ```
98
99 ```python
100 fc = collections.Counter(sanitise(mcb))
101 plot_frequency_histogram(fc, sort_key=fc.get)
102 ```
103
104 ```python
105 index_of_coincidence_scan(smcb, max_key_length=30)
106 ```
107
108 ```python
109 kwordb, score = vigenere_frequency_break(smcb, fitness=Ptrigrams, max_key_length=30)
110 kwordb
111 ```
112
113 ```python
114 kwordb, score = autokey_sa_break(smcb, fitness=Ptrigrams, max_keylength=10, max_iterations=5000)
115 kwordb
116 ```
117
118 ```python
119 autokey_decipher(smcb, kwordb)
120 ```
121
122 ```python
123 tgs = [smcb[i:i+3] for i in range(len(smcb)-2)]
124 collections.Counter(tgs).most_common(10)
125 ```
126
127 ```python
128 wcat(w for w in mcb.split() if len(w) == 3)
129 ```
130
131 ```python
132 wcat(w if len(w) >= 3 and w.lower() in keywords else w.lower()
133 for w in mcb.split())
134 ```
135
136 ```python
137 [(i, w) for i, w in enumerate(mcb.split())
138 if len(w) >= 3
139 if w.lower() in keywords ]
140 ```
141
142 ```python
143 [(i, w) for i, w in enumerate(mcb.split())
144 if i % 26 == 25 ]
145 ```
146
147 ```python
148 pb = wcat(caesar_decipher(w, i + 1) for i, w in enumerate(mcb.split()))
149 pb
150 ```
151
152 ```python
153 open(plaintext_b_filename, 'w').write(pb)
154 ```
155
156 ```python
157
158 ```