-
Notifications
You must be signed in to change notification settings - Fork 0
/
autocorrfuncs.py
119 lines (97 loc) · 3.46 KB
/
autocorrfuncs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import matplotlib.pyplot as plt
import pandas as pd
import re
import seaborn as sns
"""
A series of functions for the cryptanalysis of
the Vigenère cipher used to determine key length.
"""
def normalize(ciphertext) -> str:
"""
Preprocess input text to upper case,
remove punctuation, spacing and
newlines / carriage returns (\r: Windows)
"""
ciphertext = ciphertext.upper()
ciphertext = re.sub(r'[^\w\s]', '', ciphertext)
ciphertext = ciphertext.replace(" ", "").replace("\n", "").replace("\r", "")
return ciphertext
def normalize_german(ciphertext) -> str:
"""
Preprocess input text to upper case,
remove punctuation, spacing and
newlines / carriage returns (\r: Windows),
and replace characters unique to the
German language
"""
ciphertext = normalize(ciphertext)
ciphertext = ciphertext.replace("Ä", "AE")
ciphertext = ciphertext.replace("Ö", "OE")
ciphertext = ciphertext.replace("Ü", "UE")
ciphertext = ciphertext.replace("ß", "SS")
return ciphertext
def shift_text(ciphertext: str, positions: int) -> str:
"""
Shifts a copy of ciphertext an
input number of positions
"""
return (positions * "-") + ciphertext[0:len(ciphertext) - positions]
def print_shifts(ciphertext: str) -> None:
"""
A helper function which iterates over
and prints all shifts of text
"""
for i in range(len(ciphertext)):
print(i, ": ", shift_text(ciphertext, i))
def compare_texts(ciphertext: str, shifted_text: str) -> int:
"""
Compares ciphertext and shifted
text and returns the number of
matching letters for each index
"""
matches = 0
for i in range(len(shifted_text)):
if(ciphertext[i] == shifted_text[i]):
matches += 1
return matches
def autocorrelate1(ciphertext: str, shifts: int) -> None:
"""
Prints the number of matching letters
resulting from a comparison of the
ciphertext and each shift of a copy
of the ciphertext up to a supplied
number of shifts
"""
for i in range(shifts):
st = shift_text(ciphertext, i)
count = compare_texts(ciphertext, st)
print(i, ": ", count)
def autocorrelate2(ciphertext: str, shifts: int) -> dict:
"""
Returns as a dictionary the number of
matching letters resulting from a
comparison of the ciphertext and each
shift of a copy of the ciphertext up
to a supplied number of shifts
"""
matches = {}
for i in range(shifts):
st = shift_text(ciphertext, i)
matches[i] = compare_texts(ciphertext, st)
return matches
def display_correlations(dictionary: dict) -> None:
"""
A helper function to display as a line graph
the number of matching letters between ciphertext
and each shift of the copy of the ciphertext
"""
dictionary.pop(0) # remove first key-value pair to avoid skewing graph
plt.figure(figsize=(12, 6))
df = pd.Series(dictionary)
new_sns = sns.lineplot(data = df, marker="o")
new_sns.set(xticks=df.keys())
plt.title('Autokorrelationsanalyse')
plt.xlabel('Verschiebungen')
plt.ylabel('Übereinstimmungen')
plt.grid()
plt.show()