예제 #1
0
 private void RepeatKey(MyEnigmaMachine machine, char p1, int p2)
 {
     for (int i = 0; i < p2; i++)
     {
         machine.PressKey(p1);
     }
 }
예제 #2
0
        public void TestLowercaseEncryption()
        {
            var    machine = new MyEnigmaMachine();
            string cypher  = machine.Encrypt("This is a test; Another test. Hello, World!");

            Assert.AreEqual("Zpjj sv s pgbw; Wwiukog fxym. Lprfq, Zpvkw!", cypher);
        }
예제 #3
0
        public void TestNonCharEncryption()
        {
            var    machine = new MyEnigmaMachine();
            string cypher  = machine.Encrypt("THIS IS A TEST; ANOTHER TEST. HELLO, WORLD!");

            Assert.AreEqual("ZPJJ SV S PGBW; WWIUKOG FXYM. LPRFQ, ZPVKW!", cypher);
        }
예제 #4
0
        public void TestDefaultSettings()
        {
            var machine = new MyEnigmaMachine();

            char[] ringLetters = machine.GetCurrentRotorRingLetters();
            Assert.AreEqual('A', ringLetters[2]);
            Assert.AreEqual('A', ringLetters[1]);
            Assert.AreEqual('A', ringLetters[0]);
        }
예제 #5
0
        public void TestFullParameters()
        {
            var machine = new MyEnigmaMachine();

            machine.SetupRotors(new[] { new RotorInfo("IV", 'M', 'F'), new RotorInfo("V", 'C', 'U'), new RotorInfo("II", 'W', 'T') });
            machine.SetupPlugboard("ABCKEFGHIJDLNMOPQRSTUZWXYV");
            string cypher = machine.Encrypt("This is a test");

            Assert.AreEqual("Rmxe li c wmua", cypher);
        }
예제 #6
0
        public void TestRotorsDoubleStepping()
        {
            var machine = new MyEnigmaMachine();

            machine.SetCurrentRotorRingLetters(new [] { 'A', 'D', 'O' });
            RepeatKey(machine, 'A', 6);
            char[] ringLetters = machine.GetCurrentRotorRingLetters();
            Assert.AreEqual('B', ringLetters[0]);
            Assert.AreEqual('F', ringLetters[1]);
            Assert.AreEqual('U', ringLetters[2]);
        }
예제 #7
0
        public void TestRotors()
        {
            var machine = new MyEnigmaMachine();

            RepeatKey(machine, 'A', 26);
            char[] ringLetters = machine.GetCurrentRotorRingLetters();
            Assert.AreEqual('A', ringLetters[2]);
            Assert.AreEqual('B', ringLetters[1]);
            RepeatKey(machine, 'A', 25 * 26);
            ringLetters = machine.GetCurrentRotorRingLetters();
            Assert.AreEqual('B', ringLetters[0]);
        }
예제 #8
0
        public void TestEncryptionDefaultSettings()
        {
            var    machine = new MyEnigmaMachine();
            string cypher  = machine.Encrypt("ABCDEFGHIJKLMNOPQRSTUVWXYZ");

            Assert.AreEqual("FUVEPUMWARVQKEFGHGDIJFMFXI", cypher);
            machine.ResetRotors();
            cypher = machine.Encrypt("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
            Assert.AreEqual("FUVEPUMWARVQKEFGHGDIJFMFXI", cypher);
            machine.ResetRotors();
            cypher = machine.Encrypt("FUVEPUMWARVQKEFGHGDIJFMFXI");
            Assert.AreEqual("ABCDEFGHIJKLMNOPQRSTUVWXYZ", cypher);
        }
예제 #9
0
        public void TestFullCycle()
        {
            var machine = new MyEnigmaMachine();
            int count   = 0;

            do
            {
                machine.PressKey('A');
                count++;
            } while (machine.GetCurrentRotorRingLetters()[0] != 'A' || machine.GetCurrentRotorRingLetters()[1] != 'A' || machine.GetCurrentRotorRingLetters()[2] != 'A');

            Assert.AreEqual(26 * 26 * 25, count);
        }
예제 #10
0
        public void TestEncryptionRingStartingLettertSettings()
        {
            var machine = new MyEnigmaMachine();

            machine.SetCurrentRotorRingLetters(new[] { 'F', 'R', 'Q' });
            string cypher = machine.Encrypt("ABCDEFGHIJKLMNOPQRSTUVWXYZ");

            Assert.AreEqual("MHHKTNIROWJNYMNWKHMVEZQHWU", cypher);
            machine.ResetRotors();
            cypher = machine.Encrypt("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
            Assert.AreEqual("MHHKTNIROWJNYMNWKHMVEZQHWU", cypher);
            machine.ResetRotors();
            cypher = machine.Encrypt("MHHKTNIROWJNYMNWKHMVEZQHWU");
            Assert.AreEqual("ABCDEFGHIJKLMNOPQRSTUVWXYZ", cypher);
        }
예제 #11
0
        public void TestEncryptionRingOffsetSettings()
        {
            var machine = new MyEnigmaMachine();

            machine.SetupRotors(new[] { new RotorInfo("I", 'A', 'B'), new RotorInfo("II", 'A', 'B'), new RotorInfo("III", 'A', 'B') });

            string cypher = machine.Encrypt("AAAAA");

            Assert.AreEqual("EWTYX", cypher);
            machine.ResetRotors();
            cypher = machine.Encrypt("AAAAA");
            Assert.AreEqual("EWTYX", cypher);
            machine.ResetRotors();
            cypher = machine.Encrypt("EWTYX");
            Assert.AreEqual("AAAAA", cypher);
        }