← Back to challenges

Keyword Cipher

PythonHardcryptographylogicstrings

Instructions

A Keyword Cipher is a monoalphabetic cipher which uses a keyword to provide encryption on given string of message.

Create a function that takes two arguments: a string message and a string key, and returns an encoded message.

For the encryption key, the keyword is placed at the beginning, followed by the rest of the characters in the alphabet in order (in other words, with the keyword characters removed):

A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z
K|E|Y|W|O|R|D|A|B|C|F|G|H|I|J|L|M|N|P|Q|S|T|U|V|X|Z

The encrypted message substitutes each plaintext character with the encryption key character in the corresponding position.

Return the given message encrypted against the key:

eMessage = "KEYABC"
// A = K, B = E, C = Y, H = A, I = B, J = C

Examples

keyword_cipher("keyword", "abchij") ➞ "keyabc"

keyword_cipher("purplepineapple", "abc") ➞ "pur"

keyword_cipher("mubashir", "innokodakademija") ➞ "samucq"

Notes

  • Don't forget to remove duplicates after concatenating string with keyword.
  • Non-alphabetic characters must be left as they are.
python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.