Rearranged some code
authorNeil Smith <neil.git@njae.me.uk>
Thu, 3 Jul 2014 19:37:51 +0000 (20:37 +0100)
committerNeil Smith <neil.git@njae.me.uk>
Thu, 3 Jul 2014 19:37:51 +0000 (20:37 +0100)
cipher.sublime-project
cipherbreak.py

index 2c63c0851048d8f7bff41ecf0f8cee05f52fd120..f7f6d21153985dfde22faeac2ff2ebc002dcca0b 100644 (file)
@@ -1,2 +1,9 @@
 {
+       "folders":
+       [
+               {
+                       "follow_symlinks": true,
+                       "path": "/home/neil/Documents/programming/cipher-training"
+               }
+       ]
 }
index eae56cfc3c8560ae50f33c60b8b8d8a9e95f622f..c2c5360548b2ffa8c7c5ea8f1ec6baa30c18f3d3 100644 (file)
@@ -184,6 +184,50 @@ def keyword_break_worker(message, keyword, wrap_alphabet, fitness):
                      wrap_alphabet, fit, sanitise(plaintext)[:50]))
     return (keyword, wrap_alphabet), fit
 
+def monoalphabetic_break_hillclimbing(message, max_iterations = 10000000, 
+        fitness=Pletters):
+    ciphertext = unaccent(message).lower()
+    alphabet = list(string.ascii_lowercase)
+    random.shuffle(alphabet)
+    alphabet = ''.join(alphabet)
+    return monoalphabetic_break_hillclimbing_worker(ciphertext, alphabet,
+        max_iterations, fitness)
+
+def monoalphabetic_break_hillclimbing_mp(message, workers=10, 
+        max_iterations = 10000000, fitness=Pletters, chunksize=1):
+    worker_args = []
+    ciphertext = unaccent(message).lower()
+    for i in range(workers):
+        alphabet = list(string.ascii_lowercase)
+        random.shuffle(alphabet)
+        alphabet = ''.join(alphabet)
+        worker_args.append((ciphertext, alphabet, max_iterations, fitness))
+    with Pool() as pool:
+        breaks = pool.starmap(monoalphabetic_break_hillclimbing_worker,
+            worker_args, chunksize)
+    return max(breaks, key=lambda k: k[1])
+
+def monoalphabetic_break_hillclimbing_worker(message, alphabet, 
+        max_iterations, fitness):
+    def swap(letters, i, j):
+        if i > j:
+            i, j = j, i
+        if i == j:
+            return letters
+        else:
+            return letters[:i] + letters[j] + letters[i+1:j] + letters[i] + letters[j+1:]
+    best_alphabet = alphabet
+    best_fitness = float('-inf')
+    for i in range(max_iterations):
+        alphabet = swap(alphabet, random.randrange(26), random.randrange(26))
+        cipher_translation = ''.maketrans(string.ascii_lowercase, alphabet)
+        plaintext = message.translate(cipher_translation)
+        if fitness(plaintext) > best_fitness:
+            best_fitness = fitness(plaintext)
+            best_alphabet = alphabet
+            print(i, best_alphabet, best_fitness, plaintext)
+    return best_alphabet, best_fitness
+
 
 def column_transposition_break_mp(message, translist=transpositions, 
                      fitness=Pbigrams, chunksize=500):
@@ -229,51 +273,6 @@ def column_transposition_break_mp(message, translist=transpositions,
         return max(breaks, key=lambda k: k[1])
 column_transposition_break = column_transposition_break_mp
 
-def monoalphabetic_break_hillclimbing(message, max_iterations = 10000000, 
-        fitness=Pletters):
-    ciphertext = unaccent(message).lower()
-    alphabet = list(string.ascii_lowercase)
-    random.shuffle(alphabet)
-    alphabet = ''.join(alphabet)
-    return monoalphabetic_break_hillclimbing_worker(ciphertext, alphabet,
-        max_iterations, fitness)
-
-def monoalphabetic_break_hillclimbing_mp(message, workers=100, 
-        max_iterations = 10000000, fitness=Pletters, chunksize=1):
-    worker_args = []
-    ciphertext = unaccent(message).lower()
-    for i in range(workers):
-        alphabet = list(string.ascii_lowercase)
-        random.shuffle(alphabet)
-        alphabet = ''.join(alphabet)
-        worker_args.append((ciphertext, alphabet, max_iterations, fitness))
-    with Pool() as pool:
-        breaks = pool.starmap(monoalphabetic_break_hillclimbing_worker,
-            worker_args, chunksize)
-    return max(breaks, key=lambda k: k[1])
-
-def monoalphabetic_break_hillclimbing_worker(message, alphabet, 
-        max_iterations, fitness):
-    def swap(letters, i, j):
-        if i > j:
-            i, j = j, i
-        if i == j:
-            return letters
-        else:
-            return letters[:i] + letters[j] + letters[i+1:j] + letters[i] + letters[j+1:]
-    best_alphabet = alphabet
-    best_fitness = float('-inf')
-    for i in range(max_iterations):
-        alphabet = swap(alphabet, random.randrange(26), random.randrange(26))
-        cipher_translation = ''.maketrans(string.ascii_lowercase, alphabet)
-        plaintext = message.translate(cipher_translation)
-        if fitness(plaintext) > best_fitness:
-            best_fitness = fitness(plaintext)
-            best_alphabet = alphabet
-            print(i, best_alphabet, best_fitness, plaintext)
-    return best_alphabet, best_fitness
-
-
 def column_transposition_break_worker(message, transposition, 
         fillcolumnwise, emptycolumnwise, fitness):
     plaintext = column_transposition_decipher(message, transposition,