Skip to content

Commit c9b22da

Browse files
committed
made friedman_method
1 parent 50da9d3 commit c9b22da

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

ciphers/break_vigenere.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ def index_of_coincidence(frequencies: dict, length: int) -> float:
4242
of times it appears in the text as a percentage}
4343
:param length: the length of the text
4444
:return: the index of coincidence
45-
>>> index_of_coincidence({'A':1,'D':2,'E':3,'F':1,'H':1,'L': 2,'N':1,'T':1,'W':1}, 13) 0.0641025641025641
4645
"""
4746
index = 0.0
4847
for value in frequencies.values():
@@ -60,18 +59,20 @@ def calculate_indexes_of_coincidence(ciphertext: str, step: int) -> list:
6059
:param step: the step when traversing through the cipher
6160
:return: a list with the indexes of coincidence
6261
"""
63-
indexes_of_coincidence = list()
62+
indexes_of_coincidence = []
6463
length = len(ciphertext)
6564

6665
# for every starting point in [0, step)
6766
for j in range(step):
68-
frequencies = dict[str, int]
67+
frequencies: dict[str, int] = {}
6968
c = 0
7069
for i in range(0 + j, length, step):
7170
c += 1
7271
try: # in case the frequencies dictionary does not already have
7372
# this key
74-
frequencies[ciphertext[i]] += 1
73+
letter = ciphertext[i]
74+
temp = frequencies[letter]
75+
frequencies[ciphertext[i]] = temp + 1
7576
except KeyError:
7677
frequencies[ciphertext[i]] = 1
7778
if c > 1: # to avoid division by zero in the index_of_coincidence
@@ -94,7 +95,7 @@ def friedman_method(ciphertext: str, max_keylength: int | None = None) -> int:
9495
should check, if None then it defaults to the length of the cipher
9596
:return: the length of the key
9697
"""
97-
# sets the default value of MAX_KEYLENGTH
98+
# sets the default value of max_keylength
9899
if max_keylength is None:
99100
max_keylength = len(ciphertext)
100101

@@ -176,7 +177,12 @@ def find_key(ciphertext: str, key_length: int) -> str:
176177
) # shift the list cyclically one position to the left
177178
key.append(max1[1])
178179

179-
return "".join(chr(num + a) for num in key) # return the key as a string
180+
key_as_list_of_letters = []
181+
for num in key:
182+
if num is not None:
183+
key_as_list_of_letters.append(chr(num + a))
184+
185+
return "".join(key_as_list_of_letters) # return the key as a string
180186

181187

182188
def find_key_from_vigenere_cipher(ciphertext: str) -> str:
@@ -185,7 +191,7 @@ def find_key_from_vigenere_cipher(ciphertext: str) -> str:
185191
ciphertext. It uses Friedman's method and statistical analysis. It works
186192
best for large pieces of text written in the english language.
187193
"""
188-
clean_ciphertext_list = list()
194+
clean_ciphertext_list = []
189195
for symbol in ciphertext.upper():
190196
if symbol in LETTERS:
191197
clean_ciphertext_list.append(symbol)

ciphers/vigenere_cipher.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33

44
def main() -> None:
5+
# with open("file.txt", "r") as file:
6+
# message = file.read()
57
message = input("Enter message: ")
68
key = input("Enter key [alphanumeric]: ")
79
mode = input("Encrypt/Decrypt [e/d]: ")
@@ -15,6 +17,8 @@ def main() -> None:
1517

1618
print(f"\n{mode.title()}ed message:")
1719
print(translated)
20+
# with open("out.txt", "w") as file:
21+
# file.write(translated)
1822

1923

2024
def encrypt_message(key: str, message: str) -> str:

0 commit comments

Comments
 (0)