Пример #1
0
        private void btnEncrypt_Click(object sender, EventArgs e)
        {
            if (inputString == "" || inputString == null)
            {
                MessageBox.Show("Favor selecionar um arquivo de entrada preenchido primeiro.", "Preencher arquivo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            encryptedText = encryption.Encrypt(new String(inputString.Where(Char.IsLetter).ToArray()).ToUpper());

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

            DialogResult result = folderBrowserDialog1.ShowDialog();
            string       folderName = "", filePath = "";

            if (result == DialogResult.OK)
            {
                folderName = folderBrowserDialog1.SelectedPath;
            }

            filePath = folderName + "\\" + txtName.Text.ToString() + ".txt";
            try
            {
                System.IO.File.WriteAllText(filePath, encryptedText);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Favor selecionar um arquivo.", "Preencher arquivo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            txtOutput.Text = encryptedText;
        }
Пример #2
0
        public bool StartEnigmaAt(int r1, int r2, int r3, string stringToCheck, string crib)
        {
            EnigmaMachine em = new EnigmaMachine();

            em.SetRotorValues(r1, r2, r3);
            bool cribFound = false;

            for (int i = 0; i < stringToCheck.Length; i++)
            {
                string stringChar    = stringToCheck.ElementAt(i).ToString();
                string cribChar      = crib.ElementAt(i).ToString();
                string encryptedChar = em.Encrypt(stringChar);

                if (!encryptedChar.Equals(cribChar))
                {
                    //Console.WriteLine("Skip, wrong params");
                    break;
                }
                cribFound = stringToCheck.Length == i + 1;
            }

            if (cribFound)
            {
                Console.WriteLine("Found " + r1 + " " + r2 + " " + r3);
                return(true);
            }

            //Console.WriteLine("Not found ");
            return(false);
        }
Пример #3
0
        private static void Main(string[] args)
        {
            //initialize
            var machine = new EnigmaMachine(Constants.ReflectorA, Constants.RotorEnigma1, Constants.RotorEnigma2, Constants.RotorEnigma3);

            //encrypt
            machine.TurnTo(5, 10, 15);
            string encrypted = machine.Encrypt(args.Length == 1 ? args[0] : "teststring");

            Console.WriteLine("Encrypted: " + encrypted);

            //decrypt
            machine.TurnTo(5, 10, 15);  //since the rotors have turned during the encryption, we have to turn them back
            string decrypted = machine.Encrypt(encrypted);

            Console.WriteLine("Decrypted: " + decrypted);

            //wait for user
            Console.Read();
        }