A Vigenère Cipher is encrypted by repeating a keyword to be the length of the plaintext, and summing each character in that text with the corresponding letter in the plaintext modulo 26. (using A=0,B=1 etc.)
For example, if the keyword is LEMON
and the plaintext is ATTACKATDAWN
, then the key is repeated to form LEMONLEMONLE
, which is summed with the plaintext to form the ciphertext, LXFOPVEFRNHR
.
Write a program or function which, given a list of words and a ciphertext, outputs all possible plaintexts for that ciphertext, such that the plaintext is the concatenation of words in the list, and the key is also a word from the list.
All words use only upper-case letters A
to Z
.
Your code must run in polynomial time with respect to the total length of the ciphertext and words list.
Shortest code (in bytes) wins.
Example Inputs/Outputs
The input/output format for your program is flexible, but here I'm giving the list of words separated by commas followed by the ciphertext as input, and the output is the list of plaintexts separated by commas.
Inputs
LEMON,ATTACK,AT,DAWN,BUNNY
LXFOPVEFRNHR
A,AA,AAA,AAAA,AAAAA,AAAAAA,AAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
XHQT,GIXIIR,QTIA,AZXO,KSECS,THIS,CIPHER,IS,EASY,TO,CRACK,CODE,GOLF
ZVTXIWAMKFTXKODDZCNWGQV
A,B,C,D,E,F,G,H,Z
DBCCBHF
A,AA
AAA
Outputs
ATTACKATDAWN
(none)
THISCIPHERISEASYTOCRACK,XHQTGIXIIRQTIAAZXOKSECS
BZAAZFD,CABBAGE,DBCCBHF
AAA