PHP code example of julien-boudry / enigma-machine

1. Go to this page and download the library: Download julien-boudry/enigma-machine library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

julien-boudry / enigma-machine example snippets


use JulienBoudry\EnigmaMachine\{Enigma, EnigmaModel, Letter, ReflectorType, RotorConfiguration, RotorPosition, RotorType};

 rotating
    p2: RotorType::II,    // P2 - middle
    p3: RotorType::III,   // P3 - leftmost, slowest rotating
    ringstellungP1: Letter::B,   // Ring setting for P1 (default: A)
    ringstellungP2: Letter::A,   // Ring setting for P2 (default: A)
    ringstellungP3: Letter::A,   // Ring setting for P3 (default: A)
);

// 2. Create Enigma Instance
// Available models: WMLW (Wehrmacht/Luftwaffe), KMM3 (Kriegsmarine M3), KMM4 (Kriegsmarine M4)
$enigma = new Enigma(EnigmaModel::WMLW, $rotorsConfiguration, ReflectorType::B);

// 3. Set Initial Rotor Positions (Grundstellung)
$enigma->setPosition(RotorPosition::P1, Letter::M);
$enigma->setPosition(RotorPosition::P2, Letter::A);
$enigma->setPosition(RotorPosition::P3, Letter::A);

// 4. Configure Plugboard (Steckerbrett)
$enigma->plugLetters(Letter::A, Letter::C);
$enigma->plugLetters(Letter::B, Letter::Z);

// Encode a single letter
$encoded = $enigma->encodeLetter(Letter::A);
echo $encoded->toChar(); // Output: "U"

use JulienBoudry\EnigmaMachine\{Enigma, EnigmaModel, EnigmaTextConverter, Letter, ReflectorType, RotorConfiguration, RotorPosition, RotorType};

// Historical settings from Operation Barbarossa, 1941
$rotorsConfiguration = new RotorConfiguration(
    p1: RotorType::V, p2: RotorType::IV, p3: RotorType::II,
    ringstellungP1: Letter::L, ringstellungP2: Letter::U, ringstellungP3: Letter::B
);
$enigma = new Enigma(EnigmaModel::WMLW, $rotorsConfiguration, ReflectorType::B);
$enigma->setPosition(RotorPosition::P1, Letter::A);
$enigma->setPosition(RotorPosition::P2, Letter::L);
$enigma->setPosition(RotorPosition::P3, Letter::B);

// Plugboard (accepts space-separated pairs or array)
$enigma->plugLettersFromPairs('AV BS CG DL FU HZ IN KM OW RX');

// Decode a message
$ciphertext = 'EDPUDNRGYSZRCXNUYTPOMRMBOFKTBZREZKMLXLVEFGUEY';
$plaintext = $enigma->encodeLetters($ciphertext);
echo $plaintext;
// "AUFKLXABTEILUNGXVONXKURTINOWAXKURTINOWAXNORDW"
// German: "Aufklärungsabteilung von Kurtinowa, Kurtinowa, Nordwest..."
// Translation: "Reconnaissance division from Kurtinowa, Kurtinowa, northwest..."

$message = 'Panzer Division 7 nach München';

// Automatically converts to: "PANZERXDIVISIONXSIEBENXNACHXMUENCHEN"
$ciphertext = $enigma->encodeLatinText($message);

// With formatted output (traditional 5-letter groups)
$formatted = $enigma->encodeLatinText($message, formatOutput: true);
// Output: "GGOCS FFPLK ZFLHG UBMDA HHVRB WWGGM MAWOO L"

$enigmaEncoder = new Enigma(...);
$enigmaDecoder = clone $enigmaEncoder;

$binaryData = "\x00\x0F\xFF";
$ciphertext = $enigmaEncoder->encodeBinary($binaryData);

// 2. Decode (Enigma is reciprocal) and convert back to binary
$decryptedString = $enigmaDecoder->encodeLetters($ciphertext);
$originalBinary = EnigmaTextConverter::enigmaFormatToBinary($decryptedString);
// $originalBinary === $binaryData

use JulienBoudry\EnigmaMachine\{Enigma, EnigmaModel, ReflectorType, RotorConfiguration, RotorType};

// Create two identical Enigma instances (one for encoding, one for decoding)
$enigmaEncoder = new Enigma(
    EnigmaModel::WMLW,
    new RotorConfiguration(RotorType::I, RotorType::II, RotorType::III),
    ReflectorType::B
);
$enigmaDecoder = clone $enigmaEncoder;

// Encode a file
$enigmaEncoder->encodeFile('/path/to/input.bin', '/path/to/encoded.enigma');

// Decode the file (using the cloned instance with same initial state)
$enigmaDecoder->decodeFile('/path/to/encoded.enigma', '/path/to/decoded.bin');

$source = new \SplFileObject('/path/to/input.bin', 'rb');
$destination = new \SplFileObject('/path/to/encoded.enigma', 'wb');
$enigmaEncoder->encodeFile($source, $destination);

// Reduce chunk size for lower memory usage
Enigma::$fileChunkSize = 65536; // 64KB chunks

// Change Rotor Position
$enigma->setPosition(RotorPosition::P1, Letter::Z);

// Get Current Position
$pos = $enigma->getPosition(RotorPosition::P1);

// Modify Plugboard
$enigma->plugLetters(Letter::X, Letter::Y);
$enigma->unplugLetters(Letter::X); // Removes connection for X (and Y)

// Change Reflector
$enigma->mountReflector(ReflectorType::C);

// Change Rotors (

use JulienBoudry\EnigmaMachine\Reflector\ReflectorDora;

// Create a custom DORA reflector with specific wiring (all 13 pairs configurable)
$customDora = ReflectorDora::fromString('AQ BW CE DT FX GR HU IZ JK LN MO PS VY');

// Pass directly to the Enigma constructor
$enigma = new Enigma(
    EnigmaModel::WMLW,
    $rotorsConfiguration,
    $customDora  // ReflectorType or AbstractReflector accepted
);

// Or use default DORA wiring
$enigma = new Enigma(EnigmaModel::WMLW, $rotorsConfiguration, ReflectorType::DORA);

// Or mount after construction
$enigma->mountReflector($customDora);

// Disable compatibility checks on the Enigma instance
$enigma = new Enigma(EnigmaModel::WMLW, $rotorsConfiguration, ReflectorType::B, strictMode: false);

// Or change it after construction
$enigma->strictMode = false;

// With strictMode: false, you can:
// - Mount incompatible reflectors
$enigma->mountReflector(ReflectorType::BTHIN); // Would normally throw for WMLW model
// - Use plugboard on commercial models
$commercialEnigma = new Enigma(EnigmaModel::ENIGMA_K, $commercialRotors, ReflectorType::K, strictMode: false);
$commercialEnigma->plugLetters(Letter::A, Letter::Z); // Would normally throw

// Disable rotor configuration checks
$rotorsConfiguration = new RotorConfiguration(
    p1: RotorType::I,
    p2: RotorType::I, // Same rotor twice - would normally throw
    p3: RotorType::III,
    strictMode: false
);

// Or change it after construction
$rotorsConfiguration->strictMode = false;
$rotorsConfiguration->mountRotor(RotorPosition::P1, RotorType::BETA); // Would normally throw

// Create random setup
$enigma = Enigma::createRandom(EnigmaModel::WMLW);

// Get the generated configuration to save or display it
$config = $enigma->getConfiguration();
echo $config->getSummary();
// "Model: WMLW | Rotors: III-V-I | Ring: KNP | Position: BXG | Reflector: B | Plugs: AZ BY CX..."

// Recreate the same Enigma later
$sameEnigma = $config->createEnigma();

use JulienBoudry\EnigmaMachine\EnigmaTextConverter;

// Convert to Enigma format
echo EnigmaTextConverter::latinToEnigmaFormat('U-Boot 47'); // "UYYBOOTXVIERSIEBEN"

// Format in groups
echo EnigmaTextConverter::formatInGroups('HELLOWORLD'); // "HELLO WORLD"