Skip to Content
CoursesCSC122โปรแกรมจำลองเครื่องเอนิกมา 3 โรเตอร์

โปรแกรมจำลองเครื่องเอนิกมา 3 โรเตอร์

โจทย์

This is a simplified Enigma machine model. Although it doesn’t capture the full complexity of the historical Enigma machine, it provides a basic understanding of how such encryption devices transform plaintext into ciphertext.

Machine Description

The Enigma consists of the following components:

  1. Plugboard: A set of letter swaps that occur before and after encryption. Note that in this case we have only three pairs of plugboard swaps.

  2. Three Rotors: Each rotor has a fixed wiring represented by a string of 26 characters. The Enigma consists of rotors. In this case, it consists of 3 rotors. Each rotor with a different fixed wiring represented by a string of 26 characters. Each character in a rotor’s string represents the output for the corresponding alphabet letter (A-Z) when that letter passes through the rotor. The position of a letter in the string corresponds to a letter of the alphabet (e.g., the first character is the output for ‘A’, the second for ‘B’, and so on).

  3. Reflector Rotor: A special rotor that swaps letters in pairs.

Each input character will be encrypted through each rotor. After each letter is encrypted, the first rotor advances by one position. When it completes a full cycle (26 positions), it triggers the next rotor to advance by one position, mimicking the historical Enigma’s stepping mechanism. This rotation changes the encryption mapping for each letter, creating a more complex encryption pattern.


Encryption Process

For each character in the input:

  1. Plugboard Swap (Before Encryption):

    • If a plugboard mapping exists for this letter, swap it with its paired letter.
  2. Forward through the Rotors (Left to Right):

    • The letter is encrypted as it moves through Rotor 1 → Rotor 2 → Rotor 3.
  3. Reflector Rotor:

    • The letter is swapped based on the reflector rotor’s mapping.
  4. Backward through the Rotors (Right to Left):

    • The letter is encrypted in reverse order Rotor 3 → Rotor 2 → Rotor 1. Also reverse the process of mapping each character in step 2. (Hint: instead of finding character position on the rotor, we find the position of character on the order of alphabet, so-called “Lexical order”)
  5. Plugboard Swap (After Encryption):

    • Swap the letter again using the plugboard mapping.
  6. Rotor Advancement:

    • The first rotor advances by 1 position after encrypting a letter.
    • When it completes a full cycle (26 steps), it triggers the next rotor to advance.
  7. Space Handling:

    • If the character is a space (” ”), replace it with ”**” in the output.

Example

The Enigma consists of 3 rotors, 1 reflector rotor, and 1 plugboard with the following sequences:

Rotors:

  1. EKMFLGDQVZNTOWYHXUSPAIBRCJ
  2. AJDKSIRUXBLHWTMCQGZNPYFVOE
  3. BDFHJLCPRTXVZNYEIWGAKMUSQO

Reflector Rotor:

YRUHQSLDPXNGOKMIEBFZCWVJAT

Plugboard Mappings:

H-A L-E O-M
  • H swaps with A
  • L swaps with E
  • O swaps with M

Let say the input words are “Hello World”. The first input character ‘H’ is converted to ‘B’. Here is the explanation.

Encryption Steps for H:

  1. Plugboard (Before Encryption):

    • H Mapped to A.
  2. Forward through rotors:

    • Rotor 1: AE
    • Rotor 2: ES
    • Rotor 3: SG
  3. Reflector rotor:

    • G is mapped to L.
  4. Backward through rotors:

    • Rotor 3: LF (L is at the 5th position of Rotor 3. Hence, the letter at the lexical order of alphabet is F)
    • Rotor 2: FW (F is at the 22nd position of Rotor 2. Hence, the letter at the lexical order of alphabet is W)
    • Rotor 1: WN (W is at the 13th position of Rotor 1. Hence, the letter at the lexical order of alphabet is N)
  5. Plugboard (After Encryption):

    • N is not mapped, so it remains N.
  6. Final output character: N

Handling Spaces

If the input contains spaces, each space is replaced with ”**” in the output.

For example:

Input: “HELLO WORLD” Encrypted Output: “NVABM**HKSMN”


Input Format:

  1. The first line is a plaintext string to be encrypted.
  2. The second to fourth lines are the three rotor sequences (each a string of 26 uppercase letters).
  3. The fifth line is the reflector rotor sequence (a string of 26 uppercase letters, which defines letter swaps).
  4. The sixth line contains the plugboard mappings, consisting of pairs of letters separated by hyphens (-). Multiple pairs are space-separated. If no plugboard mappings exist, this line can be empty.

Output Format:

  • The encrypted sentence after passing through the Enigma encryption process.
  • Each space in the input is replaced by ”**” in the output.
InputOutput
HELLO WORLD
EKMFLGDQVZNTOWYHXUSPAIBRCJ
AJDKSIRUXBLHWTMCQGZNPYFVOE
BDFHJLCPRTXVZNYEIWGAKMUSQO
YRUHQSLDPXNGOKMIEBFZCWVJAT
H-A L-E O-M
NVABM**HKSMN

โค้ด

import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class Program { static final String ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; static class Rotor { String wiring; int position; Rotor(String wiring) { this.wiring = wiring; this.position = 0; } char encodeForward(char c) { int index = (ALPHABET.indexOf(c) + position) % 26; return wiring.charAt(index); } char encodeBackward(char c) { int index = wiring.indexOf(c); index = (index - position + 26) % 26; return ALPHABET.charAt(index); } boolean rotate() { if (position == 25) { position = 0; return true; } else { position = (position + 1) % 26; return false; } } } static class Reflector { String wiring; Reflector(String wiring) { this.wiring = wiring; } char reflect(char c) { return wiring.charAt(ALPHABET.indexOf(c)); } } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String message = scanner.nextLine().toUpperCase(); Rotor rotor1 = new Rotor(scanner.nextLine().toUpperCase()); Rotor rotor2 = new Rotor(scanner.nextLine().toUpperCase()); Rotor rotor3 = new Rotor(scanner.nextLine().toUpperCase()); Reflector reflector = new Reflector(scanner.nextLine().toUpperCase()); Map<Character, Character> plugboard = new HashMap<>(); for (int i = 0; i < 3; i++) { String pair = scanner.next().toUpperCase(); plugboard.put(pair.charAt(0), pair.charAt(2)); plugboard.put(pair.charAt(2), pair.charAt(0)); } for (char c : message.toCharArray()) { if (c == ' ') { System.out.print("**"); continue; } // Plugboard if (plugboard.containsKey(c)) c = plugboard.get(c); // Forward Pass c = rotor1.encodeForward(c); c = rotor2.encodeForward(c); c = rotor3.encodeForward(c); // Reflector c = reflector.reflect(c); // Backward Pass c = rotor3.encodeBackward(c); c = rotor2.encodeBackward(c); c = rotor1.encodeBackward(c); // Plugboard if (plugboard.containsKey(c)) c = plugboard.get(c); if (rotor1.rotate()) if (rotor2.rotate()) rotor3.rotate(); System.out.print(c); } } }

คำอธิบาย

ปรับปรุงล่าสุด