Scytale enciphering working, but have to think about what to do with a 16 character...
[cipher-tools.git] / cipher.py
index 0e89a9c01ebdbe60e9e29b16c0822093caca983c..6a138a075e2fa570260fdff8b4c1d8f10b93351c 100644 (file)
--- a/cipher.py
+++ b/cipher.py
@@ -2,6 +2,7 @@ import string
 import collections
 import norms
 import logging
+import math
 from itertools import zip_longest
 from segment import segment
 
@@ -33,13 +34,6 @@ for a in range(26):
         c = (a * b) % 26
         modular_division_table[b][c] = a
 
-modular_division_table_one_based = [[0]*27 for x in range(27)]
-for a in range(27):
-    for b in range(27):
-        c = ((a * b)-1) % 26 + 1
-        modular_division_table_one_based[b][c] = a
-
-
 
 def sanitise(text):
     """Remove all non-alphabetic characters and convert the text to lowercase
@@ -191,15 +185,10 @@ def affine_encipher_letter(letter, multiplier=1, adder=0, one_based=True):
         else:
             alphabet_start = ord('a')
         letter_number = ord(letter) - alphabet_start
-        if one_based: 
-            letter_number += 1
-        raw_cipher_number = (letter_number * multiplier + adder)
-        cipher_number = 0
-        if one_based: 
-            cipher_number = (raw_cipher_number - 1) % 26
-        else:
-            cipher_number = raw_cipher_number % 26        
-        return chr(cipher_number + alphabet_start)
+        if one_based: letter_number += 1
+        cipher_number = (letter_number * multiplier + adder) % 26
+        if one_based: cipher_number -= 1
+        return chr(cipher_number % 26 + alphabet_start)
     else:
         return letter
 
@@ -217,15 +206,10 @@ def affine_decipher_letter(letter, multiplier=1, adder=0, one_based=True):
         else:
             alphabet_start = ord('a')
         cipher_number = ord(letter) - alphabet_start
-        if one_based: 
-            cipher_number += 1
-        plaintext_number = 0
-        if one_based:
-            plaintext_number = (modular_division_table_one_based[multiplier][(cipher_number - adder + 26) % 26] - 1) % 26
-        else:
-            #plaintext_number = (modular_division_table[multiplier][cipher_number] - adder) % 26
-            plaintext_number = modular_division_table[multiplier][(cipher_number - adder + 26) % 26]            
-        return chr(plaintext_number + alphabet_start)
+        if one_based: cipher_number += 1
+        plaintext_number = modular_division_table[multiplier][(cipher_number - adder) % 26]
+        if one_based: plaintext_number -= 1
+        return chr(plaintext_number % 26 + alphabet_start) 
     else:
         return letter
 
@@ -321,6 +305,40 @@ def keyword_decipher(message, keyword, wrap_alphabet=0):
     cipher_translation = ''.maketrans(cipher_alphabet, string.ascii_lowercase)
     return message.lower().translate(cipher_translation)
 
+def scytale_encipher(message, rows):
+    """Enciphers using the scytale transposition cipher
+    
+    >>> scytale_encipher('thequickbrownfox', 3)
+    'tcnhkfeboqrxuoiw'
+    >>> scytale_encipher('thequickbrownfox', 4)
+    'tubnhirfecooqkwx'
+    >>> scytale_encipher('thequickbrownfox', 5)
+    'tubnhirfecooqkwx'
+    >>> scytale_encipher('thequickbrownfox', 6)
+    'tqcrnxhukofeibwo'
+    >>> scytale_encipher('thequickbrownfox', 7)
+    'tqcrnxhukofeibwo'
+    """
+    row_length = math.ceil(len(message) / rows)
+    slices = [message[i:i+row_length] for i in range(0, len(message), row_length)]
+    return ''.join([''.join(r) for r in zip_longest(*slices, fillvalue='')])
+
+def scytale_decipher(message, rows):
+    """Deciphers using the scytale transposition cipher
+    
+    >>> scytale_decipher('tcnhkfeboqrxuoiw', 3)
+    'thequickbrownfox'
+    """
+    cols = math.ceil(len(message) / rows)
+    if len(message) % rows == 0:
+        part_cols = 0
+    else:
+        part_cols = rows - len(message) % rows
+    full_cols = cols - part_cols
+    columns = [message[i:i+rows] for i in range(0, full_cols * rows, rows)] + \
+              [message[i:i+rows-1] for i in range(full_cols * rows, len(message), rows - 1)]
+    return ''.join([''.join(c) for c in zip_longest(*columns, fillvalue='')])
+
 
 def caesar_break(message, metric=norms.euclidean_distance, target_frequencies=normalised_english_counts, message_frequency_scaling=norms.normalise):
     """Breaks a Caesar cipher using frequency analysis