Ejemplo n.º 1
0
        private static NestedEnigmaMachine BuildEnigma(char[] ringSettings = null)
        {
            if (ringSettings == null)
            {
                ringSettings = new char[] { 'A', 'A', 'A' };
            }
            NestedRotor rotorI = NestedEnigmaMachineTestSupport.CreateRotorI();

            rotorI.SetRingSetting(ringSettings[0]);
            NestedRotor rotorII = NestedEnigmaMachineTestSupport.CreateRotorII();

            rotorII.SetRingSetting(ringSettings[1]);
            NestedRotor rotorIII = NestedEnigmaMachineTestSupport.CreateRotorIII();

            rotorIII.SetRingSetting(ringSettings[2]);
            EnigmaReflector     reflectorB = NestedEnigmaMachineTestSupport.CreateBReflector();
            NestedEnigmaMachine enigma     = new NestedEnigmaMachine();

            EnigmaRotor[] rotors = new EnigmaRotor[4];
            rotors[0] = reflectorB;
            rotors[1] = rotorI;
            rotors[2] = rotorII;
            rotors[3] = rotorIII;

            enigma.SetRotors(rotors);
            return(enigma);
        }
Ejemplo n.º 2
0
        private static NestedRotor CreateMultiRotorEnigmaMachine(string key)
        {
            NestedRotor rotorI = new NestedRotor("EKMFLGDQVZNTOWYHXUSPAIBRCJ");

            rotorI.SetOffset(key[0]);
            rotorI.SetTurnover('Q');
            NestedRotor rotorII = new NestedRotor("AJDKSIRUXBLHWTMCQGZNPYFVOE");

            rotorII.SetOffset(key[1]);
            rotorII.SetTurnover('E');
            NestedRotor rotorIII = new NestedRotor("BDFHJLCPRTXVZNYEIWGAKMUSQO");

            rotorIII.SetOffset(key[2]);
            rotorIII.SetTurnover('V');

            EnigmaReflector BReflector = new EnigmaReflector()
            {
                Mapping = "YRUHQSLDPXNGOKMIEBFZCWVJAT"
            };

            rotorIII.Drives = rotorII;
            rotorII.Drives  = rotorI;
            rotorI.Drives   = BReflector;
            return(rotorIII);
        }
Ejemplo n.º 3
0
        public void CanSetRotors()
        {
            EnigmaI enigma = new EnigmaI();

            EnigmaRotor[] rotors = new EnigmaRotor[4];
            rotors[0] = new EnigmaReflector();
            enigma.SetRotors(rotors);
        }
Ejemplo n.º 4
0
        public void SetRotorsEnforcesRotorCount()
        {
            EnigmaI enigma = new EnigmaI();

            EnigmaRotor[] rotors = new EnigmaRotor[3];
            rotors[0] = new EnigmaReflector();
            enigma.SetRotors(rotors);
        }
Ejemplo n.º 5
0
        public EnigmaRotor GetReflector(Reflector reflectorKey)
        {
            EnigmaRotor returnValue;

            switch (reflectorKey)
            {
            case Reflector.A:
                returnValue = new EnigmaReflector()
                {
                    Mapping = "EJMZALYXVBWFCRQUONTSPIKHGD"
                };
                break;

            case Reflector.B:
                returnValue = new EnigmaReflector()
                {
                    Mapping = "YRUHQSLDPXNGOKMIEBFZCWVJAT"
                };
                break;

            case Reflector.C:
                returnValue = new EnigmaReflector()
                {
                    Mapping = "FVPJIAOYEDRZXWGCTKUQSBNMHL"
                };
                break;

            case Reflector.BThin:
                returnValue = new EnigmaReflector()
                {
                    Mapping = "ENKQAUYWJICOPBLMDXZVFTHRGS"
                };
                break;

            case Reflector.CThin:
                returnValue = new EnigmaReflector()
                {
                    Mapping = "RDOBJNTKVEHMLFCWZAXGYIPSUQ"
                };
                break;

            default:
                returnValue = new EnigmaReflector()
                {
                    Mapping = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
                };
                break;
            }
            return(returnValue);
        }
Ejemplo n.º 6
0
        //public EnigmaMachine(NestedRotor rotorI, NestedRotor rotorII, NestedRotor rotorIII, Reflector reflectorB) : this()
        //{
        //    rotorI.Drives = reflectorB;
        //    rotorII.Drives = rotorI;
        //    rotorIII.Drives = rotorII;
        //    _rotors = rotorIII;
        //}

        public void SetRotors(EnigmaRotor[] rotors)
        {
            EnigmaReflector reflector    = rotors[0] as EnigmaReflector;
            var             temp         = rotors.Skip(1).ToArray();
            var             nestedRotors = Array.ConvertAll(temp, item => (NestedRotor)item);


            for (int i = nestedRotors.Length - 1; i > 0; i--)
            {
                nestedRotors[i].Drives = nestedRotors[i - 1];
            }

            nestedRotors[0].Drives = reflector;
            _rotors = nestedRotors[nestedRotors.Length - 1];
        }
Ejemplo n.º 7
0
        private static EnigmaRotor[] BuildRotorArray()
        {
            EnigmaRotor rotorI   = EnigmaIMachineTestSupport.CreateRotorI();
            EnigmaRotor rotorII  = EnigmaIMachineTestSupport.CreateRotorII();
            EnigmaRotor rotorIII = EnigmaIMachineTestSupport.CreateRotorIII();

            EnigmaReflector reflectorB = EnigmaIMachineTestSupport.CreateBReflector();

            EnigmaRotor[] rotors = new EnigmaRotor[4];
            rotors[0] = reflectorB;
            rotors[1] = rotorI;
            rotors[2] = rotorII;
            rotors[3] = rotorIII;

            return(rotors);
        }
Ejemplo n.º 8
0
        public void Reflect(EnigmaReflectorNumber reflectorNumber, string wiring)
        {
            string characterSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

            IEnigmaReflector reflector = new EnigmaReflector()
            {
                ReflectorNumber = reflectorNumber,
            };

            Assert.Equal(reflectorNumber, reflector.ReflectorNumber);

            for (int i = 0; i < characterSet.Length; i++)
            {
                Assert.Equal(wiring[i], reflector.Reflect(characterSet[i]));
            }
        }
Ejemplo n.º 9
0
        public void Enigma_1941_07_07_19_25()
        {
            string ciphertext = new StringBuilder()
                                .Append("EDPUD NRGYS ZRCXN UYTPO MRMBO ")
                                .Append("FKTBZ REZKM LXLVE FGUEY SIOZV ")
                                .Append("EQMIK UBPMM YLKLT TDEIS MDICA ")
                                .Append("GYKUA CTCDO MOHWX MUUIA UBSTS ")
                                .Append("LRNBZ SZWNR FXWFY SSXJZ VIJHI ")
                                .Append("DISHP RKLKA YUPAD TXQSP INQMA ")
                                .Append("TLPIF SVKDA SCTAC DPBOP VHJK")
                                .ToString();

            // Reflector: B
            // Wheel order: II IV V
            // Ring positions:  02 21 12  (B U L)
            // Plug pairs: AV BS CG DL FU HZ IN KM OW RX
            // Message key: BLA
            // Final key: BRS
            IEnigmaReflector reflector = new EnigmaReflector()
            {
                ReflectorNumber = EnigmaReflectorNumber.B
            };

            IEnigmaRotors rotors = new EnigmaRotors()
            {
                Rotors = new Dictionary <EnigmaRotorPosition, IEnigmaRotor>()
                {
                    {
                        EnigmaRotorPosition.Fastest,
                        new EnigmaRotor()
                        {
                            RotorNumber    = EnigmaRotorNumber.V,
                            RingPosition   = 12,
                            CurrentSetting = 'A',
                        }
                    },
                    {
                        EnigmaRotorPosition.Second,
                        new EnigmaRotor()
                        {
                            RotorNumber    = EnigmaRotorNumber.IV,
                            RingPosition   = 21,
                            CurrentSetting = 'L',
                        }
                    },
                    {
                        EnigmaRotorPosition.Third,
                        new EnigmaRotor()
                        {
                            RotorNumber    = EnigmaRotorNumber.II,
                            RingPosition   = 2,
                            CurrentSetting = 'B',
                        }
                    },
                },
            };

            IList <EnigmaPlugboardPair> plugs = new List <EnigmaPlugboardPair>
            {
                { new EnigmaPlugboardPair()
                  {
                      From = 'A', To = 'V'
                  } },
                { new EnigmaPlugboardPair()
                  {
                      From = 'B', To = 'S'
                  } },
                { new EnigmaPlugboardPair()
                  {
                      From = 'C', To = 'G'
                  } },
                { new EnigmaPlugboardPair()
                  {
                      From = 'D', To = 'L'
                  } },
                { new EnigmaPlugboardPair()
                  {
                      From = 'F', To = 'U'
                  } },
                { new EnigmaPlugboardPair()
                  {
                      From = 'H', To = 'Z'
                  } },
                { new EnigmaPlugboardPair()
                  {
                      From = 'I', To = 'N'
                  } },
                { new EnigmaPlugboardPair()
                  {
                      From = 'K', To = 'M'
                  } },
                { new EnigmaPlugboardPair()
                  {
                      From = 'O', To = 'W'
                  } },
                { new EnigmaPlugboardPair()
                  {
                      From = 'R', To = 'X'
                  } },
            };
            IEnigmaPlugboard plugboard = new EnigmaPlugboard(plugs);

            string plaintext = new StringBuilder()
                               .Append("AUFKL XABTE ILUNG XVONX KURTI ")
                               .Append("NOWAX KURTI NOWAX NORDW ESTLX ")
                               .Append("SEBEZ XSEBE ZXUAF FLIEG ERSTR ")
                               .Append("ASZER IQTUN GXDUB ROWKI XDUBR ")
                               .Append("OWKIX OPOTS CHKAX OPOTS CHKAX ")
                               .Append("UMXEI NSAQT DREIN ULLXU HRANG ")
                               .Append("ETRET ENXAN GRIFF XINFX RGTX")
                               .ToString();

            IEnigmaSettings settings = new EnigmaSettings()
            {
                Reflector = reflector, Rotors = rotors, Plugboard = plugboard
            };
            ICipher cipher       = new Enigma(settings);
            string  newPlaintext = cipher.Decrypt(ciphertext.ToString());

            Assert.Equal(plaintext.ToString(), newPlaintext);
            Assert.Equal('S', rotors[EnigmaRotorPosition.Fastest].CurrentSetting);
            Assert.Equal('R', rotors[EnigmaRotorPosition.Second].CurrentSetting);
            Assert.Equal('B', rotors[EnigmaRotorPosition.Third].CurrentSetting);
        }
Ejemplo n.º 10
0
        public void SinghCodeBook()
        {
            string ciphertext = new StringBuilder()
                                .Append("KJQPW CAISR XWQMA SEUPF OCZOQ")
                                .Append("ZVGZG WWKYE ZVTEM TPZHV NOTKZ")
                                .Append("HRCCF QLVRP CCWLW PUYON FHOGD")
                                .Append("DMOJX GGBHW WUXNJ EZAXF UMEYS")
                                .Append("ECSMA ZFXNN ASSZG WRBDD MAPGM")
                                .Append("RWTGX XZAXL BXCPH ZBOUY VRRVF")
                                .Append("DKHXM QOGYL YYCUW QBTAD RLBOZ")
                                .Append("KYXQP WUUAF MIZTC EAXBC REDHZ")
                                .Append("JDOPS QTNLI HIQHN MJZUH SMVAH")
                                .Append("HQJLI JRRXQ ZNFKH UIINZ PMPAF")
                                .Append("LHYON MRMDA DFOXT YOPEW EJGEC")
                                .Append("AHPYF VMCIX AQDYI AGZXL DTFJW")
                                .Append("JQZMG BSNER MIPCK POVLT HZOTU")
                                .Append("XQLRS RZNQL DHXHL GHYDN ZKVBF")
                                .Append("DMXRZ BROMD PRUXH MFSHJ")
                                .ToString();

            // Reflector: B
            // Wheel order: III I II (Possibly III II I)
            // Ring positions: 01 01 01 (A A A) (?)
            // Plug pairs: EI AS JN KL MU OT
            // Message key: OUA (?)
            // Final key: BRS (?)
            IEnigmaReflector reflector = new EnigmaReflector()
            {
                ReflectorNumber = EnigmaReflectorNumber.B
            };

            IEnigmaRotors rotors = new EnigmaRotors()
            {
                Rotors = new Dictionary <EnigmaRotorPosition, IEnigmaRotor>()
                {
                    {
                        EnigmaRotorPosition.Fastest,
                        new EnigmaRotor()
                        {
                            RotorNumber    = EnigmaRotorNumber.II,
                            RingPosition   = 1,
                            CurrentSetting = 'A',
                        }
                    },
                    {
                        EnigmaRotorPosition.Second,
                        new EnigmaRotor()
                        {
                            RotorNumber    = EnigmaRotorNumber.I,
                            RingPosition   = 1,
                            CurrentSetting = 'U',
                        }
                    },
                    {
                        EnigmaRotorPosition.Third,
                        new EnigmaRotor()
                        {
                            RotorNumber    = EnigmaRotorNumber.III,
                            RingPosition   = 1,
                            CurrentSetting = 'O',
                        }
                    },
                },
            };

            IList <EnigmaPlugboardPair> plugs = new List <EnigmaPlugboardPair>()
            {
                { new EnigmaPlugboardPair()
                  {
                      From = 'E', To = 'I'
                  } },
                { new EnigmaPlugboardPair()
                  {
                      From = 'A', To = 'S'
                  } },
                { new EnigmaPlugboardPair()
                  {
                      From = 'J', To = 'N'
                  } },
                { new EnigmaPlugboardPair()
                  {
                      From = 'K', To = 'L'
                  } },
                { new EnigmaPlugboardPair()
                  {
                      From = 'M', To = 'U'
                  } },
                { new EnigmaPlugboardPair()
                  {
                      From = 'O', To = 'T'
                  } },
            };

            IEnigmaPlugboard plugboard = new EnigmaPlugboard(plugs);

            string plaintext = new StringBuilder()
                               .Append("DASXL OESUN GSWOR TXIST XPLUT")
                               .Append("OXXST UFEXN EUNXE NTHAE LTXEI")
                               .Append("NEXMI TTEIL UNGXD IEXMI TXDES")
                               .Append("XENTK ODIER TXIST XXICH XHABE")
                               .Append("XDASX LINKS STEHE NDEXB YTEXD")
                               .Append("ESXSC HLUES SELSX ENTDE CKTXX")
                               .Append("ESXIS TXEIN SXEIN SXZER OXEIN")
                               .Append("SXZER OXZER OXEIN SXEIN SXEIN")
                               .Append("SXXIC HXPRO GRAMM IERTE XDESX")
                               .Append("UNDXE NTDEC KTEXD ASSXD ASXWO")
                               .Append("RTXDE BUGGE RXWEN NXESX MITXD")
                               .Append("EMXUN TENST EHEND ENXSC HLUES")
                               .Append("SELXE NTKOD IERTX WIRDX ALSXR")
                               .Append("ESULT ATXDI EXUNT ENSTE HENDE")
                               .Append("NXSCH RIFTZ EICHE NXHAT")
                               .ToString();

            IEnigmaSettings settings = new EnigmaSettings()
            {
                Reflector = reflector, Rotors = rotors, Plugboard = plugboard
            };
            ICipher cipher       = new Enigma(settings);
            string  newPlaintext = cipher.Decrypt(ciphertext.ToString());

            Assert.Equal(plaintext.ToString(), newPlaintext);
            Assert.Equal('S', rotors[EnigmaRotorPosition.Fastest].CurrentSetting);
            Assert.Equal('R', rotors[EnigmaRotorPosition.Second].CurrentSetting);
            Assert.Equal('B', rotors[EnigmaRotorPosition.Third].CurrentSetting);
        }
Ejemplo n.º 11
0
        public void PracticalCryptography()
        {
            string ciphertext = new StringBuilder()
                                .Append("YXBMXADQBDBAAYIMKDODAYIXNBDQZF")
                                .Append("JKOLFVEEQBCLUUXDFVQYGKEYBVRHON")
                                .Append("JKPJMKUNLYLZUKBKJOAJTWVWMOMDPG")
                                .Append("VXEPUKXBVSGHROFOSBCNKEHEHAKWKO")
                                .Append("GWTBZFXSYCGSUUPPIZTRTFVCXZVCXT")
                                .Append("FLMTPTAQVMREGWSBFZBM")
                                .ToString();

            // Reflector: B
            // Wheel order: II V I
            // Ring positions:  23 15 02  (W O B)
            // Plug pairs: PO ML IU KJ NH YT
            // Message key: KJS
            // Final key: KPG
            IEnigmaReflector reflector = new EnigmaReflector()
            {
                ReflectorNumber = EnigmaReflectorNumber.B
            };

            IEnigmaRotors rotors = new EnigmaRotors()
            {
                Rotors = new Dictionary <EnigmaRotorPosition, IEnigmaRotor>()
                {
                    {
                        EnigmaRotorPosition.Fastest,
                        new EnigmaRotor()
                        {
                            RotorNumber    = EnigmaRotorNumber.I,
                            RingPosition   = 2,
                            CurrentSetting = 'S',
                        }
                    },
                    {
                        EnigmaRotorPosition.Second,
                        new EnigmaRotor()
                        {
                            RotorNumber    = EnigmaRotorNumber.V,
                            RingPosition   = 15,
                            CurrentSetting = 'J',
                        }
                    },
                    {
                        EnigmaRotorPosition.Third,
                        new EnigmaRotor()
                        {
                            RotorNumber    = EnigmaRotorNumber.II,
                            RingPosition   = 23,
                            CurrentSetting = 'K',
                        }
                    },
                },
            };

            IList <EnigmaPlugboardPair> plugs = new List <EnigmaPlugboardPair>()
            {
                { new EnigmaPlugboardPair()
                  {
                      From = 'P', To = 'O'
                  } },
                { new EnigmaPlugboardPair()
                  {
                      From = 'M', To = 'L'
                  } },
                { new EnigmaPlugboardPair()
                  {
                      From = 'I', To = 'U'
                  } },
                { new EnigmaPlugboardPair()
                  {
                      From = 'K', To = 'J'
                  } },
                { new EnigmaPlugboardPair()
                  {
                      From = 'N', To = 'H'
                  } },
                { new EnigmaPlugboardPair()
                  {
                      From = 'Y', To = 'T'
                  } },
            };

            IEnigmaPlugboard plugboard = new EnigmaPlugboard(plugs);

            string plaintext = new StringBuilder()
                               .Append("THEENIGMACIPHERWASAFIELDCIPHER")
                               .Append("USEDBYTHEGERMANSDURINGWORLDWAR")
                               .Append("IITHEENIGMAISONEOFTHEBETTERKNO")
                               .Append("WNHISTORICALENCRYPTIONMACHINES")
                               .Append("ANDITACTUALLYREFERSTOARANGEOFS")
                               .Append("IMILARCIPHERMACHINES")
                               .ToString();

            IEnigmaSettings settings = new EnigmaSettings()
            {
                Reflector = reflector, Rotors = rotors, Plugboard = plugboard
            };
            ICipher cipher       = new Enigma(settings);
            string  newPlaintext = cipher.Decrypt(ciphertext.ToString());

            Assert.Equal(plaintext.ToString(), newPlaintext);
            Assert.Equal('G', rotors[EnigmaRotorPosition.Fastest].CurrentSetting);
            Assert.Equal('P', rotors[EnigmaRotorPosition.Second].CurrentSetting);
            Assert.Equal('K', rotors[EnigmaRotorPosition.Third].CurrentSetting);
        }
Ejemplo n.º 12
0
        public void Defaults()
        {
            IEnigmaReflector reflector = new EnigmaReflector();

            Assert.Equal(EnigmaReflectorNumber.B, reflector.ReflectorNumber);
        }
Ejemplo n.º 13
0
 public void CanCreateReflector()
 {
     EnigmaReflector reflector = new EnigmaReflector();
 }
Ejemplo n.º 14
0
        public void ReflectorAllowsKnownValidWiring()
        {
            EnigmaReflector reflector = new EnigmaReflector();

            reflector.Mapping = "EJMZALYXVBWFCRQUONTSPIKHGD";
        }
Ejemplo n.º 15
0
        public void ReflectorValidatesOnWiring()
        {
            EnigmaReflector reflector = new EnigmaReflector();

            reflector.Mapping = "JEMZALYXVBWFCRQUONTSPIKHGD";
        }