from numpy import matrix
from numpy import linalg
from language_models import *
+import pprint
## Utility functions
cat = ''.join
wcat = ' '.join
+def pos(letter):
+ if letter in string.ascii_lowercase:
+ return ord(letter) - ord('a')
+ elif letter in string.ascii_uppercase:
+ return ord(letter) - ord('A')
+ else:
+ return ''
+
+def unpos(number): return chr(number % 26 + ord('a'))
+
modular_division_table = [[0]*26 for _ in range(26)]
for a in range(26):
>>> caesar_encipher_letter('é', 1)
'f'
"""
+ # letter = unaccent(accented_letter)
+ # if letter in string.ascii_letters:
+ # if letter in string.ascii_uppercase:
+ # alphabet_start = ord('A')
+ # else:
+ # alphabet_start = ord('a')
+ # return chr(((ord(letter) - alphabet_start + shift) % 26) +
+ # alphabet_start)
+ # else:
+ # return letter
+
letter = unaccent(accented_letter)
if letter in string.ascii_letters:
+ cipherletter = unpos(pos(letter) + shift)
if letter in string.ascii_uppercase:
- alphabet_start = ord('A')
+ return cipherletter.upper()
else:
- alphabet_start = ord('a')
- return chr(((ord(letter) - alphabet_start + shift) % 26) +
- alphabet_start)
+ return cipherletter
else:
return letter
def affine_encipher_letter(accented_letter, multiplier=1, adder=0, one_based=True):
"""Encipher a letter, given a multiplier and adder
- >>> cat([affine_encipher_letter(l, 3, 5, True) \
- for l in string.ascii_uppercase])
- 'HKNQTWZCFILORUXADGJMPSVYBE'
- >>> cat([affine_encipher_letter(l, 3, 5, False) \
- for l in string.ascii_uppercase])
- 'FILORUXADGJMPSVYBEHKNQTWZC'
+ >>> cat(affine_encipher_letter(l, 3, 5, True) \
+ for l in string.ascii_letters)
+ 'hknqtwzcfiloruxadgjmpsvybeHKNQTWZCFILORUXADGJMPSVYBE'
+ >>> cat(affine_encipher_letter(l, 3, 5, False) \
+ for l in string.ascii_letters)
+ 'filoruxadgjmpsvybehknqtwzcFILORUXADGJMPSVYBEHKNQTWZC'
"""
+ # letter = unaccent(accented_letter)
+ # if letter in string.ascii_letters:
+ # if letter in string.ascii_uppercase:
+ # alphabet_start = ord('A')
+ # else:
+ # alphabet_start = ord('a')
+ # letter_number = ord(letter) - 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
letter = unaccent(accented_letter)
if letter in string.ascii_letters:
- if letter in string.ascii_uppercase:
- alphabet_start = ord('A')
- else:
- alphabet_start = ord('a')
- letter_number = ord(letter) - alphabet_start
+ letter_number = pos(letter)
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)
+ if letter in string.ascii_uppercase:
+ return unpos(cipher_number).upper()
+ else:
+ return unpos(cipher_number)
else:
return letter
def affine_decipher_letter(letter, multiplier=1, adder=0, one_based=True):
"""Encipher a letter, given a multiplier and adder
- >>> cat([affine_decipher_letter(l, 3, 5, True) \
- for l in 'HKNQTWZCFILORUXADGJMPSVYBE'])
- 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
- >>> cat([affine_decipher_letter(l, 3, 5, False) \
- for l in 'FILORUXADGJMPSVYBEHKNQTWZC'])
- 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
+ >>> cat(affine_decipher_letter(l, 3, 5, True) \
+ for l in 'hknqtwzcfiloruxadgjmpsvybeHKNQTWZCFILORUXADGJMPSVYBE')
+ 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
+ >>> cat(affine_decipher_letter(l, 3, 5, False) \
+ for l in 'filoruxadgjmpsvybehknqtwzcFILORUXADGJMPSVYBEHKNQTWZC')
+ 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
"""
+ # if letter in string.ascii_letters:
+ # if letter in string.ascii_uppercase:
+ # alphabet_start = ord('A')
+ # else:
+ # alphabet_start = ord('a')
+ # cipher_number = ord(letter) - 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
if letter in string.ascii_letters:
- if letter in string.ascii_uppercase:
- alphabet_start = ord('A')
- else:
- alphabet_start = ord('a')
- cipher_number = ord(letter) - alphabet_start
+ cipher_number = pos(letter)
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)
+ if letter in string.ascii_uppercase:
+ return unpos(plaintext_number).upper()
+ else:
+ return unpos(plaintext_number)
else:
return letter
return cat(plaintext_list)
+def bifid_grid(keyword, wrap_alphabet, letter_mapping):
+ """Create the grids for a Bifid cipher
+ """
+ cipher_alphabet = keyword_cipher_alphabet_of(keyword, wrap_alphabet)
+ if letter_mapping is None:
+ letter_mapping = {'j': 'i'}
+ translation = ''.maketrans(letter_mapping)
+ cipher_alphabet = cat(collections.OrderedDict.fromkeys(cipher_alphabet.translate(translation)))
+ f_grid = {k: ((i // 5) + 1, (i % 5) + 1)
+ for i, k in enumerate(cipher_alphabet)}
+ r_grid = {((i // 5) + 1, (i % 5) + 1): k
+ for i, k in enumerate(cipher_alphabet)}
+ return translation, f_grid, r_grid
+
+def bifid_encipher(message, keyword, wrap_alphabet=KeywordWrapAlphabet.from_a,
+ letter_mapping=None, period=None, fillvalue=None):
+ """Bifid cipher
+
+ >>> bifid_encipher("indiajelly", 'iguana')
+ 'ibidonhprm'
+ >>> bifid_encipher("indiacurry", 'iguana', period=4)
+ 'ibnhgaqltm'
+ >>> bifid_encipher("indiacurry", 'iguana', period=4, fillvalue='x')
+ 'ibnhgaqltzml'
+ """
+ translation, f_grid, r_grid = bifid_grid(keyword, wrap_alphabet, letter_mapping)
+
+ t_message = message.translate(translation)
+ pairs0 = [f_grid[l] for l in sanitise(t_message)]
+ if period:
+ chunked_pairs = [pairs0[i:i+period] for i in range(0, len(pairs0), period)]
+ if len(chunked_pairs[-1]) < period and fillvalue:
+ chunked_pairs[-1] += [f_grid[fillvalue]] * (period - len(chunked_pairs[-1]))
+ else:
+ chunked_pairs = [pairs0]
+
+ pairs1 = []
+ for c in chunked_pairs:
+ items = sum(list(list(i) for i in zip(*c)), [])
+ p = [(items[i], items[i+1]) for i in range(0, len(items), 2)]
+ pairs1 += p
+
+ return cat(r_grid[p] for p in pairs1)
+
+
+def bifid_decipher(message, keyword, wrap_alphabet=KeywordWrapAlphabet.from_a,
+ letter_mapping=None, period=None, fillvalue=None):
+ """Decipher with bifid cipher
+
+ >>> bifid_decipher('ibidonhprm', 'iguana')
+ 'indiaielly'
+ >>> bifid_decipher("ibnhgaqltm", 'iguana', period=4)
+ 'indiacurry'
+ >>> bifid_decipher("ibnhgaqltzml", 'iguana', period=4)
+ 'indiacurryxx'
+ """
+ translation, f_grid, r_grid = bifid_grid(keyword, wrap_alphabet, letter_mapping)
+
+ t_message = message.translate(translation)
+ pairs0 = [f_grid[l] for l in sanitise(t_message)]
+ if period:
+ chunked_pairs = [pairs0[i:i+period] for i in range(0, len(pairs0), period)]
+ if len(chunked_pairs[-1]) < period and fillvalue:
+ chunked_pairs[-1] += [f_grid[fillvalue]] * (period - len(chunked_pairs[-1]))
+ else:
+ chunked_pairs = [pairs0]
+
+ pairs1 = []
+ for c in chunked_pairs:
+ items = [j for i in c for j in i]
+ gap = len(c)
+ p = [(items[i], items[i+gap]) for i in range(gap)]
+ pairs1 += p
+
+ return cat(r_grid[p] for p in pairs1)
+
class PocketEnigma(object):
"""A pocket enigma machine
The wheel is internally represented as a 26-element list self.wheel_map,