The Vigenère Cipher: How To Encipher A Plaintext Message
The Vigenère Cipher: How To Encipher A Plaintext Message
For many centuries, most - if not all - known methods for enciphering messages considered a single cipher alphabet. For example, Julius Caesar used a substitution cipher that replaced each letter in the plaintext message by a letter three places further down in the alphabet ('A' would be 'D' and so on). Sometime in the 16th century, a French diplomat called Blaise de Vigenre perfected a new cipher based on previous work by Leon Alberti, Johannes Trithemius and Giovanni Porta. The most significant breakthrough was the use of multiple cipher alphabets, which turned the cipher impregnable to frequency analysis (a technique for deducing the plaintext from a ciphertext).
| 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 ----------+---------------------------------------------------1 (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 A 2 (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 A B 3 (C) | D E F G H I J K L M N O P Q R S T U V W X Y Z A B C 4 (D) | E F G H I J K L M N O P Q R S T U V W X Y Z A B C D 5 (E) | F G H I J K L M N O P Q R S T U V W X Y Z A B C D E C 6 (F) | G H I J K L M N O P Q R S T U V W X Y Z A B C D E F I 7 (G) | H I J K L M N O P Q R S T U V W X Y Z A B C D E F G P 8 (H) | I J K L M N O P Q R S T U V W X Y Z A B C D E F G H H 9 (I) | J K L M N O P Q R S T U V W X Y Z A B C D E F G H I E 10 (J) | K L M N O P Q R S T U V W X Y Z A B C D E F G H I J R 11 (K) | L M N O P Q R S T U V W X Y Z A B C D E F G H I J K 12 (L) | M N O P Q R S T U V W X Y Z A B C D E F G H I J K L A 13 (M) | N O P Q R S T U V W X Y Z A B C D E F G H I J K L M L 14 (N) | O P Q R S T U V W X Y Z A B C D E F G H I J K L M N P 15 (O) | P Q R S T U V W X Y Z A B C D E F G H I J K L M N O H 16 (P) | Q R S T U V W X Y Z A B C D E F G H I J K L M N O P A 17 (Q) | R S T U V W X Y Z A B C D E F G H I J K L M N O P Q B 18 (R) | S T U V W X Y Z A B C D E F G H I J K L M N O P Q R E 19 (S) | T U V W X Y Z A B C D E F G H I J K L M N O P Q R S T 20 (T) | U V W X Y Z A B C D E F G H I J K L M N O P Q R S T S 21 (U) | V W X Y Z A B C D E F G H I J K L M N O P Q R S T U 22 (V) | W X Y Z A B C D E F G H I J K L M N O P Q R S T U V 23 (W) | X Y Z A B C D E F G H I J K L M N O P Q R S T U V W 24 (X) | Y Z A B C D E F G H I J K L M N O P Q R S T U V W X 25 (Y) | Z 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 26 (Z) | 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
Then, the multiple cipher alphabets (rows in the Vigenre square) to be used in the encipherment must be chosen. A practical way of doing this is via a keyword. For example, the keyword 'MIUP' selects the cipher alphabets 13, 9, 21 and 16 (see underlined rows in the Vigenre square). In the final step, each letter in the plaintext message is enciphered according to the corresponding cipher alphabet. For example, suppose the plaintext message is 'programming' and the keyword is 'MIUP.' The encoded message would be:
plaintext message keyword cipher alphabets ----------------encoded message p r o g r a m m i n g M I U P M I U P M I U 13 9 21 16 13 9 21 16 13 9 21 ---------------------------------C A J W E J H C V W B
The letter 'p' in the plaintext message is enciphered using cipher alphabet 13 into the letter 'C'. The 'C' comes from the intersection of column 'p' with row 13 in the Vigenre square. The second letter in the plaintext message, 'r', is enciphered into the letter 'A' using cipher alphabet 9 (check the intersection of column 'r' with row 9), and so on. If necessary, as in this case, the keyword can be repeated multiple times to match the length of the plaintext message.
The multiple cipher alphabets of 'MIUP' are, again, 13, 9, 21 and 16 (grey rows in the Vigenre square above). Each plaintext message letter is obtained by finding the corresponding encoded letter in the row of the current cipher alphabet and then checking the letter on top of the column. For example, the letter 'p' in the plaintext message is on top of the column where 'C' (the encoded letter) appears in row (cipher alphabet) 13. The decipherment ends when the last encoded letter is deciphered. (This background was written based on Simon Singh's "The Code Book," p. 45-51.)
Input
The input is a single line containing a message enciphered with the Vigenre cipher (in uppercase). The only knowledge about this message is that after being deciphered it represents a 3 digit number written as text (for example: 'oneoneone' or 'zerozeroseven').
Output
The output is all possible decipherments of the encoded message. For example, the encoded message 'PQRPQRPQR' means 'oneoneone' when the keyword is 'ACM', but also means 'oneonetwo' or 'oneonesix' when the keywords are, respectively, 'ACMACMVTC' and 'ACMACMWHT', and so on. Each decipherment is written in a single line, as shown in the sample output. The text to the left of the '->' symbol is the keyword (in uppercase) and to the right is the corresponding plaintext 3 digit number (in lowercase). There should be no empty line between decipherments. Note: the output must be sorted in descending order by value of the plaintext number ('nineninenine' > 'ninenineeight' > ... > 'zerozeroone' > 'zerozerozero').
Sample Input
PQRPQRPQR
Sample Output
WHTWHTWHT WHTWHTVTC WHTWHTACM WHTVTCWHT WHTVTCVTC WHTVTCACM WHTACMWHT WHTACMVTC WHTACMACM VTCWHTWHT VTCWHTVTC VTCWHTACM VTCVTCWHT VTCVTCVTC VTCVTCACM VTCACMWHT VTCACMVTC VTCACMACM ACMWHTWHT ACMWHTVTC ACMWHTACM ACMVTCWHT ACMVTCVTC -> -> -> -> -> -> -> -> -> -> -> -> -> -> -> -> -> -> -> -> -> -> -> sixsixsix sixsixtwo sixsixone sixtwosix sixtwotwo sixtwoone sixonesix sixonetwo sixoneone twosixsix twosixtwo twosixone twotwosix twotwotwo twotwoone twoonesix twoonetwo twooneone onesixsix onesixtwo onesixone onetwosix onetwotwo