Beispiel #1
0
 private void Enigma_Load(object sender, EventArgs e)
 {
     ofd.Filter = "Arquivos de Text (.txt)| *.txt";
     folderBrowserDialog1.Description =
         "Selecione o diretório onde deseja salvar o arquivo criptografado";
     encryption = new EnigmaMachine();
 }
Beispiel #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);
        }
Beispiel #3
0
        static void Main(string[] args)
        {
            EnigmaMachine em = new EnigmaMachine();

            em.Run();
            Bombe b = new Bombe();

            b.Run();
        }
Beispiel #4
0
        public static void Main(string[] args)
        {
            EnigmaMachine machine = new EnigmaMachine(EnigmaMachine.RotorI, EnigmaMachine.RotorII, EnigmaMachine.RotorIII);

            machine.SetCode(1, 1, 1);
            while (true)
            {
                Console.WriteLine(machine.ToString());
                Console.WriteLine(machine.ProcessString(Console.ReadLine()));
            }
        }
Beispiel #5
0
        private void uncipherButton_Click(object sender, EventArgs e)
        {
            int    firstRotorOffset, secondRotorOffset, thirdRotorOffset;
            int    firstRotorNumber, secondRotorNumber, thirdRotorNumber;
            int    reflectorNumber;
            string commutatorsString;

            try
            {
                firstRotorNumber = (int)firstRotorPicker.Value;
                firstRotorOffset = (int)firstRotorPositionInput.Value;

                secondRotorNumber = (int)secondRotorPicker.Value;
                secondRotorOffset = (int)secondRotorPositionInput.Value;

                thirdRotorNumber = (int)thirdRotorPicker.Value;
                thirdRotorOffset = (int)thirdRotorPositionInput.Value;

                reflectorNumber = (int)reflectorPicker.Value;

                commutatorsString = commutatorInput.Text;
            }
            catch (InvalidCastException exception)
            {
                MessageBox.Show("You input bad values in the settings!");
                return;
            }
            catch (Exception exception)
            {
                MessageBox.Show($"Internal error: {exception.Message}");
                return;
            }

            EnigmaMachine uncipherer = new EnigmaMachine(
                (Rotors)firstRotorNumber,
                firstRotorOffset,
                (Rotors)secondRotorNumber,
                secondRotorOffset,
                (Rotors)thirdRotorNumber,
                thirdRotorOffset,
                (Reflectors)reflectorNumber,
                commutatorsString
                );

            uncipheredText.Text = uncipherer.UncipherText(cipheredText.Text);
        }
Beispiel #6
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();
        }
Beispiel #7
0
        static void Main(string[] args)
        {
            Console.WriteLine("I have eight scramblers. Please indicate the ones you would like to use, in order (e.g. '4,1,8'): ");
            var scramblerIndices = Console.ReadLine().Replace(" ", "").Split(',').Select(s => int.Parse(s) - 1);

            while (scramblerIndices.Any(s => s < 0 || s > 7))
            {
                Console.WriteLine("Please only enter numbers from 1 to 8.");
                Console.WriteLine("I have eight scramblers. Please indicate the ones you would like to use, in order (e.g. '4,1,8'): ");
                scramblerIndices = Console.ReadLine().Replace(" ", "").Split(',').Select(s => int.Parse(s) - 1);
            }

            var scramblers = new List <Scrambler>();

            foreach (var index in scramblerIndices)
            {
                scramblers.Add(scramblerPile[index]);
            }

            var enigmaMachine = new EnigmaMachine(scramblers.ToArray(), reflector, plugboard);

            Console.WriteLine("Please enter your message:");
            var message = Console.ReadLine();

            while (message != "Q" && message != "q")
            {
                try
                {
                    var cryptedMessage = enigmaMachine.Crypt(message);
                    Console.WriteLine("\r\n\r\nThe crypted message is: \r\n" + cryptedMessage);
                    Console.WriteLine("\r\nEnter another message, or 'Q' to quit:");
                    message = Console.ReadLine();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    Console.ReadKey();
                    return;
                }
            }
        }
Beispiel #8
0
        public void GivenOneRotorAndReflectorWhenEncodingSingleCharacter()
        {
            _inputCharacter = 'P';

            _indexOfInputCharacter = 123;
            _character             = 'W';
            _mockAlphabet          = new Mock <IAlphabet>();
            _mockAlphabet.Setup(alphabet => alphabet.IndexOf(It.IsAny <char>())).Returns(_indexOfInputCharacter);
            _mockAlphabet.Setup(alphabet => alphabet.CharacterAt(It.IsAny <int>())).Returns(_character);

            _rightRotorEncodedOffset  = 321;
            _rightRotorReturnedOffset = 12345;

            _rightRotor = new Mock <IRotor>();
            _rightRotor.Setup(x => x.EncodeRightToLeft(It.IsAny <int>())).Returns(_rightRotorEncodedOffset);
            _rightRotor.Setup(x => x.EncodeLeftToRight(It.IsAny <int>())).Returns(_rightRotorReturnedOffset);

            _centerRotorEncodedOffset  = 324361;
            _centerRotorReturnedOffset = 1234645;

            _centerRotor = new Mock <IRotor>();
            _centerRotor.Setup(x => x.EncodeRightToLeft(It.IsAny <int>())).Returns(_centerRotorEncodedOffset);
            _centerRotor.Setup(x => x.EncodeLeftToRight(It.IsAny <int>())).Returns(_centerRotorReturnedOffset);

            _leftRotorEncodedOffset  = 8;
            _leftRotorReturnedOffset = 3;

            _leftRotor = new Mock <IRotor>();
            _leftRotor.Setup(x => x.EncodeRightToLeft(It.IsAny <int>())).Returns(_leftRotorEncodedOffset);
            _leftRotor.Setup(x => x.EncodeLeftToRight(It.IsAny <int>())).Returns(_leftRotorReturnedOffset);

            _reflectedOffset = 11;
            _mockReflector   = new Mock <IReflector>();
            _mockReflector.Setup(reflector => reflector.Reflect(It.IsAny <int>())).Returns(_reflectedOffset);

            var enigmaMachine = new EnigmaMachine(_mockAlphabet.Object, _leftRotor.Object, _centerRotor.Object, _rightRotor.Object, _mockReflector.Object);

            _result = enigmaMachine.Encode(_inputCharacter);
        }
Beispiel #9
0
 private void Form1_Load(object sender, EventArgs e)
 {
     enigmaMachine = new EnigmaMachine();
     EventCenter.AddListener <string, int[]>(EventType.RotorChange, UpdateRotor);
     EventCenter.AddListener <int[]>(EventType.ReflectorChange, UpdateRelector);
 }
        public void GivenOneRotorAndReflectorWhenEncodingSingleCharacter()
        {
            _inputCharacter = 'P';

            _indexOfInputCharacter = 123;
            _character = 'W';
            _mockAlphabet = new Mock<IAlphabet>();
            _mockAlphabet.Setup(alphabet => alphabet.IndexOf(It.IsAny<char>())).Returns(_indexOfInputCharacter);
            _mockAlphabet.Setup(alphabet => alphabet.CharacterAt(It.IsAny<int>())).Returns(_character);

            _rightRotorEncodedOffset = 321;
            _rightRotorReturnedOffset = 12345;

            _rightRotor = new Mock<IRotor>();
            _rightRotor.Setup(x => x.EncodeRightToLeft(It.IsAny<int>())).Returns(_rightRotorEncodedOffset);
            _rightRotor.Setup(x => x.EncodeLeftToRight(It.IsAny<int>())).Returns(_rightRotorReturnedOffset);

            _centerRotorEncodedOffset = 324361;
            _centerRotorReturnedOffset = 1234645;

            _centerRotor = new Mock<IRotor>();
            _centerRotor.Setup(x => x.EncodeRightToLeft(It.IsAny<int>())).Returns(_centerRotorEncodedOffset);
            _centerRotor.Setup(x => x.EncodeLeftToRight(It.IsAny<int>())).Returns(_centerRotorReturnedOffset);

            _leftRotorEncodedOffset = 8;
            _leftRotorReturnedOffset = 3;

            _leftRotor = new Mock<IRotor>();
            _leftRotor.Setup(x => x.EncodeRightToLeft(It.IsAny<int>())).Returns(_leftRotorEncodedOffset);
            _leftRotor.Setup(x => x.EncodeLeftToRight(It.IsAny<int>())).Returns(_leftRotorReturnedOffset);
            
            _reflectedOffset = 11;
            _mockReflector = new Mock<IReflector>();
            _mockReflector.Setup(reflector => reflector.Reflect(It.IsAny<int>())).Returns(_reflectedOffset);

            var enigmaMachine = new EnigmaMachine(_mockAlphabet.Object, _leftRotor.Object, _centerRotor.Object, _rightRotor.Object, _mockReflector.Object);
            _result = enigmaMachine.Encode(_inputCharacter);
        }