Done challenge 7
[cipher-challenge.git] / 2021 / 2021-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.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.support.text_prettify import *
24
25 import numpy as np
26 import pandas as pd
27 import matplotlib.pyplot as plt
28
29 import collections
30 %matplotlib inline
31 ```
32
33 ```python Collapsed="false"
34 challenge_number = 7
35 plaintext_a_filename = f'plaintext.{challenge_number}a.txt'
36 plaintext_b_filename = f'plaintext.{challenge_number}b.txt'
37 ciphertext_a_filename = f'ciphertext.{challenge_number}a.txt'
38 ciphertext_b_filename = f'ciphertext.{challenge_number}b.txt'
39 ```
40
41 ```python Collapsed="false"
42 nca = open(ciphertext_a_filename).read()
43 cb = open(ciphertext_b_filename).read()
44
45 numtrans = ''.maketrans('12345', 'abcde')
46 ca = nca.translate(numtrans)
47
48 sca = sanitise(ca)
49 rsca = cat(reversed(sca))
50 scb = sanitise(cb)
51 rscb = cat(reversed(scb))
52 ```
53
54 ```python
55 sca_counts = collections.Counter(sca)
56 sca_counts
57 ```
58
59 ```python
60 pd.Series(sca_counts).sort_index().plot.bar()
61 ```
62
63 ```python
64 key_a, score_a = polybius_break(sca, column_labels='abcde', row_labels='abcde',
65 fitness=Ptrigrams)
66 key_a, score_a
67 ```
68
69 ```python
70 word_a, wrap_a, col_a, row_a, col_first_a = key_a
71 polybius_decipher(sca, keyword=word_a, column_order=col_a, row_order=row_a,
72 column_first=col_first_a, wrap_alphabet=wrap_a)
73 ```
74
75 ```python
76 xca = polybius_decipher(sca, keyword='a', column_order=col_a, row_order=row_a,
77 column_first=col_first_a, wrap_alphabet=wrap_a)
78 xca
79 ```
80
81 ```python
82 word_a, _ = monoalphabetic_sa_break(xca)
83 word_a
84 ```
85
86 ```python
87 pa = keyword_decipher(xca, word_a)
88 pa
89 ```
90
91 ```python
92 print(prettify(pa))
93 ```
94
95 ```python Collapsed="false"
96 open(plaintext_a_filename, 'w').write(prettify(pa))
97 ```
98
99 ```python
100 cb[:11]
101 ```
102
103 ```python
104 int('10101', 2)
105 ```
106
107 ```python
108 scb = cat(c for c in cb if c in '01')
109 len(scb)
110 ```
111
112 ```python
113 ncb = [int(g, 2) for g in cb.split()]
114 min(ncb), max(ncb)
115 ```
116
117 ```python
118 tcb = cat(unpos(n) for n in ncb)
119 tcb
120 ```
121
122 ```python
123 word_b, score_b = monoalphabetic_sa_break(tcb)
124 word_b
125 ```
126
127 ```python
128 word_b, score_b = vigenere_frequency_break(tcb, fitness=Ptrigrams)
129 print(word_b, '\n')
130 pb = vigenere_decipher(tcb, word_b)
131 print(pb)
132 ```
133
134 ```python
135 print(prettify(pb))
136 ```
137
138 ```python Collapsed="false"
139 open(plaintext_b_filename, 'w').write(prettify(pb))
140 ```
141
142 ```python Collapsed="false"
143
144 ```