X-Git-Url: https://git.njae.me.uk/?a=blobdiff_plain;f=cipher.py;h=e318aa46f8776fe24d9293e7d2b91def53d72663;hb=0e3e7f869cb661bd92d50a0407556e289c5e7bf9;hp=5781e6df74acb01c72b523e0325636792485e21f;hpb=687c0a0c78242810cc617fa2135b6eb6f730d020;p=cipher-tools.git diff --git a/cipher.py b/cipher.py index 5781e6d..e318aa4 100644 --- a/cipher.py +++ b/cipher.py @@ -13,6 +13,7 @@ import pprint ## Utility functions cat = ''.join wcat = ' '.join +lcat = '\n'.join def pos(letter): if letter in string.ascii_lowercase: @@ -1136,6 +1137,32 @@ def bifid_decipher(message, keyword, wrap_alphabet=KeywordWrapAlphabet.from_a, return cat(r_grid[p] for p in pairs1) + +def autokey_encipher(message, keyword): + """Encipher with the autokey cipher + + >>> autokey_encipher('meetatthefountain', 'kilt') + 'wmpmmxxaeyhbryoca' + """ + shifts = [pos(l) for l in keyword + message] + pairs = zip(message, shifts) + return cat([caesar_encipher_letter(l, k) for l, k in pairs]) + +def autokey_decipher(ciphertext, keyword): + """Decipher with the autokey cipher + + >>> autokey_decipher('wmpmmxxaeyhbryoca', 'kilt') + 'meetatthefountain' + """ + plaintext = [] + keys = list(keyword) + for c in ciphertext: + plaintext_letter = caesar_decipher_letter(c, pos(keys[0])) + plaintext += [plaintext_letter] + keys = keys[1:] + [plaintext_letter] + return cat(plaintext) + + class PocketEnigma(object): """A pocket enigma machine The wheel is internally represented as a 26-element list self.wheel_map,