Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey guys! With some great help from someone I met on the internet, I was able to programm an Enigma in C#. But only it's three rotors and the reflector. When I type "TEST" , "OLPF" comes out. All rotors are set on A-A-A. Here's the code:

C#
namespace Enigma
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        // in this part I declare the variables
        // int declares a number, string a letter
        int n = 0; 
        int i = 0;
        int ii = 0;
        int iii = 0;
        int notch3 = 22; // if the first rotor reaches the 22. letter, the second rotor will turn one time
        int notch2 = 5; // if the second rotor reaches the 5. letter, the third will turn one time
        string alpha = "ABCDEFGHJIKLMNOPQRSTUVWXYZ"; 
                                                     wird sondern nur ABCDEFGH....Z
        public static string rotor1 = "EKMFLGDQVZNTOWYHXUSPAIBRCJ"; // Rotor III
        public static string rotor2 = "AJDKSIRUXBLHWTMCQGZNPYFVOE"; // Rotor II
        public static string rotor3 = "BDFHJLCPRTXVZNYEIWGAKMUSQO"; // Rotor I
        public static string reflector1 = "YRUHQSLDPXNGOKMIEBFZCWVJAT"; // Reflektor

 
       // with the numericUpDown elements, you can configurate the position of the three rotors. (in my Case 1-1-1)
        private void numericUpDown1_ValueChanged_1(object sender, EventArgs e)
        {
            label1.Text = alpha.Substring((int)numericUpDown1.Value - 1, 1); //a
        }                                                                   
 
        private void numericUpDown2_ValueChanged_1(object sender, EventArgs e)
        {
            label2.Text = alpha.Substring((int)numericUpDown2.Value - 1, 1);
        }
 
        private void numericUpDown3_ValueChanged_1(object sender, EventArgs e)
        {
            label3.Text = alpha.Substring((int)numericUpDown3.Value - 1, 1);
        }
 
        // tbxInput ist das Fenster, in dem man den Klartext eingibt. Der eingegebene Text 
        //wird durch den Algorithmus, den ich "Encrypt" genannt habe verschlüsselt.
        private void tbxInput_TextChanged_1(object sender, EventArgs e)
        {
 
            Encrypt();
        
        }
 
 

        private void tbxOutput_TextChanged(object sender, EventArgs e)
        {
            
        }
 
        // thats the main part. Encryp() contains the algorithm which encrypts the letters. also here should be the solution for my question but I don't get it....
        private void Encrypt()
        {
 
 
            // declare variables
            string sInput = tbxInput.Text.Substring(tbxInput.Text.Length - 1, 1);
            string sOutput = sInput.ToUpper();
 
          
            i = Int32.Parse(numericUpDown1.Value.ToString()) - 1;  
            ii = Int32.Parse(numericUpDown2.Value.ToString()) - 1; 
            iii = Int32.Parse(numericUpDown3.Value.ToString()) - 1;
 
 
            if (sInput == " ")
            {
                sOutput = " ";
                tbxOutput.Text += " ";
                return;
            }
 
            Increment_Rotors();
                               
 
            n = alpha.IndexOf(sOutput) + iii;  
            validateN();
            sOutput = rotor3.Substring(n, 1); 
 
            n = alpha.IndexOf(sOutput) + ii - iii;
            validateN();
            sOutput = rotor2.Substring(n, 1);  
            n = alpha.IndexOf(sOutput) + i - ii; 
            validateN();
            sOutput = rotor1.Substring(n, 1); 
 
            n = alpha.IndexOf(sOutput);
            validateN();
 
            n = alpha.IndexOf(sOutput) - i;
            validateN();
 
 
           // here comes the reflector
            sOutput = reflector1.Substring(n, 1); 
 
            n = alpha.IndexOf(sOutput) + i;
            validateN();
            sOutput = alpha.Substring(n, 1);
            n = rotor1.IndexOf(sOutput) - i;
            validateN();
            sOutput = alpha.Substring(n, 1);
            
            n = alpha.IndexOf(sOutput) + ii;
            validateN();
            sOutput = alpha.Substring(n, 1);
 
            n = rotor2.IndexOf(sOutput) - ii;
            validateN();
            sOutput = alpha.Substring(n, 1);
            
            n = alpha.IndexOf(sOutput) + iii;
            validateN();
            sOutput = alpha.Substring(n, 1);
 
            n = rotor3.IndexOf(sOutput) - iii;
            validateN();
            sOutput = alpha.Substring(n, 1);
 
            numericUpDown3.Value = iii + 1;    
            numericUpDown2.Value = ii + 1;
            numericUpDown1.Value = i + 1;
            tbxOutput.Text += sOutput;
        }
 
      
        private void validateN()
        {
            if (n < 0)
                n = n + 26;
            if (n > 25)
                n = n - 26;
            if (n < 0 || n > 25)
                validateN();
        }
 
       
        private void Increment_Rotors() 
        {
            iii++;
            if (iii > 25)
                iii = iii - 26;
 
            if (ii == (notch2 - 1))
            {
                i++;
                ii++;
            }
            else
            {
 
                if (iii == notch3)
                {
                    ii++;
                }
            }
            if (ii > 25)
                ii = ii - 26;
 
            if (i > 25)
                i = i - 26;
        }


My question: Why "OLPF" if I type "TEST"? Shouldn't it be "OWPF"?

Have a nice day! :)
Posted
Updated 5-Dec-11 0:07am
v2
Comments
#realJSOP 5-Dec-11 5:39am    
Your comments aren't in English, so we don't know what the hell you're doing.
JoeilG 5-Dec-11 6:58am    
I already changed it
Nickos_me 5-Dec-11 6:56am    
i = Int32.Parse(numericUpDown1.Value.ToString()) - 1;
ii = Int32.Parse(numericUpDown2.Value.ToString()) - 1;
iii = Int32.Parse(numericUpDown3.Value.ToString()) - 1;
Hm..interesting code style :(

1 solution

This is an English Language Site.
All the comments in code are not much use to non-German speakers.

May I suggest a few minutes of your time translating them may be useful.
 
Share this answer
 
Comments
JoeilG 5-Dec-11 5:36am    
Sure! Refresh the site in about 5-8min :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900