Affine letter enciphering working
authorNeil Smith <neil.github@njae.me.uk>
Fri, 25 Oct 2013 09:54:44 +0000 (10:54 +0100)
committerNeil Smith <neil.github@njae.me.uk>
Fri, 25 Oct 2013 09:54:44 +0000 (10:54 +0100)
cipher.py

index b07cd250fb8263e338a1b35686f28a583f9bf2a2..7acaf6c36d76a44e6fa6ca3db1dfc9c4a2fd2804 100644 (file)
--- a/cipher.py
+++ b/cipher.py
@@ -53,6 +53,24 @@ def caesar_cipher_message(message, shift):
 def caesar_decipher_message(message, shift):
     return caesar_cipher_message(message, -shift)
 
+def affine_cipher_letter(letter, multiplier, shift, one_based=True):
+    if letter in string.ascii_letters:
+        if letter in string.ascii_lowercase:
+            alphastart = ord('a')
+        else:
+            alphastart = ord('A')
+        letter_number = ord(letter) - alphastart
+        if one_based: letter_number += 1  
+        enciphered_letter_number = letter_number * multiplier + shift
+        if one_based: enciphered_letter_number -=1
+        enciphered_letter = chr(enciphered_letter_number % 26 + alphastart)
+        return enciphered_letter
+    else:
+        return letter
+         
+
+
+
 def caesar_break(message):
     best_key = 0
     best_fit = float("inf")
@@ -66,3 +84,4 @@ def caesar_break(message):
             best_key = shift
             best_fit = fit
     return best_key
+