Done challenge 4
[cipher-tools.git] / 2020-early / 2020-a-challenge3.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
28 from support.text_prettify import *
29 from support.utilities import *
30 from support.plot_frequency_histogram import *
31 ```
32
33 ```python
34 challenge_number = 3
35 plaintext_a_filename = f'{challenge_number}a.plaintext'
36 plaintext_b_filename = f'{challenge_number}b.plaintext'
37 ciphertext_a_filename = f'{challenge_number}a.ciphertext'
38 ciphertext_b_filename = f'{challenge_number}b.ciphertext'
39 ```
40
41 ```python
42 ca = open(ciphertext_a_filename).read()
43 cb = open(ciphertext_b_filename).read()
44
45 sca = sanitise(ca)
46 pca = letters(ca)
47 pta = depunctuate(ca)
48
49 scb = sanitise(cb)
50 pcb = letters(cb)
51 ptb = depunctuate(cb)
52 ```
53
54 ```python
55 (kworda, kwrapa), score = keyword_break_mp(sca, fitness=Ptrigrams)
56 kworda, kwrapa
57 ```
58
59 ```python
60 pa = keyword_decipher(ca, kworda, kwrapa)
61 print(pa)
62 ```
63
64 ```python
65 open(plaintext_a_filename, 'w').write(pa)
66 ```
67
68 ```python
69 (kwordb, kwrapb), score = keyword_break_mp(scb, fitness=Ptrigrams)
70 kwordb, kwrapb
71 ```
72
73 ```python
74 pb = keyword_decipher(cb, kwordb, kwrapb)
75 print(pb)
76 ```
77
78 ```python
79 open(plaintext_b_filename, 'w').write(pb)
80 ```
81
82 ```python
83
84 ```