Updated for posting affine ciphers on blog
authorNeil Smith <neil.git@njae.me.uk>
Tue, 27 Mar 2018 09:30:23 +0000 (10:30 +0100)
committerNeil Smith <neil.git@njae.me.uk>
Tue, 27 Mar 2018 09:30:23 +0000 (10:30 +0100)
blog-images/jon-davey-334700-unsplash.jpg [new file with mode: 0644]
cipher/affine.py
cipher/hill.py

diff --git a/blog-images/jon-davey-334700-unsplash.jpg b/blog-images/jon-davey-334700-unsplash.jpg
new file mode 100644 (file)
index 0000000..fd4fbae
Binary files /dev/null and b/blog-images/jon-davey-334700-unsplash.jpg differ
index 8a9e5845a1afb784d0b1ee607a5a31dd5ce7d1e3..9c521b94e2385bc4292c1519a3b8a75c7be9263e 100644 (file)
@@ -3,13 +3,19 @@ from support.language_models import *
 from logger import logger
 
 
-modular_division_table = [[0]*26 for _ in range(26)]
-for a in range(26):
-    for b in range(26):
-        c = (a * b) % 26
-        modular_division_table[b][c] = a
+modular_division_table = [[0]*26 for _ in range(26)]
+for a in range(26):
+    for b in range(26):
+        c = (a * b) % 26
+        modular_division_table[b][c] = a
 
 
+modular_division_table = {
+    (multiplier, (multiplier * plaintext) % 26): plaintext
+    for plaintext in range(26) 
+    for multiplier in range(26)
+    }
+
 
 
 def affine_encipher_letter(accented_letter, multiplier=1, adder=0, one_based=True):
@@ -75,9 +81,12 @@ def affine_decipher_letter(letter, multiplier=1, adder=0, one_based=True):
     if letter in string.ascii_letters:
         cipher_number = pos(letter)
         if one_based: cipher_number += 1
+        # plaintext_number = ( 
+        #     modular_division_table[multiplier]
+        #                           [(cipher_number - adder) % 26])
         plaintext_number = ( 
-            modular_division_table[multiplier]
-                                  [(cipher_number - adder) % 26])
+            modular_division_table[multiplier, (cipher_number - adder) % 26]
+            )
         if one_based: plaintext_number -= 1
         if letter in string.ascii_uppercase:
             return unpos(plaintext_number).upper()
index 24bac7f986f3346f028e516bd69832a45ba015a8..8233de721231b5b4b2d1c2b3bafff982f48756e6 100644 (file)
@@ -42,7 +42,7 @@ def hill_decipher(matrix, message, fillvalue='a'):
     'hellothereaa'
     """
     adjoint = linalg.det(matrix)*linalg.inv(matrix)
-    inverse_determinant = modular_division_table[int(round(linalg.det(matrix))) % 26][1]
+    inverse_determinant = modular_division_table[int(round(linalg.det(matrix))) % 261]
     inverse_matrix = (inverse_determinant * adjoint) % 26
     return hill_encipher(inverse_matrix, message, fillvalue)