X-Git-Url: https://git.njae.me.uk/?a=blobdiff_plain;f=cipher.py;h=e318aa46f8776fe24d9293e7d2b91def53d72663;hb=d0a53e974970bc915d94280b5158b50f93054dc3;hp=86125b38e805654f0957ec5e4fd27d7e285c5177;hpb=837af0a78bfaf51266a1e21fbfd2baa80857d5bb;p=cipher-tools.git diff --git a/cipher.py b/cipher.py index 86125b3..e318aa4 100644 --- a/cipher.py +++ b/cipher.py @@ -1137,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,