Done challenge 8
[cipher-tools.git] / 2020-early / 2020-a-challenge5.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
29 from support.text_prettify import *
30 from support.utilities import *
31 from support.plot_frequency_histogram import *
32 %matplotlib inline
33 ```
34
35 ```python
36 challenge_number = 5
37 plaintext_a_filename = f'{challenge_number}a.plaintext'
38 plaintext_b_filename = f'{challenge_number}b.plaintext'
39 ciphertext_a_filename = f'{challenge_number}a.ciphertext'
40 ciphertext_b_filename = f'{challenge_number}b.ciphertext'
41 ```
42
43 ```python
44 ca = open(ciphertext_a_filename).read()
45 cb = open(ciphertext_b_filename).read()
46
47 sca = sanitise(ca)
48 pca = letters(ca)
49 pta = depunctuate(ca)
50
51 scb = sanitise(cb)
52 pcb = letters(cb)
53 ptb = depunctuate(cb)
54 ```
55
56 ```python
57 fc = collections.Counter(sca)
58 plot_frequency_histogram(fc, sort_key=fc.get)
59 ```
60
61 ```python
62 kshifta, score = caesar_break(sca, fitness=Ptrigrams)
63 kshifta
64 ```
65
66 ```python
67 pa = caesar_decipher(sca, kshifta)
68 print(pa)
69 ```
70
71 ```python
72 (kworda, kwrapa), score = keyword_break_mp(sca, fitness=Ptrigrams)
73 kworda, kwrapa
74 ```
75
76 ```python
77 pa = keyword_decipher(sca, kworda, kwrapa)
78 print(pa)
79 ```
80
81 ```python
82 fpa = lcat(tpack(segment(pa)))
83 print(fpa)
84 ```
85
86 ```python
87 open(plaintext_a_filename, 'w').write(fpa)
88 ```
89
90 ```python
91 fc = collections.Counter(scb)
92 plot_frequency_histogram(fc, sort_key=fc.get)
93 ```
94
95 ```python
96 rscb = cat(reversed(cb))
97 rscb
98 ```
99
100 ```python
101 (kwordb, fillb, emptyb), score = column_transposition_break_mp(scb, fitness=Ptrigrams)
102 (kwordb, fillb, emptyb), score
103 ```
104
105 ```python
106 pb = column_transposition_decipher(scb, kwordb, fillcolumnwise=fillb, emptycolumnwise=emptyb)
107 pb
108 ```
109
110 ```python
111 fpb = lcat(tpack(segment(pb)))
112 print(fpb)
113 ```
114
115 ```python
116 open(plaintext_b_filename, 'w').write(fpb)
117 ```
118
119 ```python
120 transpositions[kwordb]
121 ```
122
123 ```python
124
125 ```