2021 challenge 1
[cipher-challenge.git] / 2020 / 2020-challenge2.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 Collapsed="false"
17 from szyfrow.affine import *
18 from szyfrow.keyword_cipher import *
19 ```
20
21 ```python Collapsed="false"
22 challenge_number = 2
23 plaintext_a_filename = f'plaintext.{challenge_number}a.txt'
24 plaintext_b_filename = f'plaintext.{challenge_number}b.txt'
25 ciphertext_a_filename = f'ciphertext.{challenge_number}a.txt'
26 ciphertext_b_filename = f'ciphertext.{challenge_number}b.txt'
27 ```
28
29 ```python Collapsed="false"
30 ca = open(ciphertext_a_filename).read()
31 cb = open(ciphertext_b_filename).read()
32 scb = sanitise(cb)
33 ```
34
35 ```python Collapsed="false"
36 (m_a, a_a, o_a), score_a = affine_break(ca)
37 print(m_a, a_a, o_a, '\n')
38 pa = affine_decipher(ca, m_a, a_a, o_a)
39 print(pa)
40 ```
41
42 ```python Collapsed="false"
43 open(plaintext_a_filename, 'w').write(pa)
44 ```
45
46 ```python Collapsed="false"
47 (word_b, wrap_b), score_b = keyword_break_mp(scb, fitness=Ptrigrams)
48 print(word_b, wrap_b, '\n')
49 pb = keyword_decipher(cb, word_b, wrap_b)
50 print(pb)
51 ```
52
53 ```python Collapsed="false"
54 word_b, score_b = simulated_annealing_break(scb)
55 print(word_b, '\n')
56 pb = keyword_decipher(cb, word_b)
57 print(pb)
58 ```
59
60 ```python Collapsed="false"
61 keyword_decipher(cb, 'abwehr', KeywordWrapAlphabet.from_last)
62 ```
63
64 ```python Collapsed="false"
65 open(plaintext_b_filename, 'w').write(pb)
66 ```
67
68 ```python Collapsed="false"
69
70 ```