Done challenge 9
[cipher-challenge.git] / 2021 / 2021-challenge6.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.support.text_prettify import *
23
24 import numpy as np
25 import pandas as pd
26 import matplotlib.pyplot as plt
27
28 import collections
29 %matplotlib inline
30 ```
31
32 ```python Collapsed="false"
33 challenge_number = 6
34 plaintext_a_filename = f'plaintext.{challenge_number}a.txt'
35 plaintext_b_filename = f'plaintext.{challenge_number}b.txt'
36 ciphertext_a_filename = f'ciphertext.{challenge_number}a.txt'
37 ciphertext_b_filename = f'ciphertext.{challenge_number}b.txt'
38 ```
39
40 ```python Collapsed="false"
41 ca = open(ciphertext_a_filename).read()
42 cb = open(ciphertext_b_filename).read()
43
44 sca = sanitise(ca)
45 rsca = cat(reversed(sca))
46 scb = sanitise(cb)
47 rscb = cat(reversed(scb))
48 ```
49
50 ```python
51 sca_counts = collections.Counter(sca)
52 sca_counts
53 ```
54
55 ```python
56 pd.Series(sca_counts).sort_index().plot.bar()
57 ```
58
59 ```python
60 key_a, score_a = railfence_break(sca, fitness=Ptrigrams)
61 print(key_a, '\n')
62 pa = railfence_decipher(sca, key_a)
63 print(pa)
64 ```
65
66 ```python
67 (word_a, fill_a, empty_a), score_a = column_transposition_break(sca, fitness=Ptrigrams)
68 print(word_a, fill_a, empty_a, '\n')
69 pa = column_transposition_decipher(sca, word_a, fillcolumnwise=fill_a, emptycolumnwise=empty_a)
70 print(pa)
71 ```
72
73 ```python
74 transpositions_of('deacb')
75 ```
76
77 ```python
78 transpositions[word_a]
79 ```
80
81 ```python
82 print(prettify(pa))
83 ```
84
85 ```python Collapsed="false"
86 open(plaintext_a_filename, 'w').write(prettify(pa))
87 ```
88
89 ```python
90 word_b, score_b = vigenere_frequency_break(scb, fitness=Ptrigrams)
91 print(word_b, '\n')
92 pb = vigenere_decipher(scb, word_b)
93 print(pb)
94 ```
95
96 ```python
97 word_b, score_b = vigenere_frequency_break(rscb, fitness=Ptrigrams)
98 print(word_b, '\n')
99 pb = vigenere_decipher(rscb, word_b)
100 print(pb)
101 ```
102
103 ```python
104 pb = vigenere_decipher(rscb, word_b)
105 print(prettify(pb))
106 ```
107
108 ```python Collapsed="false"
109 open(plaintext_b_filename, 'w').write(prettify(pb))
110 ```
111
112 ```python Collapsed="false"
113
114 ```