Location: PHPKode > scripts > Enigma > enigma/core/Enigma.php
<?
/**
 * @author David Kargl <hide@address.com>
 * @copyright Copyright (c) 2007, David Kargl
 * @version 1.0
 * @package Enigma
 */

/**
 * Wehrmacht/Luftwaffe (3-rotor model)
 */
define("ENIGMA_MODEL_WMLW", 0);
/**
 * Kriegsmarine M3 (3-rotor model)
 */
define("ENIGMA_MODEL_KMM3", 1);
/**
 * Kriegsmarine M4 (4-rotor model)
 */
define("ENIGMA_MODEL_KMM4", 2);


/**
 * ID Rotorposition 1
 */
define("ENIGMA_ROTOR_1",     0);
/**
 * ID Rotorposition 2
 */
define("ENIGMA_ROTOR_2",     1);
/**
 * ID Rotorposition 3
 */
define("ENIGMA_ROTOR_3",     2);
/**
 * ID Rotorposition 4
 * only available in model Kriegsmarine M4, also call 'Greek rotor'
 * this rotor never turns
 */
define("ENIGMA_ROTOR_GREEK", 3);


/**
 * ID Rotor I
 */
define("ENIGMA_ROTOR_I",     0);
/**
 * ID Rotor II
 */
define("ENIGMA_ROTOR_II",    1);
/**
 * ID Rotor III
 */
define("ENIGMA_ROTOR_III",   2);
/**
 * ID Rotor IV
 */
define("ENIGMA_ROTOR_IV",    3);
/**
 * ID Rotor V
 */
define("ENIGMA_ROTOR_V",     4);
/**
 * ID Rotor VI
 * only available in model Kriegsmarine M3 and M4
 */
define("ENIGMA_ROTOR_VI",    5);
/**
 * ID Rotor VII
 * only available in model Kriegsmarine M3 and M4
 */
define("ENIGMA_ROTOR_VII",   6);
/**
 * ID Rotor VII
 * only available in model Kriegsmarine M3 and M4
 */
define("ENIGMA_ROTOR_VIII",   7);
/**
 * ID Rotor BETA
 * only available in model Kriegsmarine M4 as 'Greek rotor'
 */
define("ENIGMA_ROTOR_BETA",  8);
/**
 * ID Rotor GAMMA
 * only available in model Kriegsmarine M4 as 'Greek rotor'
 */
define("ENIGMA_ROTOR_GAMMA", 9);


/**
 * ID Reflector B
 */
define("ENIGMA_REFLECTOR_B",     0);
/**
 * ID Reflector C
 */
define("ENIGMA_REFLECTOR_C",     1);
/**
 * ID Reflector B Thin
 * only available in model Kriegsmarine M4
 */
define("ENIGMA_REFLECTOR_BTHIN", 2);
/**
 * ID Reflector C Tthin
 * only available in model Kriegsmarine M4
 */
define("ENIGMA_REFLECTOR_CTHIN", 3);


define("ENIGMA_KEY_A", 0);
define("ENIGMA_KEY_B", 1);
define("ENIGMA_KEY_C", 2);
define("ENIGMA_KEY_D", 3);
define("ENIGMA_KEY_E", 4);
define("ENIGMA_KEY_F", 5);
define("ENIGMA_KEY_G", 6);
define("ENIGMA_KEY_H", 7);
define("ENIGMA_KEY_I", 8);
define("ENIGMA_KEY_J", 9);
define("ENIGMA_KEY_K", 10);
define("ENIGMA_KEY_L", 11);
define("ENIGMA_KEY_M", 12);
define("ENIGMA_KEY_N", 13);
define("ENIGMA_KEY_O", 14);
define("ENIGMA_KEY_P", 15);
define("ENIGMA_KEY_Q", 16);
define("ENIGMA_KEY_R", 17);
define("ENIGMA_KEY_S", 18);
define("ENIGMA_KEY_T", 19);
define("ENIGMA_KEY_U", 20);
define("ENIGMA_KEY_V", 21);
define("ENIGMA_KEY_W", 22);
define("ENIGMA_KEY_X", 23);
define("ENIGMA_KEY_Y", 24);
define("ENIGMA_KEY_Z", 25);

/**
 * encoding table
 * eg.: ENIGMA_KEY_A=>"A", ENIGMA_KEY_B=>"B", ...
 * @global array $ENIGMA_ALPHABET
 */
$ENIGMA_ALPHABET = array(
ENIGMA_KEY_A=>"A", ENIGMA_KEY_B=>"B", ENIGMA_KEY_C=>"C", ENIGMA_KEY_D=>"D", ENIGMA_KEY_E=>"E",
ENIGMA_KEY_F=>"F", ENIGMA_KEY_G=>"G", ENIGMA_KEY_H=>"H", ENIGMA_KEY_I=>"I", ENIGMA_KEY_J=>"J",
ENIGMA_KEY_K=>"K", ENIGMA_KEY_L=>"L", ENIGMA_KEY_M=>"M", ENIGMA_KEY_N=>"N", ENIGMA_KEY_O=>"O",
ENIGMA_KEY_P=>"P", ENIGMA_KEY_Q=>"Q", ENIGMA_KEY_R=>"R", ENIGMA_KEY_S=>"S", ENIGMA_KEY_T=>"T",
ENIGMA_KEY_U=>"U", ENIGMA_KEY_V=>"V", ENIGMA_KEY_W=>"W", ENIGMA_KEY_X=>"X", ENIGMA_KEY_Y=>"Y",
ENIGMA_KEY_Z=>"Z"
);

/**
 * Size of the Enigma alphabet
 */
define("ENIGMA_ALPHABET_SIZE", sizeof($ENIGMA_ALPHABET));


/**
 * stores the setup for all available rotors
 * fields are
 * key: ID of the rotor
 * wiring: the setup for the wiring of a rotor
 * used: IDs of models, this rotos can be used in
 * notches: the turnover positions of a rotor
 * @global array $ENIGMA_ROTORS
 */
$ENIGMA_ROTORS = array(
array("key"=>ENIGMA_ROTOR_I,     "wiring"=>"EKMFLGDQVZNTOWYHXUSPAIBRCJ", "used"=>array(ENIGMA_WM, ENIGMA_KM_M3, ENIGMA_KM_M4), "notches"=>array(ENIGMA_KEY_Q)),
array("key"=>ENIGMA_ROTOR_II,    "wiring"=>"AJDKSIRUXBLHWTMCQGZNPYFVOE", "used"=>array(ENIGMA_WM, ENIGMA_KM_M3, ENIGMA_KM_M4), "notches"=>array(ENIGMA_KEY_E)),
array("key"=>ENIGMA_ROTOR_III,   "wiring"=>"BDFHJLCPRTXVZNYEIWGAKMUSQO", "used"=>array(ENIGMA_WM, ENIGMA_KM_M3, ENIGMA_KM_M4), "notches"=>array(ENIGMA_KEY_V)),
array("key"=>ENIGMA_ROTOR_IV,    "wiring"=>"ESOVPZJAYQUIRHXLNFTGKDCMWB", "used"=>array(ENIGMA_WM, ENIGMA_KM_M3, ENIGMA_KM_M4), "notches"=>array(ENIGMA_KEY_J)),
array("key"=>ENIGMA_ROTOR_V,     "wiring"=>"VZBRGITYUPSDNHLXAWMJQOFECK", "used"=>array(ENIGMA_WM, ENIGMA_KM_M3, ENIGMA_KM_M4), "notches"=>array(ENIGMA_KEY_Z)),
array("key"=>ENIGMA_ROTOR_VI,    "wiring"=>"JPGVOUMFYQBENHZRDKASXLICTW", "used"=>array(ENIGMA_KM_M3, ENIGMA_KM_M4), "notches"=>array(ENIGMA_KEY_M, ENIGMA_KEY_Z)),
array("key"=>ENIGMA_ROTOR_VII,   "wiring"=>"NZJHGRCXMYSWBOUFAIVLPEKQDT", "used"=>array(ENIGMA_KM_M3, ENIGMA_KM_M4), "notches"=>array(ENIGMA_KEY_M, ENIGMA_KEY_Z)),
array("key"=>ENIGMA_ROTOR_VIII,  "wiring"=>"FKQHTLXOCBJSPDZRAMEWNIUYGV", "used"=>array(ENIGMA_KM_M3, ENIGMA_KM_M4), "notches"=>array(ENIGMA_KEY_M, ENIGMA_KEY_Z)),
array("key"=>ENIGMA_ROTOR_BETA,  "wiring"=>"LEYJVCNIXWPBQMDRTAKZGFUHOS", "used"=>array(ENIGMA_KM_M4), "notches"=>array()),
array("key"=>ENIGMA_ROTOR_GAMMA, "wiring"=>"FSOKANUERHMBTIYCWLQPZXVGJD", "used"=>array(ENIGMA_KM_M4), "notches"=>array()),
);


/**
 * stores the setup for all available reflectors
 * fields are
 * key: ID of the reflector
 * wiring: the setup for the wiring of a reflector
 * used: IDs of models, this rotos can be used in
 * @global array $ENIGMA_REFLECTORS
 */
$ENIGMA_REFLECTORS = array(
array("key"=>ENIGMA_REFLECTOR_B,     "wiring"=>"YRUHQSLDPXNGOKMIEBFZCWVJAT", "used"=>array(ENIGMA_WM, ENIGMA_KM_M3)),
array("key"=>ENIGMA_REFLECTOR_C,     "wiring"=>"FVPJIAOYEDRZXWGCTKUQSBNMHL", "used"=>array(ENIGMA_WM, ENIGMA_KM_M3)),
array("key"=>ENIGMA_REFLECTOR_BTHIN, "wiring"=>"ENKQAUYWJICOPBLMDXZVFTHRGS", "used"=>array(ENIGMA_KM_M4)),
array("key"=>ENIGMA_REFLECTOR_CTHIN, "wiring"=>"RDOBJNTKVEHMLFCWZAXGYIPSUQ", "used"=>array(ENIGMA_KM_M4))
);


/**
 * converts a character into its pendant in the Enigma alphabet
 * @param string character to convert
 * @return integer represention of a character in the Enigma alphabet
 * @uses $ENIGMA_ALPHABET
 */
function enigma_l2p($l) {
	global $ENIGMA_ALPHABET;
	return array_search(strtoupper($l), $ENIGMA_ALPHABET);
}


/**
 * converts an element of the Enigma alphabet to 'our' alphabet
 * @param integer element to be converted
 * @return string resulting character
 * @uses $ENIGMA_ALPHABET
 */
function enigma_p2l($p) {
	global $ENIGMA_ALPHABET;
	return $ENIGMA_ALPHABET[$p];
}

require_once("wiring.class.php");
require_once("plugboard.class.php");
require_once("reflector.class.php");
require_once("rotor.class.php");
require_once("enigma.class.php");

?>
Return current item: Enigma