Vigenere Cipher
(Challenge added 1277 days ago.)
Or should that be the Belaso Cipher?
The Problem
The Vigenere cipher is a simple form of a polyalphabetic substitution, using a series of different Caesar ciphers based on the letters of a keyword. It's over 450 years old now, but that shouldn't stop us having a bit of golfing fun with it now.
The following information is largely taken from Wikipedia's Vigenere cipher page. You might find some interesting information that isn't included here, so I recommend you check it out.
Your job is to write some code which takes a keyword and some plaintext, and encrypts it according to the Vigenere cipher.
In a Caesar cipher, each letter of the alphabet is shifted along some number of places; for example, in a Caesar cipher of shift 3, A would become D, B would become E and so on. The Vigenere cipher consists of several Caesar ciphers in sequence with different shift values.
To encipher, a table of alphabets can be used, termed a tabula recta, Vigenere square, or Vigenere table. It consists of the alphabet written out 26 times in different rows, each alphabet shifted cyclically to the left compared to the previous alphabet, corresponding to the 26 possible Caesar ciphers. At different points in the encryption process, the cipher uses a different alphabet from one of the rows. The alphabet used at each point depends on a repeating keyword.
Click here to see the Vigenere square.
See the examples section below for a worked example.
More Information
Your code will be given both the keyword and the plaintext on stdin. It will be in the format
KEYWORD PLAINTEXT
with newlines at the end of each line.
- You should print the encrypted plaintext on stdout.
- Both the keyword and the plaintext will contain only the uppercase letters A through Z.
- Your code will be run three times and will need to pass all three tests to be deemed successful.
Examples
For example, suppose that the plaintext to be encrypted is
ATTACKATDAWN
and the keyword is
LEMON
The person sending the message chooses a keyword and repeats it until it matches the length of the plaintext, for example
LEMONLEMONLE
The first letter of the plaintext, A, is enciphered using the alphabet in row L, which is the first letter of the key. This is done by looking at the letter in row L and column A of the Vigenere square, namely L. Similarly, for the second letter of the plaintext, the second letter of the key is used; the letter at row E and column T is X. The rest of the plaintext is enciphered in a similar fashion giving a ciphertext of
LXFOPVEFRNHR
Your code will be run three times for each submission, and will receive a random keyword and plaintext each time. Click here to see examples of this. Reload the page to see more examples.
