Crypt::OOEnigma - A World War II Enigma machine in a flexible OO framework
use Crypt::OOEnigma; my $enigma = Crypt::OOEnigma->new(); # OR set the default rotors' start positions Crypt::OOEnigma->new( start_positions => [10,20,5] ); # OR choose rotors from the set (0..5) Crypt::OOEnigma->new( rotor_choice => [3,4,5]); # OR both Crypt::OOEnigma->new( rotor_choice => [3,4,5], start_positions => [10,20,5] ); my $cipher = $enigma->encipher("Some Text"); my $decode = $enigma->decipher($cipher); # OR, since Enigma's are self-inverse: my $decode = $enigma->encipher($cipher);
The Enigma Machine was a much-used encryption device in the Second World War. It was an electrical device, somewhat like a typewriter, combining substitution and rotation ciphers in such a manner that the resulting overall cipher was difficult to break, unlike the constituent ciphers. The machine could decode as well as encoding messages.
In short, the electrical Enigma is constructed from a number of rotors - usually 3, each of which implements a simple substitution cipher on the letters of the alphabet only, and a reflector. The reflector is a self-inverse simple substitution cipher with no short circuits. On receiving a clear-text message for transmission, the operator first replaces all spaces with the letter X, before typing the message into the machine. As each key is pressed, a current passes through each rotor, the reflector and back through the rotors in reverse order to produce the cipher-text message. As the message is processed, the first rotor rotates once for every letter that is encoded, the second rotor once every 26 letters, the third rotor every 26 squared letters and so on.
The Enigma is configured by selecting several rotors from a larger set, placing them in a particular order and a particular start position. Received messages are decoded by setting the Enigma to the same state as the encoding Enigma and processing the message again. The result is clear text with the letter X instead of spaces.
For a good description of the Enigma, including some interesting exercises, see Part IV of "The Pleasures of Counting" by T W Korner, Cambridge University Press.
The commercial Enigma typically consists of 3 rotors and a reflector. A Commercial Enigma is provided in the package.
It turns out that the Commercial Enigma is relatively easily cracked by brute force methods (see Korner) and that it can be easily strengthened by the addition of a simple device known as the plugboard. The plugboard is a set of electrical plugs implementing the identity substitution (A -> A, B -> B etc) with a number of pairs of letters - say, 6 - swapped. The plugboard sits between the operator's keyboard and the commercial enigma and it has an impact far in excess of its complexity.
A Military Enigma Machine is included in the package.
For simplicity, this OOEnigma module supplies a number of rotors and a military enigma machine so that the user can do one of:
And the Enigma takes care of the rest. Users who wish to use their own substitution codes or create their own Enigmas should see the documentation for the individual modules.
Enigmas based on an arbitrary number of rotors and using rotors, reflectors and plugboards with any reasonable cipher may be easily constructed. The user creates the required Rotor, Reflector and Plugboard objects then uses them to instantiate either a Military or Commercial Enigma.
Cryptographers talk of the strength of a cryptographic algorithm in term of whether it is computationally feasible to break it. It is, of course, computationally feasible to break an Enigma cipher so don't use it for anything serious!
This package was created in spring 2002 as an exercise in OO Perl and preparing modules properly for CPAN. More importantly, the Enigma is interesting.
CPAN already has a Crypt::Enigma which is not object-oriented and implements only one Enigma (whereas you can create any Enigma-like machine with these objects). Hence the package name Crypt::OOEnigma
The Pleasures of Counting, T W Korner, CUP 1996. A great book for anyone with the slightest interest in mathematics: ISBN 0 521 56087 X hardback ISBN 0 521 56823 4 paperback
S J Baker, Ambriel Consulting, http://ambrielconsulting.com
This package is licenced under the same terms as Perl itself.