Introduction

This article shows how to implement an Enigma like cryptography using C#. This kind of cryptography system was used until the 70's. The most famous use of it was by the German army during the WWII. The breaking of this code led the allies to victory. This cryptography method can still be secure enough for use in non critical systems. If the Germans would have used the method with care, maybe the allies would not have discovered their secrets.

Background

The reader might be interested in some general information about Enigma. These links could help you:

Using the code

This code is a complete program that shows the inner workings of such a machine. If the user wants to understand more about the Enigma machine, then he/she would need to Google for more information. This code shows how simple it is to emulate such complicate wirings as those of the Enigma machine, using a modern programming language.

The machine has at least three rotors, each one having the alphabet written on it. The rotors has a visible part which consists of the alphabet and a part that has other letters which are linked to the first one by wires. The three rotors are arranged one after the other from right to left. The rotor is not static.

This kind of machine was a combination of electrical and mechanical cryptography system. Basically, when the user of the machine pressed a key, the current would go to the first rotor to the letter corresponding to that key (let's say 'A'). The wirings of the wheel would lead to an output letter from the first rotor with a different value, let's say 'D', this impulse would then go to the second rotor, and the same thing would happen there, and in the third rotor. From there, the impulse would go to a last wheel - that is named the 'reflector'. This wheel (rotor) is the only non rotating wheel. The impulse would go to this wheel and from here it would return to the third rotor, and make its way backwards. From the first rotor, the current would illuminate a LED under a panel. This would be the encrypted letter.

This is not all. At each key press, the first rotor would rotate with one position so that the output letter would not be the same when you press the same key twice. The second rotor would rotate one position once at every 28 rotations of the first rotor, and the third one once every 28 rotations of the second rotor. As I've said earlier, the reflector is the only static rotor. On some models, the second and third rotor would rotate more than once at every 28 rotations of the previous rotor.

To decrypt the data, you just had to set the rotors at the exact initial position as the rotors on the machine that produced that output and then type in the encrypted data.

There were several rotors and they could be changed. To complicate things even more, the military version had a front panel with letters were you could interconnect two letters so that each time a letter would occur, it would be replaced with the other one. To show this kind of complicated inner workings, the following C# code would suffice:

You can see that we only use the upper letters. That's because the existence of punctuation and spaces would help in the decryption of the message.

I've also created a rotor class which practically represents one rotor of the enigma machine. In this class, we have the methods Move which moves the rotor one position and the PutDataIn which emulates the sending of the electric signal to the first rotor. As you can see, we only send the data to the first rotor, which will send the data further down the chain.

Points of Interest

Although it seems peculiar, the older the cryptographic algorithm, the safer it is. This is because it means that people have tested it and they did not found any way in. Maybe in a later article, I will show you how to implement an instant messenger that uses this cryptography technique.

This enigma machine simulation using an Arduino Uno and a Touch Screen LCD focuses on an accurate implementation of the three wheel Enigma I and Enigma M3 and the four wheel Enigma M4. The double stepping anomaly is correctly implemented as well. The plugboard can be left empty, set up with the standard 10 plugs or up to 13 plugs can be used. When 10 plugs are used, the Uhr switch, a device that was invented to further scramble the plugboard settings can be used.

The original enigma had a 3 moving rotors system. Some military enigma had a 3 moving and 1 or 2 fixed rotors system.
But there's one thing missing for the military enigma : there's a permutation fixed table before and after the rotor system.
By default, an input of A is A. but it can be converted to B (for example). In this case, if you type A, it's transform to B before beeing inputed in the rotors encryption. If you type B, it's transformed to A.
The exact same permutation is used after the rotors output. If the rotors generates a A, it's then transformed into B and B is transformed into A.
Each letter can be permutted or not with another.
This system keeps the flaw of not outputing an input letter in the output. For example, if you type K 26 times you won't obtain any K in the output.
May I suggest you to implement the military enigma ?

The bug prevent from selecting back the reflector B after the reflector C has been selected.
at the end of the source code of Settings.cs the line :
"
if(cmbReflector.SelectedIndex>-0){
mf.SetReflector(cmbReflector.SelectedItem.ToString().Substring(cmbReflector.SelectedItem.ToString().LastIndexOf("=")+2).Trim());
}
"
The minus of "if(cmbReflector.SelectedIndex>-0)" must be replace by equals " if(cmbReflector.SelectedIndex>=0) "

Being a WWII buff can't help to comment on the article's introduction. The Germans did use Enigma very carefully, what they never knew was that the British recovered one of the machines intact from a captured German U-Boat. With that machine and the use of the first computer employed for military purposes (MARK II)and the genius chrystographers at Betchley Park it was possible to decipher first the German navy code and later on the Luftwaffe's.

hehehe British just made better version of Bomba_(cryptography)[^]
"Up to July 25, 1939, the Poles had been breaking Enigma messages for over six and a half years"
edit:
don't forget about French Military Intelligence ... their bought "using of enigma instruction" from Hans-Thilo Schmidt[^] and provide it to Poles in 1932

When rotor V steps from Z to A the second rotor does'nt step.
I tried to change in the source with another letter in the al3.Add("Z"). It works the problem seems to come from the letter Z in the incremental part somewhere or in the rotor class.
If someone can find from where the problem comes from it would be great .

yahoo, I found one solution I don't know if it's the better but it works. I have only change the two 65 of the line " if(next!=null && (offset+65) == ((notchPos-65)%26)+66){next.Move();}" in 66 and 64 and know it work's.

A double step occurs as follows: The first (right) rotor is in its notch position, and when a key is depressed it will step and push the second (middle) rotor one step further. If the second rotor has moved by this step into its own notch position, the third pawl can move on the next step into the ratchet of the third (left) rotor. With this next step, the third pawl pushes the ratchet of the third (left) rotor and advances it, but will also push into the second rotor's own notch, advancing the second rotor a second time in a row.

So I have done a lot of searching online, downloaded many enigma emulators and for the LIFE (noticed how captialized life) of me cannot figure out how to use any of them.

I am in deperate need of help. To asnwer your question, yes I am asking you/sopmeone to do an assignment for me. It is one question I am faced with and I will never have to do another one as long as I live. Here is the quesiton that has been possed to me:

I do not know whether in Enigma we have the mapping table for each rotor fixed or not. If the mappings are fixed and only the selection of three from five rotors is done by user, so what are the mapping tables or atleast where can I find them.

I don't know if I understand your question, but here is an answer.
The rotor configuration was fixed. The rotors were in a box and the user would have to choose the three rotors to put in the machine. The possible configurations of the rotors for this version of enigma are the ones you can find in the program.
By the term of "mapping" I meant the way a letter was transfered from a rotor to another one. So for example letter "a" would go from rotor I to rotor II and there it would result the letter "b" so we can tell that "a" was mapped to "b". The way this mapping is done is through a complex mechanism which was transposed in an algorithm in my code. (please note that if you move any of the rotors to another possition, you would have another mapping).
There are no preestablished mappings of the letters.
If you look at the link I've provided in my article you can find more information about Enigma and the way the letters were transformed.

Dear Adi,
Thanks for your quick reply.
Your description of mapping was exactly the same as I had meant, but I want to know if this mapping tables for differenet versions of Enigma are explicitly available, or every one have the choice to select an implicit mapping for its own implemenation. If we are limited in selection of mapping tables so what the limitations are, otherwise the question is whether this space of mapping tables increases the key space (in order to attack) or not.

Well,
The germans had the five rotors (initially) (from which they could choose three to use. The five rotors had the same configuration for all the machines.
When they put the rotors in the machine, they could choose any combination and order of the rotors. This could give them lots of mapping possibilities.
The was no rotor order and starting possitions. The only thing was that they had to transmit the exact order of the rotors to the person deciphering the message.
I don't know if you can create custom rotors (different letter order - mapping) and get the same results as with the ones used by the germans.
This could be a good exercise to try.
(The advanced versions of enigma could use more rotors,but they were however limited by the fact that they had to be able to cript messages that could be decripted by older machines.)
Whenever you set a different starting possition for any rotor you have a different mapping.
The explication below is only valid for the version of enigma I've presented in my article.
The mapping only depends on the order of the letters on the rotor, the wirings inside the rotor and between rotors(offset) and rotor starting possitions.
The wirrings inside the rotors are fixed. You only had exact mappings you could use (as far as I know). The wirrings between rotors depended on the possition of each rotor, but you could not change the offset of the wirring.
In the links I provided you also have a calculation of the possibilities of configuration
HTH
Adi

Hi,
You are right. Although I remember putting the resource files in the sources and checking them to be sure they are there, they seem to be missing now.
I will update the sources as soon as possible.
Thanks
Adi

The project was created in SharpDevelop so, if you have SharpDevelop you should be able to run it from the .prjx file. The images are stored in the .resource files.
Anyway, I've also submited to codeproject another sourcecode file which should replace the present one within a mather of days.
This file contains the image file, as well as a visual studio version of this project.

adi_clepcea wrote:Although it seems peculiar, the older the cryptographic algorithm, the safer it is. This is because it means that people have tested it and they did not found any way in.

As fas as I know, the Enigma algorithm has exploitable flaws. First, one particular cleartext letter can never be encrypted as itself. Second, two consecutive occurences of the same cleartext letter are always different ciphertext letters. Third, cleartext position maps 1:1 to ciphertext position.

Enigma's power arose from the fact that it was a secret algorithm using secret keys. After capturing an Enigma machine (and thus revealing the algorithm) above flaws could be found and exploited. And the relation to the famous "weather codebook" provided a back door to reveal the secret keys.

You're absolutely right. Enigma cipher can and was broken.
It was broken before the war by the polish secret services and afterwards theys knowledge was used to broke othe messages.
Still. The power of enigma was real since from milions of messages transmited during the war, the allies only decripted about 5000 as far as I know. Maybe the calculation of that times (the Bomb) was not enough.
The weak points you've mentioned about do exist. Thank you for your comment.

It was clasified that the British had cracked enigma, so after 45, the British and Americans spread disinformation that it was Enigma was uncrackable and sold it to other countries, knowing that they would be able to read everything.