The autokey cipher is closely related to the Vigenère cipher (both were invented by Blaise de Vigenère). Decryption involves the encrypted message and the key. For simplicity, let us assume all characters in the message and key are lowercase letters (no spaces, numbers or symbols etc.).
How to decrypt the cipher
- Convert all letters in the message and key into their 0-indexed counterparts. For example, 'A' is converted to 0, while 'Z' is converted to 25.
- Subtract the first letter of the key from the first letter of the message, and wrap the result to be within 0 to 25 inclusive. For example,
2 - 6 = 22
. - Append this result to both the plaintext message and the key.
- Repeat steps 2 and 3 for each letter in the message. Notice that after the initial key runs out, the key becomes the plaintext.
- Convert the plaintext message back into a string of capital letters.
Challenge
Write a program or function that, when given a message encrypted using an autokey cipher and its key, outputs the decrypted plaintext message.
- The message and key both contain only letters.
- You may assume that the message and key both contain at least 1 letter, but you cannot assume that the message is longer than the key.
- You may choose to have only lowercase or only capital letters.
- You may choose to instead take the input and output strings as lists of integers from 0 to 26 inclusive, however a program that processes letters is preferred.
- For example,
hyezvazczz, auto ==> helloworld
would instead be[7, 24, 4, 25, 21, 0, 25, 2, 25, 25], [0, 20, 19, 14] ==> [7, 4, 11, 11, 14, 22, 14, 17, 11, 3]
- You may also mix between string input/output and list input/output (e.g. input as two lists, output as string)
- For example,
Test Cases
(format: message, key ==> plaintext)
hyezvazczz, auto ==> helloworld
amrkegkaikmoqsuwyacegikmoq, alphabet ==> abcdefghijklmnopqrstuvwxyz
lavxxwgwgusgfaugth, amet ==> loremipsumdolorsit
nlfpe, verylongkey ==> short