View on
MetaCPAN is shutting down
For details read Perl NOC. After June 25th this page will redirect to
Alistair Francis > Crypt-Enigma-1.4 > Crypt::Enigma



Annotate this POD

View/Report Bugs
Module Version: 1.4   Source   Latest Release: Crypt-Enigma-1.5


Crypt::Enigma - Perl implementation of the Enigma cipher


This module is a complete working Perl implementation of the Enigma Machine used during World War II. The cipher calculations are based on actual Enigma values and the resulting ciphered values are as would be expected from an Enigma Machine.

The implementation allows for all of the Rotors and Reflectors available to the real world Enigma to be used. A Steckerboard has also been implemented, allowing letter substitutions to be made.

The list of available rotors is as follows:

RotorI, RotorII, RotorIII, RotorIV, RotorV, RotorVI, RotorVII, RotorVIII, RotorBeta, RotorGamma.

The list of available reflectors is as follows:

ReflectorB, ReflectorBdunn, ReflectorC, ReflectorCdunn.

As with the real world Enigma, a minimum of 3 and a maximum of 5 rotors along with 1 reflector may be defined for each encryption/decryption.


  use Crypt::Enigma;

  my $args = {
    rotors       => [ 'RotorI', 'RotorII', 'RotorIII' ],
    startletters => [ 'A', 'B', 'C' ],
    ringsettings => [ '0', '5', '10' ],
    reflector    => 'ReflectorB',

  $enigma = Crypt::Enigma->new( $args );

  # Change rotor settings
  $enigma->setRotor( 'RotorVI', 'Z', '3', 1 );

  # Set the letter substitutions
  $enigma->setSteckerBoard( [ 'G', 'C' ] );

  # Encode the plaintext
  $cipher_text = $enigma->cipher( $plain_text );

  # Decode the ciphertext 
  $plain_text = $enigma->cipher( $cipher_text );



A Crypt::Enigma object is created by calling the new constructor either with, or without arguments. If the constructor is called without arguments the defaults values will be used (unless these are set using the setRotor method detailed below).

new ( ARGS )

The arguments which can be used to create a Crypt::Enigma instance are as follows:


The first four are to be passed in as references to arrays, while the last argument is a scalar.


cipher ( ARGS )

This method crypts and decrypts the supplied argument containing a string of text. Any characters which are not from the English alphabet (punctuation, numerics, etc) are ignored.

setRotor ( ARGS )

The setRotor method is called to set a rotor of the Enigma to specific settings. The arguments to be passed in are as follows:

  -rotor name (eg. RotorI, RotorII, etc)
  -initial start letter (eg. 'A', 'B', etc)
  -ring setting (eg. '0', '1', etc)
  -rotor number (eg. '1', '2', etc)

If incorrect values are passed in, the default settings are used.

setReflector ( ARG )

The setReflector method is called to set the reflector of the Enigma Machine. The argument to be passed in is a string containing the name of any of the available reflectors.

setSteckerBoard ( ARGS )

The Steckerboard is set by calling the setSteckerBoard method and supplying a reference to an array as the first argument.

The array should contain a set of letter pairs, such as:

  [ 'A', 'B', 'C', 'D' ];

In this example, each instance of the letter 'A' will be replaced with the letter 'B' (and vice-versa) and each instance of the letter 'C' will be replaced with the letter 'D' (and vice-versa).


Returns an array containing the rotor names currently defined for encryption/decryption.


Returns an array containing the start letters currently defined for encryption/decryption.


Returns an array containing the ring settings currently defined for encryption/decryption.


Returns a string containing the name of the reflector currently defined for encryption/decryption.


This method will print out (to STDERR) the current rotor settings.

setDebug ( ARG )

The setDebug method is used to set the debug value of the Crypt::Enigma object. The value of the argument can be either 1 (debug on) or 0 (debug off). The debug value is set to 0 by default.


None, but that does not mean there are not any.


Alistair Francis, <>

syntax highlighting: