22. Vigenère cipher

The attic of the university library smelled of wax and dust, of bindings too long untouched. A pale light slipped through the dormer window, glinting on glass jars of dried ink and the brass latch of an old globe.

Frau Nettelmann, the archivist, stood at the center of a small storm of papers. „They’ve sent up another crate from the cellar,“ she sighed. „Student notebooks, mostly. Keep the ones that still have legible dates. The rest we’ll consign to storage again.“

Johann, sleeves rolled up, peered into the crate and groaned. „Not a single sketch of anatomy, not even a scandalous note. Just arithmetic and dust. I swear these students lived without imagination.“

Mihkel smiled faintly and said nothing. He lifted one of the notebooks from the pile—a small, leather-bound thing, the edges soft from handling, its clasp rusted shut. Inside, the pages were covered not with equations or German notes, but with lines of letters that made no sense: mdzgs thkrg wzrkl...

„Gibberish,“ Johann declared. „Someone went mad from exams and decided to invent a language.“

But Mihkel’s eyes narrowed. The rhythm of the letters—no punctuation, yet too deliberate for nonsense—stirred something familiar. He copied a few lines into his own notebook.

That evening, in the quiet of his workshop, he examined the pattern by lamplight. Each cluster repeated after irregular intervals, as if the text obeyed a shifting alphabet. „A key,“ he murmured. „Not a code of words, but of motion.“

He recalled a mention in a French journal—a method where each letter is shifted by the one in a key. It was elegant, precise, and, in its own way, musical.

He fed the lines into the Logic Mill, adjusting its plates until the alphabet itself became a sequence of numbers. The gears began to turn, the machine whispering through letters instead of digits. One by one, nonsense resolved into sense.

On the input tape, you'll get a key and an encrypted message separated by :. Your task is to decrypt the message using the Vigenère cipher.

The cipher works as follows: each letter of the plaintext is shifted forward in the alphabet by the position of the corresponding key letter (a = 0, b = 1, ..., z = 25). For example, b (1) + e (4) = f (5). If the shift goes past 25 (z), you just subtract 26. For example, v (21) + n (13) = i (21 + 13 = 34 - 26 = 8).

To decrypt, you shift backward instead.

The key, the encrypted text, and the plaintext consist only of lowercase Latin letters (a–z), no spaces or punctuation. The key is always at least as long as the encrypted text (so it never repeats).

For example, if the input tape is mdzgstf:thkrg (mdzgstf is the key, thkrg is the encrypted message) the output tape should be hello:

19 (t) - 12 (m) =             7 (h)
 7 (h) -  3 (d) =             4 (e)
10 (k) - 25 (z) = -15 + 26 = 11 (l)
17 (r) -  6 (g) =            11 (l)
 6 (g) - 18 (s) = -12 + 26 = 14 (o)

Sign in to submit your solution.