Esempio n. 1
14
        static void Main(string[] args)
        {
            Random myRand = new Random();
            Enigma myCode = new Enigma(myRand);
            int userChoice;
            string userMessage;

            do
            {
                Console.Write("Enter '1' to Encrypt a message, '2' to Decrypt a message, or '0' to Exit: ");
                userChoice = Int32.Parse(Console.ReadLine());

                switch (userChoice)
                {
                    case 1:
                        Console.WriteLine("\nWhat message would you like to encrypt?\n");
                        userMessage = Console.ReadLine().ToUpper();
                        Console.WriteLine(myCode.Encrypt(userMessage));
                        Console.WriteLine();
                        break;
                    case 2:
                        Console.WriteLine("\nWhat message would you like to decrypt?\n");
                        userMessage = Console.ReadLine().ToUpper();
                        Console.WriteLine(myCode.Decrypt(userMessage));
                        Console.WriteLine();
                        break;
                    default:
                        break;
                }
            } while (userChoice != 0);
        }
Esempio n. 2
0
        public void Decrypt(string plaintext, string ciphertext, char newFastestRotorPosition)
        {
            IEnigmaSettings settings = new EnigmaSettings();
            ICipher         cipher   = new Enigma(settings);

            Assert.Equal(plaintext, cipher.Decrypt(ciphertext));
            Assert.Equal(newFastestRotorPosition, settings.Rotors[EnigmaRotorPosition.Fastest].CurrentSetting);
        }
Esempio n. 3
0
        public void TestDecryption(string message, string encrypt)
        {
            // a b c d e f g h i j k l m n o p q r s t u v w x y z
            // ! ) " ( £ * % & > < @ a b c d e f g h i j k l m n o

            var secret = Enigma.Decrypt(encrypt);

            Assert.AreEqual(message, secret);
        }
Esempio n. 4
0
 public static Model FromRijndael <Model>(this string cipherText) where Model : class
 {
     return(Enigma.Decrypt(cipherText).FromJson <Model>());
 }
Esempio n. 5
0
 public static string FromRijndael(this string cipherText)
 {
     return(Enigma.Decrypt(cipherText));
 }
Esempio n. 6
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);
        }
Esempio n. 7
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);
        }
Esempio n. 8
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);
        }
Esempio n. 9
0
        public void TestDecrypting()
        {
            string result = Enigma.Decrypt(Encrypted, key);

            Assert.AreEqual(sourceText, result);
        }