public void RotorOrder() { IEnigmaRotors settings = new EnigmaRotors() { Rotors = new Dictionary <EnigmaRotorPosition, IEnigmaRotor>() { { EnigmaRotorPosition.Fastest, new EnigmaRotor() { RotorNumber = EnigmaRotorNumber.IV } }, { EnigmaRotorPosition.Second, new EnigmaRotor() { RotorNumber = EnigmaRotorNumber.II } }, { EnigmaRotorPosition.Third, new EnigmaRotor() { RotorNumber = EnigmaRotorNumber.III } }, }, }; Assert.Equal(EnigmaRotorNumber.IV, settings[EnigmaRotorPosition.Fastest].RotorNumber); Assert.Equal(EnigmaRotorNumber.II, settings[EnigmaRotorPosition.Second].RotorNumber); Assert.Equal(EnigmaRotorNumber.III, settings[EnigmaRotorPosition.Third].RotorNumber); }
public void RingPositionDefaults() { IEnigmaRotors settings = new EnigmaRotors(); Assert.Equal(1, settings[EnigmaRotorPosition.Fastest].RingPosition); Assert.Equal(1, settings[EnigmaRotorPosition.Second].RingPosition); Assert.Equal(1, settings[EnigmaRotorPosition.Third].RingPosition); }
public void CurrentSettingDefaults() { IEnigmaRotors settings = new EnigmaRotors(); Assert.Equal('A', settings[EnigmaRotorPosition.Fastest].CurrentSetting); Assert.Equal('A', settings[EnigmaRotorPosition.Second].CurrentSetting); Assert.Equal('A', settings[EnigmaRotorPosition.Third].CurrentSetting); }
public void RotorOrderDefaults() { IEnigmaRotors settings = new EnigmaRotors(); Assert.Equal(EnigmaRotorNumber.I, settings[EnigmaRotorPosition.Fastest].RotorNumber); Assert.Equal(EnigmaRotorNumber.II, settings[EnigmaRotorPosition.Second].RotorNumber); Assert.Equal(EnigmaRotorNumber.III, settings[EnigmaRotorPosition.Third].RotorNumber); }
public void RingPosition() { IEnigmaRotors settings = new EnigmaRotors(); settings[EnigmaRotorPosition.Fastest].RingPosition = 02; settings[EnigmaRotorPosition.Second].RingPosition = 04; settings[EnigmaRotorPosition.Third].RingPosition = 05; Assert.Equal(2, settings[EnigmaRotorPosition.Fastest].RingPosition); Assert.Equal(4, settings[EnigmaRotorPosition.Second].RingPosition); Assert.Equal(5, settings[EnigmaRotorPosition.Third].RingPosition); }
public void CurrentSettingSet() { IEnigmaRotors settings = new EnigmaRotors(); settings[EnigmaRotorPosition.Fastest].CurrentSetting = 'B'; settings[EnigmaRotorPosition.Second].CurrentSetting = 'D'; settings[EnigmaRotorPosition.Third].CurrentSetting = 'E'; Assert.Equal('B', settings[EnigmaRotorPosition.Fastest].CurrentSetting); Assert.Equal('D', settings[EnigmaRotorPosition.Second].CurrentSetting); Assert.Equal('E', settings[EnigmaRotorPosition.Third].CurrentSetting); }
public void Rotors() { IEnigmaRotors rotors = new EnigmaRotors() { Rotors = new Dictionary <EnigmaRotorPosition, IEnigmaRotor>() { { EnigmaRotorPosition.Fastest, new EnigmaRotor() { RotorNumber = EnigmaRotorNumber.VI, RingPosition = 2, CurrentSetting = 'X' } }, { EnigmaRotorPosition.Second, new EnigmaRotor() { RotorNumber = EnigmaRotorNumber.VII, RingPosition = 3, CurrentSetting = 'Y' } }, { EnigmaRotorPosition.Third, new EnigmaRotor() { RotorNumber = EnigmaRotorNumber.VIII, RingPosition = 4, CurrentSetting = 'Z' } }, }, }; IEnigmaSettings settings = new EnigmaSettings() { Rotors = rotors, }; Assert.Equal(rotors.Rotors[EnigmaRotorPosition.Fastest].RotorNumber, settings.Rotors[EnigmaRotorPosition.Fastest].RotorNumber); Assert.Equal(rotors.Rotors[EnigmaRotorPosition.Second].RotorNumber, settings.Rotors[EnigmaRotorPosition.Second].RotorNumber); Assert.Equal(rotors.Rotors[EnigmaRotorPosition.Third].RotorNumber, settings.Rotors[EnigmaRotorPosition.Third].RotorNumber); Assert.Equal(rotors.Rotors[EnigmaRotorPosition.Fastest].RingPosition, settings.Rotors[EnigmaRotorPosition.Fastest].RingPosition); Assert.Equal(rotors.Rotors[EnigmaRotorPosition.Second].RingPosition, settings.Rotors[EnigmaRotorPosition.Second].RingPosition); Assert.Equal(rotors.Rotors[EnigmaRotorPosition.Third].RingPosition, settings.Rotors[EnigmaRotorPosition.Third].RingPosition); Assert.Equal(rotors.Rotors[EnigmaRotorPosition.Fastest].CurrentSetting, settings.Rotors[EnigmaRotorPosition.Fastest].CurrentSetting); Assert.Equal(rotors.Rotors[EnigmaRotorPosition.Second].CurrentSetting, settings.Rotors[EnigmaRotorPosition.Second].CurrentSetting); Assert.Equal(rotors.Rotors[EnigmaRotorPosition.Third].CurrentSetting, settings.Rotors[EnigmaRotorPosition.Third].CurrentSetting); }
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); }
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); }
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); }
public void RingPositionInvalid() { IEnigmaRotors settings = new EnigmaRotors(); Assert.Throws <ArgumentOutOfRangeException>(() => settings[EnigmaRotorPosition.Fastest].RingPosition = 'Å'); }