PROBLEM 3: Simplified Enigma Machine


Description:

Substitution cyphers which encode a message by substituting one character for another go back at least as far as Julius Caesar, who used a rotating character scheme to encode military orders. This simple type of encryption is vulnerable to statistical attacks, however, as anyone who has solved CRYPTOGRAM puzzles can attest.

In World War II, the Nazi military employed an encryption scheme that addressed this weakness of simple substitution cyphers. This scheme, implemented by typewriter-sized devices known as Enigma machines, gave the Nazis a tactical advantage that greatly contributed to their early success in the war. In fact, the eventual breaking of this coding scheme by researchers at Bletchley Park, England (including Alan Turing) is hailed as one of the turning points of the war.

Enigma machines used interchangeable rotors that could be placed in different orientations to obtain different substitution patterns. In addition, the rotors rotated after encoding each character, so the pattern was constantly changing. A simple model of a two-rotor Enigma machine is shown to the right.

To encrypt a character using such a machine, find the character on the inside rotor (i.e., the inner wheel) and note the character aligned with it on the backplate (i.e., the outside wheel), then find that character on the second rotor (i.e., the middle wheel) and output the one aligned with it on the backplate. After a character is encrypted, turn the inside rotor clockwise one step. Whenever the inside rotor returns to its original orientation, the second rotor turns once in lock-step, just like the odometer in a car.

For example, in the configuration to the left the character 'A' would be encrypted as 'N' since 'A' on the inside rotor is aligned with 'H' on the backplate, and 'H' on the second rotor is aligned with 'N' on the backplate. After performing this encryption, the inner rotor is rotated clockwise, so the letter 'A' would next be encrypted as 'D'.

Input:

The input will consist of four lines of text. The first three lines represent the three wheels on the Enigma machine: the inner rotor, middle rotor, and backplate, respectively. Each line will contain a permutation of the 26 capital letters plus the '#' symbol. There will be no spaces between the characters, and corresponding characters on each line are aligned on their respective wheels. That is, the first characters on each line are aligned with each other, the second characters are aligned with each other, and so on. The fourth input line contains the message to be encoded. You may assume that this message contains only those letters that appear on the wheels ('A'..'Z', '#') and that the message will be terminated by a period.

Example Input:

#GNUAHOVBIPWCJQXDKRYELSZFMT
#EJOTYCHMRWAFKPUZDINSXBGLQV
#BDFHJLNPRTVXZACEGIKMOQSUWY
AAA#ALAN#TURING#RULES.

Output:

The output should consist of a single line of text, namely the encoded message using the two-rotor Enigma machine with the specified initial configuration. The output should be terminated by a period.

Example Output:

NDUXAJHRAAEGFLLLKPOOL.