Done challenge 9
[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 pa = polybius_decipher(sca, keyword='a', column_order=col_a, row_order=row_a,
77 column_first=col_first_a, wrap_alphabet=wrap_a,
78 letters_to_merge={'z': 'y'})
79 pa
80 ```
81
82 ```python
83 # word_a, _ = monoalphabetic_sa_break(xca)
84 # word_a
85 ```
86
87 ```python
88 # pa = keyword_decipher(xca, word_a)
89 # pa
90 ```
91
92 ```python
93 print(prettify(pa))
94 ```
95
96 ```python Collapsed="false"
97 open(plaintext_a_filename, 'w').write(prettify(pa))
98 ```
99
100 ```python
101 cb[:11]
102 ```
103
104 ```python
105 int('10101', 2)
106 ```
107
108 ```python
109 scb = cat(c for c in cb if c in '01')
110 len(scb)
111 ```
112
113 ```python
114 ncb = [int(g, 2) for g in cb.split()]
115 min(ncb), max(ncb)
116 ```
117
118 ```python
119 tcb = cat(unpos(n) for n in ncb)
120 tcb
121 ```
122
123 ```python
124 word_b, score_b = monoalphabetic_sa_break(tcb)
125 word_b
126 ```
127
128 ```python
129 word_b, score_b = vigenere_frequency_break(tcb, fitness=Ptrigrams)
130 print(word_b, '\n')
131 pb = vigenere_decipher(tcb, word_b)
132 print(pb)
133 ```
134
135 ```python
136 print(prettify(pb))
137 ```
138
139 ```python Collapsed="false"
140 open(plaintext_b_filename, 'w').write(prettify(pb))
141 ```
142
143 ```python Collapsed="false"
144
145 ```