Skip to content

Commit 4c9f042

Browse files
committed
made test_break_vigenere.py
1 parent 4feb0b4 commit 4c9f042

File tree

3 files changed

+56
-18
lines changed

3 files changed

+56
-18
lines changed

ciphers/break_vigenere.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def friedman_method(ciphertext: str, max_keylength: int | None = None) -> int:
9090
page: https://en.wikipedia.org/wiki/Vigen%C3%A8re_cipher The algorithm
9191
is in the book "Introduction to Cryptography", K. Draziotis
9292
https://repository.kallipos.gr/handle/11419/8183
93-
:param ciphertext: a string (text)
93+
:param ciphertext: a string (uppercase text)
9494
:param max_keylength: the maximum length of key that Friedman's method
9595
should check, if None then it defaults to the length of the cipher
9696
:return: the length of the key
@@ -144,7 +144,7 @@ def find_key(ciphertext: str, key_length: int) -> str:
144144
to a letter of the key. The whole procedure takes place for every letter
145145
of the key (essentially as many times as the length of the key). See
146146
here: https://www.youtube.com/watch?v=LaWp_Kq0cKs
147-
:param ciphertext: a string (text)
147+
:param ciphertext: a string (uppercase text)
148148
:param key_length: a supposed length of the key
149149
:return: the key as a string
150150
"""
@@ -190,7 +190,12 @@ def find_key_from_vigenere_cipher(ciphertext: str) -> str:
190190
Tries to find the key length and then the actual key of a Vigenere
191191
ciphertext. It uses Friedman's method and statistical analysis. It works
192192
best for large pieces of text written in the english language.
193+
IMPORTANT: Some trial and error might be needed to find the actual key
194+
especially by changing the value of MAX_KEYLENGTH.
195+
193196
"""
197+
# clean the ciphertext so that it only contains uppercase letters with no
198+
# punctuation, spaces etc.
194199
clean_ciphertext_list = []
195200
for symbol in ciphertext.upper():
196201
if symbol in LETTERS:
@@ -208,15 +213,9 @@ def find_key_from_vigenere_cipher(ciphertext: str) -> str:
208213

209214

210215
if __name__ == "__main__":
211-
print("")
216+
print()
212217
# # how to execute
213218
# with open("out.txt") as file:
214219
# ciphertext = file.read()
215220
# key = find_key_from_vigenere_cipher(ciphertext)
216221
# print(key)
217-
218-
219-
220-
# ---------- TESTS ----------
221-
# def test_index_of_coincidence(f)
222-

ciphers/test_break_vigenere.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import math
2+
3+
from ciphers.break_vigenere import (
4+
LETTER_FREQUENCIES_DICT,
5+
calculate_indexes_of_coincidence,
6+
find_key,
7+
find_key_from_vigenere_cipher,
8+
friedman_method,
9+
get_frequencies,
10+
index_of_coincidence,
11+
)
12+
13+
14+
class Test:
15+
def test_index_of_coincidence(self):
16+
ic = index_of_coincidence({"a": 50, "b": 50}, 50)
17+
assert math.isclose(ic, 2.0)
18+
19+
def test_calculate_indexes_of_coincidence(self):
20+
ciphertext = "hellothere"
21+
result = calculate_indexes_of_coincidence(ciphertext, 2)
22+
assert result == [0.1, 0.3]
23+
24+
def test_friedman_method(self):
25+
ciphertext = "asqsfdybpypvhftnboexqumfsnglmcstyefv".upper()
26+
result = friedman_method(ciphertext, 5)
27+
assert result == 3
28+
29+
def test_get_frequencies(self):
30+
result = get_frequencies()
31+
expected = tuple(num / 100 for num in LETTER_FREQUENCIES_DICT.values())
32+
assert result == expected
33+
34+
def test_find_key(self):
35+
ciphertext = "asqsfdybpypvhftnboexqumfsnglmcstyefv".upper()
36+
result = find_key(ciphertext, 3)
37+
assert result == "ABC"
38+
39+
def test_find_key_from_vigenere_cipher(self):
40+
ciphertext = (
41+
"A dqxryeocqgj mpth ms sptusb ticq ms aoihv. Fgf "
42+
"edrsou ylxmes jhv, sos exwyon uweqe igu msfjplxj "
43+
"vbtliyy. Bno xme xqupi's b uwele, bpg eql ujh qjn bpg "
44+
"atmfp piwema spfyftv. E wotg ec fnz qwljr ocpi bovng "
45+
"wremn dw xwfgw."
46+
)
47+
result = find_key_from_vigenere_cipher(ciphertext)
48+
assert result == "ABCDEF"

ciphers/tests/test.py

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)