Пример #1
0
        private string SteckerSettings()
        {
            string steckerSettings = steckerverbindungen.Text.Replace(" ", "").ToUpper();

            if (steckerSettings == "")
            {
                return(steckerSettings);
            }

            if (!ValidationOfEnigma.BelongAlphabet(steckerSettings))
            {
                MessageBox.Show("Cbvdjks");
                return(null);
            }

            if (steckerSettings.Length > 20)
            {
                MessageBox.Show("10");
                return(null);
            }

            if (steckerSettings.Length % 2 != 0)
            {
                MessageBox.Show("Парне");
                return(null);
            }

            if (ValidationOfEnigma.SymbolsRepeated(steckerSettings))
            {
                MessageBox.Show("Повторення");
                return(null);
            }

            return(steckerSettings);
        }
Пример #2
0
        private void AddReflector(string filename)
        {
            string[] file         = File.ReadAllLines(filename);
            string   errorMessage = "Файл пошкоджено!";

            if (file.Length < 2)
            {
                MessageBox.Show(errorMessage);
                return;
            }
            string wiring = file[1].Replace(" ", "").ToUpper();

            if (wiring.Length != 26)
            {
                return;
            }
            if (!ValidationOfEnigma.BelongAlphabet(wiring))
            {
                MessageBox.Show(errorMessage);
                return;
            }
            if (ValidationOfEnigma.SymbolsRepeated(wiring))
            {
                MessageBox.Show(errorMessage);
                return;
            }
            reflectors.Add(file[0], wiring);
            listReflectors.Items.Add(file[0]);
            reflectorComboBox.Items.Add(file[0]);
        }
Пример #3
0
        private int[] RinngSettings()
        {
            int[] ringSettings;
            if (ringstellung.Text.Replace(" ", "") == "")
            {
                if (fourthRotorOn.Checked)
                {
                    ringstellung.Text = "1 1 1 1";
                    return(new int[] { 1, 1, 1, 1 });
                }
                else
                {
                    ringstellung.Text = "1 1 1";
                    return(new int[] { 1, 1, 1 });
                }
            }
            string errorMessage = "У вікні положення кілець, мають бути числа від 1 до 26 включно!";

            try
            {
                ringSettings = ringstellung.Text.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(n => int.Parse(n)).ToArray();
            }
            catch
            {
                MessageBox.Show(errorMessage);
                return(null);
            }
            if (fourthRotorOn.Checked)
            {
                if (ringSettings.Length < 4)
                {
                    MessageBox.Show("Положення кілець встановлено не для всіх роторів!\r\nНа разі використовується чотири ротори");
                    return(null);
                }
            }
            else
            {
                if (ringSettings.Length < 3)
                {
                    MessageBox.Show("Положення кілець встановлено не для всіх роторів!\r\nНа разі використовується три ротори");
                    return(null);
                }
            }
            if (!ValidationOfEnigma.AllNumbersIntoInterval(ringSettings))
            {
                MessageBox.Show(errorMessage);
                return(null);
            }
            return(ringSettings);
        }
Пример #4
0
 private void EncodeDecode_Click(object sender, EventArgs e)
 {
     if (settings != null)
     {
         List <Rotor> selectedRotors = (List <Rotor>)settings[1];
         try
         {
             selectedRotors[0].Position = Enigma.alphabet.IndexOf(rightRotor.Text.ToUpper()[0]);
             selectedRotors[1].Position = Enigma.alphabet.IndexOf(middleRotor.Text.ToUpper()[0]);
             selectedRotors[2].Position = Enigma.alphabet.IndexOf(leftRotor.Text.ToUpper()[0]);
             if (selectedRotors.Count == 4)
             {
                 selectedRotors[3].Position = Enigma.alphabet.IndexOf(rotor4.Text.ToUpper()[0]);
             }
         }
         catch
         {
             MessageBox.Show("Неправильне введення положення роторів!\r\nПоложення роторів вказуються латинськими літерами від A до Z включно");
             return;
         }
         string inpuText = symbol.Text.ToUpper();
         if (ValidationOfEnigma.BelongAlphabet(inpuText.Replace(" ", "")))
         {
             string cipherText = string.Empty;
             foreach (char s in inpuText)
             {
                 if (s != ' ')
                 {
                     cipherText += Enigma.Encrypt(s, (string)settings[0], selectedRotors, (string)settings[2]).ToString();
                 }
                 else
                 {
                     cipherText += " ";
                 }
             }
             encodedSymbol.Text = cipherText;
         }
         else
         {
             MessageBox.Show("Символи у вхідному тексті мають бути латинськими літерами!");
         }
     }
 }
Пример #5
0
        private void AddRotor(string filename)
        {
            string[] file         = File.ReadAllLines(filename);
            string   errorMessage = "Файл пошкоджено!";

            if (file.Length < 3)
            {
                MessageBox.Show(errorMessage);
                return;
            }
            string wiring = file[1].Replace(" ", "").ToUpper();

            int[] notchs;
            if (wiring.Length == 26 && file[2].Replace(" ", "") != "")
            {
                if (!ValidationOfEnigma.BelongAlphabet(wiring))
                {
                    MessageBox.Show(errorMessage);
                    return;
                }
                if (ValidationOfEnigma.SymbolsRepeated(wiring))
                {
                    MessageBox.Show(errorMessage);
                    return;
                }
                try
                {
                    notchs = file[2].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(n => int.Parse(n)).ToArray();
                }
                catch
                {
                    MessageBox.Show(errorMessage);
                    return;
                }
                if (!ValidationOfEnigma.AllNumbersIntoInterval(notchs))
                {
                    MessageBox.Show(errorMessage);
                    return;
                }

                try
                {
                    if (notchs.Length > 1)
                    {
                        rotors.Add(file[0], new Rotor()
                        {
                            Wiring = wiring, Notch = notchs[0] - 1, Notch2 = notchs[1] - 1
                        });
                    }
                    else
                    {
                        rotors.Add(file[0], new Rotor()
                        {
                            Wiring = wiring, Notch = notchs[0] - 1
                        });
                    }
                }
                catch (ArgumentException)
                {
                    MessageBox.Show("Ротор з таким ключем уже існує!");
                    return;
                }
                listRotors.Items.Add(file[0]);
                rightRotorComboBox.Items.Add(file[0]);
                middleRotorComboBox.Items.Add(file[0]);
                leftRotorComboBox.Items.Add(file[0]);
                rotor4ComboBox.Items.Add(file[0]);
            }
            else
            {
                MessageBox.Show(errorMessage);
            }
        }