Changed simulated annealing breaks to use trigrams by default
[cipher-tools.git] / cipher / keyword_cipher.py
index c5e7cd8b27b2c381d1421660296b9a26c8454c69..fc8eb1c6847b11dc2a2ea29c40e0934776c84c1c 100644 (file)
@@ -160,7 +160,7 @@ def monoalphabetic_break_hillclimbing(message,
                               max_iterations=20000,
                               plain_alphabet=None, 
                               cipher_alphabet=None, 
-                              fitness=Pletters, chunksize=1):
+                              fitness=Ptrigrams, chunksize=1):
     return simulated_annealing_break(message, 
                               workers=1, 
                               initial_temperature=0,
@@ -175,7 +175,7 @@ def monoalphabetic_break_hillclimbing_mp(message,
                               max_iterations=20000,
                               plain_alphabet=None, 
                               cipher_alphabet=None, 
-                              fitness=Pletters, chunksize=1):
+                              fitness=Ptrigrams, chunksize=1):
     return simulated_annealing_break(message, 
                               workers=workers, 
                               initial_temperature=0,
@@ -190,17 +190,27 @@ def simulated_annealing_break(message, workers=10,
                               max_iterations=20000,
                               plain_alphabet=None, 
                               cipher_alphabet=None, 
-                              fitness=Pletters, chunksize=1):
+                              fitness=Ptrigrams, chunksize=1):
     worker_args = []
     ciphertext = sanitise(message)
     for i in range(workers):
-        if not plain_alphabet:
-            plain_alphabet = string.ascii_lowercase
-        if not cipher_alphabet:
-            cipher_alphabet = list(string.ascii_lowercase)
-            random.shuffle(cipher_alphabet)
-            cipher_alphabet = cat(cipher_alphabet)
-        worker_args.append((ciphertext, plain_alphabet, cipher_alphabet, 
+        if plain_alphabet is None:
+            used_plain_alphabet = string.ascii_lowercase
+        else:
+            used_plain_alphabet = plain_alphabet
+        if cipher_alphabet is None:
+            used_cipher_alphabet = list(string.ascii_lowercase)
+            random.shuffle(used_cipher_alphabet)
+            used_cipher_alphabet = cat(used_cipher_alphabet)
+        else:
+            used_cipher_alphabet = cipher_alphabet
+        # if not plain_alphabet:
+        #     plain_alphabet = string.ascii_lowercase
+        # if not cipher_alphabet:
+        #     cipher_alphabet = list(string.ascii_lowercase)
+        #     random.shuffle(cipher_alphabet)
+        #     cipher_alphabet = cat(cipher_alphabet)
+        worker_args.append((ciphertext, used_plain_alphabet, used_cipher_alphabet, 
                             initial_temperature, max_iterations, fitness))
     with multiprocessing.Pool() as pool:
         breaks = pool.starmap(simulated_annealing_break_worker,