Enigma Machine

Submitted on: 1/22/2015 5:09:00 PM
By: Abdulaziz Alfoudari (from psc cd)  
Level: Advanced
User Rating: By 3 Users
Compatibility: VB 4.0 (32-bit), VB 5.0, VB 6.0
Views: 865
     The Enigma machine, created by the German inventor Arthur Scherbius, was used as Germany's system of encryption before and during World War II. This worksheet emulates a version of the Enigma machine.


Visit us at:

The Enigma Machine

The Enigma machine, created by the German inventor Arthur Scherbius, was used as Germany's system of encryption before and during World War II. This worksheet emulates a version of the Enigma machine.

The three main components of the Enigma machine were a plugboard, three scramblers, and a reflector.

The plugboard was used to swap six pairs of letters before they entered the guts of the machine. For example, if I swapped letters 'a' and 'b', and then typed in the letter 'b', it would follow the encryption path originally associated with the letter 'a'.

The scrambler, a thick rubber disk filled with wires, was the most important part of the machine. Each scrambler was essentially a monoalphabetic cipher, which encrypted one plaintext letter to a ciphertext letter. Each letter that was typed into the keyboard of the Enigma, passed through three scramblers, so was encrypted three times. The most important feature of the scramblers was that after each letter was encrypted, they would change their orientation to create a new cipher in the following manner. The first scrambler would rotate 1/26 of a revolution, and after one whole revolution, it would turn the second scrambler 1/26 of a revolution. After one whole revolution of the second scrambler, it would eventually turn the third scrambler one notch. This is a similar to a car odometer, the rotor representing single km has to rotates once completely before the rotor representing 10's of km rotates by one unit. Because of this rotation of three scramblers, the cipher text obtained after typing in the same letter consecutively would only repeat itself after the 17,576th letter.

The last part of the Enigma machine was the reflector, which was a little bit like the scramblers, but it did not rotate, and the wires entered and exited through the same side. So if a letter 'a' goes into the reflector, it might come out as a letter 's', and then get sent back through the three scramblers in reverse order, through the plugboard, and finally show up as the encrypted letter. The reflector allows users to use the Enigma in the same way for both encrypting and decrypting messages.

Because of the changing nature of the machine, sender and receiver must agree on a set of keys, or initial conditions, before communicating with the Enigma. On the actual Enigma machine, the three scramblers could be interchanged, meaning that there were 6 different orders that they could be positioned in. (123, 132, 213, 231, 312, 321) Also, each scrambler could be rotated to any one of 26 initial orientations, yielding 26*26*26 = 17,576 different settings. Also the number of ways of swapping six pairs of letters out of 26 is 100,391,791,500! So the total number of keys available is 6*17,576*100,391,791,500 = 10,586,916,764,424,000!!

> restart;

Given a seed, this will create a scrambler with random internal wiring.

> makeScrambler := proc(seed)
local numGen, i, j, num, scrambler;
scrambler := [];
numGen := rand(1..26);
for i from 1 to 26 do
num := numGen();
for j while member(num, scrambler) do
num := numGen();
end do;
scrambler := [op(scrambler),num];
end do;
end proc:
Given a seed, this will create a reflector with random internal wiring.
> makeReflector := proc(seed)
local num, pos, i, j, reflector, numGen, temp;
reflector := table();
numGen := rand(1..26);
temp := [ entries(reflector) ];
for i while nops(temp) <> 26 do
pos := numGen();
for j while member([pos],temp) do
pos := numGen();
end do;
num := numGen();
for j while member([num],temp) or num = pos do
num := numGen();
end do;
reflector[pos] := num;
reflector[num] := pos;
temp := [ entries(reflector) ];
end do;
for i from 1 to 26 do
temp[i] := temp[i][1];
end do;
end proc:

This will rotate a scrambler passed in by list, by making the n'th letter in the list the first letter.

Ex: list := [1, 2, 3, 4, 5];
list := rotate(list, 3);
[3, 4, 5, 1, 2]
> rotate := proc(list, n)
local i, toReturn, rotateBy;
toReturn := [];
rotateBy := n - 1;
for i from 1 to 26 do
rotateBy := rotateBy + 1;
toReturn := [op(toReturn),list[((rotateBy - 1)mod 26) + 1]];
end do;
end proc:

This will create three scramblers, position them in the order given by scramOrder, arrange them according to orientation, and create a plugboard according to plugSettings. It will then encrypt or decrypt (the processes are the same) message and return the result.

> enigma := proc(message, scramOrder, orientation, plugSettings)
local i, scramblers, scram1, scram2, scram3, letter1, letter2, letter3, plainText,
pair1, pair2, pair3, pair4, pair5, pair6, plugboard, reflector, cipherText, toReturn,
plugIndex, plugEntry;
plainText := StringTools[LowerCase](message);
plainText := convert(plainText, bytes);
scramblers := [ makeScrambler(456), makeScrambler(504), makeScrambler(607) ];
scram1 := scramblers[scramOrder[1]];
scram2 := scramblers[scramOrder[2]];
scram3 := scramblers[scramOrder[3]];
letter1 := convert(StringTools[LowerCase](orientation[1]),bytes)[1] - 96;
letter2 := convert(StringTools[LowerCase](orientation[2]),bytes)[1] - 96;
letter3 := convert(StringTools[LowerCase](orientation[3]),bytes)[1] - 96;
scram1 := rotate(scram1, letter1);
scram2 := rotate(scram2, letter2);
scram3 := rotate(scram3, letter3);
pair1 := convert(StringTools[LowerCase](plugSettings[1]),bytes);
pair2 := convert(StringTools[LowerCase](plugSettings[2]),bytes);
pair3 := convert(StringTools[LowerCase](plugSettings[3]),bytes);
pair4 := convert(StringTools[LowerCase](plugSettings[4]),bytes);
pair5 := convert(StringTools[LowerCase](plugSettings[5]),bytes);
pair6 := convert(StringTools[LowerCase](plugSettings[6]),bytes);
plugboard := table([(pair1[1] - 96) = pair1[2] - 96, (pair2[1] - 96) = pair2[2] - 96,
(pair3[1] - 96) = pair3[2] - 96, (pair4[1] - 96) = pair4[2] - 96,
(pair5[1] - 96) = pair5[2] - 96, (pair6[1] - 96) = pair6[2] - 96]);
plugIndex := [indices(plugboard)];
plugEntry := [entries(plugboard)];
for i from 1 to 6 do
plugIndex[i] := plugIndex[i][1];
plugEntry[i] := plugEntry[i][1];
end do;
reflector := makeReflector(978);
cipherText := encode(plainText, plugIndex, plugEntry, scram1, scram2, scram3, reflector);
toReturn := convert(cipherText, bytes);
end proc:
> encode := proc(plainText, plugIndex, plugEntry, scrm1, scrm2, scrm3, reflector)
local i, scram1, scram2, scram3, pos1, pos2, pos3, letter, response;
scram1 := scrm1; scram2 := scrm2; scram3 := scrm3;
pos1 := 1; pos2 := 1; pos3 := 1;
response := [];
for i from 1 to nops(plainText) do
letter := plainText[i] - 96;
if letter > 0 and letter < 27 then
if member(letter, plugIndex, 'x') then
letter := plugEntry[x];
elif member(letter, plugEntry, 'y') then
letter := plugIndex[y];
end if;
letter := scram1[letter];
letter := scram2[letter];
letter := scram3[letter];
letter := reflector[letter];
member(letter, scram3, 'letter');
member(letter, scram2, 'letter');
member(letter, scram1, 'letter');
if member(letter, plugEntry, 'z') then
letter := plugIndex[z];
elif member(letter, plugIndex, 'w') then
letter := plugEntry[w];
end if;
end if;
scram1 := rotate(scram1, 2);
pos1 := pos1 + 1;
if pos1 > 26 then
pos1 := 1;
scram2 := rotate(scram2, 2);
pos2 := pos2 + 1;
if pos2 > 26 then
pos2 := 1;
scram3 := rotate(scram3, 2);
pos3 := pos3 + 1;
if pos3 > 26 then
pos3 := 1;
end if;
end if;
end if;
response := [op(response), letter + 96];
end do;
end proc:

> message := "There is one more feature of Scherbius's design, known as the ring, which has not yet been mentioned. Although the ring does have some effect on encryption, it is the least significant part of the whole Enigma machine.";

> code := enigma(message, [2,3,1], [c,b,t],[tg,yh,rf,ep,av,bn]);

> result := enigma(code, [2,3,1], [c,b,t],[tg,yh,rf,ep,av,bn]);

The actual Enigma machine did not have upper and lower case letters, and did not have any spaces or punctuation, so this worksheet will always output lower case letters and ignore spaces and puntuation.
If we try to decrypt the message with slightly different initial conditions, the result will be gibberish.

> result2 := enigma(code, [2,3,1], [a,b,t], [tg,yh,rf,ep,av,bn]);

Note: This article was taken from:
Sylvain Muise

Other 2 submission(s) by this author


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Advanced category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)

Other User Comments

 There are no comments on this submission.

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)

To post feedback, first please login.