A Coincidental Note (con't)
\emph{This problem's story is a continuation of the previous problem, but
the solution is independant.}
Ok, now you know how long the key is, but you still don't know it.
Rotating an alphabet isn't difficult, but you need to run a lot of
them. Johnny Hooker, Gondorff's partner, is probably almost here.
You'd love to leave a little, encrypted note in the place of the
one you found...
\emph{And a little more background on breaking Vigen\`ere ciphers}...
This simple rotation is just a building block. You'll need this
as a subroutine which will be applied many, many times. The
general recipe for breaking Vigen\`ere ciphers isn't too difficult,
actually.
Take each substring with the same rotation (those corresponding to
the same letter of the key). For the first substring, guess the
rotation by mapping the most common letter to the most common letter
of some sample plaintext (normally `E'). Later guesses should try
less likely matches, typically falling down the string ``etaoinshrdlu...''
For each successive substring, guess the rotation by checking the
likelihood of possible digrams, pairs of letters. For example, if a
letter in the previous substring was 'T', the corresponding letter in
the current substring is probably 'H'.
Continue these guesses until you find the plaintext. It could take
a while, but not as long as factoring 1024-bit pseudoprimes like
those used in public key cryptosystems.
Write a program to translate a block of text into a rotated alphabet.
A reasonable guess at the correct rotation is to first try mapping the
most common letter to `E'. Afterwards, try rotating the most common letter
to an input character.
The first input will be a block of at most 30 letters. After
displaying the initial attempt, repeatedly prompt for another letter
and redisplay the newly transformed string.
In the case of ties, pick the letter which occurs first in the text.
Ciphertext: qddazqageqjbqofmfuaze
Attempt: erroneousexpectations
Rotate `q' to: r
Decoded: reebarbhfrkcrpgngvbaf
Ciphertext: yqnekqocaknca
Attempt: mebsyecqoybqo
Rotate `q' to: s
Decoded: aspgmsqecmpec
Rotate `q' to: r
Decoded: zroflrpdblodb
Rotate `q' to: u
Decoded: curiousgeorge
Ciphertext: blah
Attempt: eodk
Rotate `b' to: c
Decoded: cmbi
Rotate `b' to: d
Decoded: dncj
Ciphertext: tmbbkefbmfduowepmk
Attempt: lettcwxtexvmgowhec
Rotate `m' to: t
Decoded: atiirlmitmkbvdlwtr
Rotate `m' to: a
Decoded: happystpatricksday