Decoding Task
InterviewTime limit2sMemory limit128 MB
Given an encrypted message and the same message with a space prepended, recover the shared XOR key bytes.
- Level
Medium4 of 10
- Topics
- Bit manipulation, Implementation
- Solved
- No attempts yet
Problem

In the near future, all research and publication about cryptography is outlawed worldwide on national-security grounds: if cryptographic literature were public, anyone (including criminals) could hide their plans from the authorities. As a result, public cryptographic systems no longer exist, and everyone who needs strong protection for their secrets must invent a proprietary algorithm.
The ACM Corporation has many competitors eager to learn its trade secrets. Protecting those secrets is hard because ACM must use intercontinental communication lines that are easy to eavesdrop on, unlike its well-guarded internal lines. To defend itself, ACM invented the Intercontinental Cryptographic Protection Code (ICPC), which it considers unbreakable — until now.
A group of hackers, hired by an unnamed rival, set out to break ICPC. They first bribed one of ICPC's programmers and learned how it works. ICPC uses a very long key: a sequence of bytes produced by a sophisticated random physical process. The key is changed every week and encrypts every message sent over the intercontinental lines during that week. ICPC is extremely fast because it simply computes a bitwise exclusive OR (XOR) between the message bytes and the key. That is, the -th byte of the encrypted message is , where is the -th byte of the key and is the -th byte of the original clear-text message.
The hackers now need a reliable way to obtain the weekly key. They found a clerk who sends weekly newsletters to employees just after each key change; a newsletter is long enough that studying it together with its encrypted form would reveal a large part of the key. But no employee would leak a newsletter, because a Non-Disclosure Agreement makes the penalty for disclosure death.
Instead, they convinced the clerk (for a small reward) to do something seemingly innocent: when copying the newsletter to the corporation, he inserts one extra space character at the beginning of some copies while sending other copies unchanged.
Now recovering the key is straightforward — and that is your job. You are given two ICPC-encrypted messages. The first message is bytes long. The second message is bytes long and is the encryption of the same clear text as the first, but with one extra space character (the byte with decimal value ) inserted at the beginning. Find the first bytes of the key that was used to encrypt the messages.
Input
The input consists of two lines.
- The first line contains characters and represents the first encrypted message, which is bytes long.
- The second line contains characters and represents the second encrypted message, which is bytes long.
Here . Each message is written on a single line in hexadecimal, byte by byte and without spaces. Each byte is written as two characters from 0-9 and A-F giving the hexadecimal value of that byte.
Output
Print a single line containing the recovered bytes of the key, in the same hexadecimal format as the input (two uppercase hexadecimal characters per byte, no spaces).