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