@@ -21,6 +21,33 @@ def index_of_coincidence(frequencies: dict, length: int) -> float:
21
21
return index
22
22
23
23
24
+ def calculate_indexes_of_coincidence (ciphertext : str , step : int ) -> list :
25
+ """
26
+ For each number j in the range [0, step) the function checks the letters of the ciphertext whose position has the
27
+ form j+n*step, where n is an integer and for these letters it calculates the index of coincidence. It returns a list
28
+ with step elements, which represent the indexes of coincidence.
29
+ :param ciphertext: s string (text)
30
+ :param step: the step when traversing through the cipher
31
+ :return: a list with the indexes of coincidence
32
+ """
33
+ indexes_of_coincidence = list ()
34
+ length = len (ciphertext )
35
+
36
+ # for every starting point in [0, step)
37
+ for j in range (step ):
38
+ frequencies = dict ()
39
+ c = 0
40
+ for i in range (0 + j , length , step ):
41
+ c += 1
42
+ try : # in case the frequencies dictionary does not already have this key
43
+ frequencies [ciphertext [i ]] += 1
44
+ except KeyError :
45
+ frequencies [ciphertext [i ]] = 1
46
+ indexes_of_coincidence .append (index_of_coincidence (frequencies , c ))
47
+
48
+ return indexes_of_coincidence
49
+
50
+
24
51
def find_key_from_vigenere_cipher (ciphertext : str ) -> str :
25
52
clean_ciphertext = list ()
26
53
for symbol in ciphertext :
@@ -30,4 +57,8 @@ def find_key_from_vigenere_cipher(ciphertext: str) -> str:
30
57
clean_ciphertext = "" .join (clean_ciphertext )
31
58
32
59
key = "" # todo replace with function
33
- return key
60
+ return key
61
+
62
+
63
+ if __name__ == '__main__' :
64
+ print (index_of_coincidence (LETTER_FREQUENCIES_DICT , 1000 ))
0 commit comments