The Key to Cryptography
Time limit2sMemory limit512 MB
Given an autokey cipher's ciphertext and secret word, recover the original plaintext message.
- Level
Easy3 of 10
- Topics
- String, Implementation
- Solved
- No attempts yet
Problem
Suppose you have to encrypt a top secret message such as "SEND MORE MONKEYS". You could use a simple substitution cipher that replaces each letter of the alphabet with a different letter, but ciphers like that break easily: some letters (E, S, A) turn up far more often than others (Q, Z, X). Varying the substitution applied to each letter is safer. The autokey cipher works that way.
To encrypt a message, first pick a secret word, say "ACM", and put it in front of the message. Truncate the longer string to the length of the message and call it the key. The th letter of the key encrypts the th letter of the original message. Each key letter is a cyclic shift amount: A means a shift of 0, B a shift of 1, and so on. With "ACM" as the secret word, the message is encrypted like this.
SENDMOREMONKEYS (message)
ACMSENDMOREMONK (key)
------------------------------
SGZVQBUQAFRWSLC (ciphertext)
The letter E in the message became G the first time it appeared, because the matching key letter was C, a shift of 2. The next two times it became Q and then S.
Given a ciphertext and the secret word, recover the original message.
Input
The input has two lines. The first line holds the ciphertext and the second line holds the secret word. Both lines contain uppercase letters only. If the secret word is at least as long as the ciphertext, the key is the prefix of the secret word whose length equals the ciphertext length.
Output
Print the original message that produced the given ciphertext under the given secret word.