Updated for challenge 9
[cipher-tools.git] / cipher / vigenere.py
index f54fe6f71afe94b505d3af4481a9bf9f1a41ba61..f1cfe996c5bc1eb6e4c3fcb6bf1ac708f40a509b 100644 (file)
@@ -1,6 +1,7 @@
 from enum import Enum
 from itertools import starmap, cycle
 import multiprocessing
+from cipher.caesar import *
 from support.utilities import *
 from support.language_models import *
 
@@ -43,6 +44,16 @@ beaufort_variant_encipher=vigenere_decipher
 beaufort_variant_decipher=vigenere_encipher
 
 
+def index_of_coincidence_scan(text, max_key_length=20):
+    """Finds the index of coincidence of the text, using different chunk sizes."""
+    stext = sanitise(text)
+    iocs = {}
+    for i in range(1, max_key_length + 1):
+        splits = every_nth(stext, i)
+        mean_ioc = sum(index_of_coincidence(s) for s in splits) / i
+        iocs[i] = mean_ioc
+    return iocs
+
 def vigenere_keyword_break_mp(message, wordlist=keywords, fitness=Pletters,
                               chunksize=500):
     """Breaks a vigenere cipher using a dictionary and frequency analysis.