Skip to content

Enigma Machine

The EnigmaMachine allows to create a python instance to cipher texts. It has to be composed by three parts: the PlugBoard, the Rotor and the Reflector. The settings of all three parts are defined as the machine's configuration.

The EnigmaMachine class can be imported directly from the enigma_cipher package:

from enigma_cipher import EnigmaMachine

EnigmaMachine(plugboard, rotors, reflector, reset_after_ciphering=True)

Constructor of the EnigmaMachine cipher. All specified components (plugboard, rotors and reflector) but have defined the same include_digits choice.

Parameters

  • plugboard PlugBoard:
    Component of the PlugBoard. It specifies the mapping among all the keys at the input/output level.
  • rotors Sequence of Rotor:
    Initialized Rotor instances in a sequence. While the historic enigma machine contained only three rotors, the EnigmaMachine class can work with as many or few as desired.
  • reflector Reflector:
    Component of the Reflector.
  • reset_after_ciphering bool (default = True):
    Flag that controls if the machine should reset to the initial configuration after ciphering a text.

EnigmaMachine.cipher_text(text)

Proceeds to cipher a given text. Ciphering will decode an encoded text if the machine has the same configuration as the initial machine that encoded the text.

Parameters

  • text str:
    Message to cipher (encode or decode) with the current configuration.

Returns

  • str:
    The ciphered text.

EnigmaMachine.export_configuration_to_json_file(output_path, force=False)

Export the machine configuration to a '.json' file.

Parameters

  • output_path str:
    Path to the file to contain the configuration. Values as "path/to/file" will create a file "path/to/file.json".
  • force bool (default = False):
    If True, allows overwriting existing output files. Otherwise, raises an error if the file already exists.

Class methods

EnigmaMachine.random_configuration(nof_rotors=None, reset_after_ciphering=True, include_digits=False)

Initializes the machine and all its components with a random configuration.

Parameters

  • nof_rotors int, optional:
    Number of rotors that compose the machine. If not specified, a randon number of them between 2 and 10 will be configured.
  • reset_after_ciphering bool, default = True:
    Flag that controls if the machine should reset to the initial configuration after ciphering a text.
  • include_digits bool, default = False:
    If True, the EnigmaMachine will include the digits to be ciphered. As default, only letters are to be ciphered.

Returns

  • EnigmaMachine:
    The instance initialized to a totally random configuration for all the elements.

EnigmaMachine.from_configuration(configuration, reset_after_ciphering=True)

Initializes the machine from a specified configuration.

Parameters

  • configuration dict:
    Mapping containing the machine configuration. It must be defined as the one returned in EnigmaMachine.initial_config.
  • reset_after_ciphering bool (default = True):
    Flag that controls if the machine should reset to the initial configuration after ciphering a text.

Returns

  • EnigmaMachine:
    The instance initialized to the specified configuration.

EnigmaMachine.from_configuration_file(input_path, reset_after_ciphering=True)

Initializes the machine from a .json configuration file. Configuration files from previous released versions are supported.

Parameters

  • input_path str:
    Path to the .json file containing the configuration.
  • reset_after_ciphering bool (default = True):
    Flag that controls if the machine should reset to the initial configuration after ciphering a text.

Returns

  • EnigmaMachine:
    The instance initialized to the specified configuration within the file.

Properties

EnigmaMachine.initial_configuration

Returns

  • dict:
    Initial configuration as a dictionary with the keys 'plugboard', 'rotors', 'reflector' and 'alphanumeric'.