+def keyword_cipher_alphabet_of(keyword, wrap_alphabet=False):
+ """Find the cipher alphabet given a keyword
+
+ >>> keyword_cipher_alphabet_of('harry')
+ 'harybcdefgijklmnopqstuvwxz'
+ >>> keyword_cipher_alphabet_of('harry', True)
+ 'haryzbcdefgijklmnopqstuvwx'
+ >>> keyword_cipher_alphabet_of('harry', False)
+ 'harybcdefgijklmnopqstuvwxz'
+ """
+ cipher_alphabet = ''
+ if wrap_alphabet:
+ last_keyword_letter = deduplicate(sanitise(keyword))[-1]
+ last_keyword_position = string.ascii_lowercase.find(last_keyword_letter) + 1
+ cipher_alphabet = ''.join(deduplicate(sanitise(keyword) + string.ascii_lowercase[last_keyword_position:] + string.ascii_lowercase))
+ else:
+ cipher_alphabet = ''.join(deduplicate(sanitise(keyword) + string.ascii_lowercase))
+ return cipher_alphabet
+
+
+def keyword_encipher(message, keyword, wrap_alphabet=False):
+ """Enciphers a message with a keyword substitution cipher
+
+ >>> keyword_encipher('test message', 'harry')
+ 'sbqs kbqqhdb'
+ >>> keyword_encipher('test message', 'harry', True)
+ 'qzpq jzpphcz'
+ >>> keyword_encipher('test message', 'harry', False)
+ 'sbqs kbqqhdb'
+ """
+ cipher_alphabet = keyword_cipher_alphabet_of(keyword, wrap_alphabet)
+ cipher_translation = ''.maketrans(string.ascii_lowercase, cipher_alphabet)
+ return message.lower().translate(cipher_translation)
+
+def keyword_decipher(message, keyword, wrap_alphabet=False):
+ """Deciphers a message with a keyword substitution cipher
+
+ >>> keyword_decipher('sbqs kbqqhdb', 'harry')
+ 'test message'
+ >>> keyword_decipher('qzpq jzpphcz', 'harry', True)
+ 'test message'
+ >>> keyword_decipher('sbqs kbqqhdb', 'harry', False)
+ 'test message'
+ """
+ cipher_alphabet = keyword_cipher_alphabet_of(keyword, wrap_alphabet)
+ cipher_translation = ''.maketrans(cipher_alphabet, string.ascii_lowercase)
+ return message.lower().translate(cipher_translation)
+
+