def test_encryption_w_m3_reflectorC(self): enigma = Enigma(reflector=ReflectorC(), plugboard=Plugboard(), rotors=[RotorIII(), RotorII(), RotorI()]) self.assertEqual(enigma.decrypt(enigma.encrypt("A")), "A") self.assertEqual(enigma.encrypt("AAAAA"), "PJBUZ") self.assertEqual( enigma.encrypt(REDUCED_LOREM_IPSUM), "KQZIFEZXSBHUFEFRRNSHTHEDTISEBMIKIURGQERSLBAVMMINXGWLKRCLKQWKBFUKEMSISOUNNBFGATWWKDP" ) cyphertext = enigma.encrypt(LOREM_IPSUM) self.assertEqual( enigma.decrypt(cyphertext), "".join([c for c in LOREM_IPSUM.upper() if c.isalpha()]))
def test_encryption_w_m3_reflectorB(self): enigma = Enigma(reflector=ReflectorB(), plugboard=Plugboard(), rotors=[RotorIII(), RotorII(), RotorI()]) self.assertEqual(enigma.decrypt(enigma.encrypt("A")), "A") self.assertEqual(enigma.encrypt("AAAAA"), "BDZGO") self.assertEqual(enigma.encrypt("PROBANDO"), "LCNCOEFQ") self.assertEqual( enigma.encrypt(REDUCED_LOREM_IPSUM), "PIXWHLIFPVFHECGDYIPYYXWASFXPPOALEJGRHCZXDTNKGZAFAZXFGOSGIYMRMEEZHQBOWSKIZJAVWFDATOH" ) cyphertext = enigma.encrypt(LOREM_IPSUM) self.assertEqual( enigma.decrypt(cyphertext), "".join([c for c in LOREM_IPSUM.upper() if c.isalpha()]))
def test_encryption_decryption_w_ukw_c(self): enigma = Enigma(reflector=ReflectorC(), plugboard=Plugboard(PLUGBOARD_TUPLES), rotors=[ Rotor(offset=10, period=1), Rotor(offset=10, period=2), Rotor(offset=10, period=3), Rotor(offset=10, period=7), Rotor(offset=10, period=26) ]) cyphertext = enigma.encrypt(LOREM_IPSUM) self.assertEqual( enigma.decrypt(cyphertext), "".join([c for c in LOREM_IPSUM.upper() if c.isalpha()]))
def test_encryption_double_notch_rotors(self): enigma = Enigma(reflector=ReflectorB(), plugboard=Plugboard(), rotors=[ RotorVIII(ring_setting=1), RotorVII(ring_setting=1), RotorVI(ring_setting=1) ]) self.assertEqual( enigma.encrypt(REDUCED_LOREM_IPSUM), "QPEVWGFMEFUKAMFQQOHBMHYMEQNTFCOWYMKZBTGEEMWZEPLDYYNTLPXAVEYCZBJWYOQMLCJHITIYZRDZTPX" ) cyphertext = enigma.encrypt(LOREM_IPSUM) self.assertEqual( enigma.decrypt(cyphertext), "".join([c for c in LOREM_IPSUM.upper() if c.isalpha()]))
def test_encryption_m4(self): enigma = Enigma(reflector=ReflectorBThin(), plugboard=Plugboard(), rotors=[ RotorIII(ring_setting=1), RotorII(), RotorI(), RotorBeta(ring_setting=3) ]) self.assertEqual( enigma.encrypt(REDUCED_LOREM_IPSUM), "NXVMWHIMNFCIDSJXDNQTIBJNYMAWWWVNCPQZJREWOUCNDKSHKQRBMETDSWEJDVJUIEGETVQUAORAYMKMDFA" ) cyphertext = enigma.encrypt(LOREM_IPSUM) self.assertEqual( enigma.decrypt(cyphertext), "".join([c for c in LOREM_IPSUM.upper() if c.isalpha()]))
def test_encryption_w_ringstellung(self): enigma = Enigma(reflector=ReflectorB(), plugboard=Plugboard(), rotors=[ RotorIII(ring_setting=1), RotorII(ring_setting=1), RotorI(ring_setting=1) ]) self.assertEqual(enigma.encrypt("AAAAA"), "EWTYX") self.assertEqual( enigma.encrypt(REDUCED_LOREM_IPSUM), "HEPCLDHGHAMAXFGUGXCKOFIHDQLOMXROJEYSVJWPUBSCQOAMNMNZBKPPPNLFJOILBXIBKXTGEKEGQNSQZPK" ) cyphertext = enigma.encrypt(LOREM_IPSUM) self.assertEqual( enigma.decrypt(cyphertext), "".join([c for c in LOREM_IPSUM.upper() if c.isalpha()]))
def crack(self, encrypted_message): combinations = list( product(range(len(GERMAN_ALPHABET_CHARACTERS)), repeat=4)) shuffle(combinations) print(combinations[:10]) kb_divergences = {} i = 0 for combination in combinations: rotor_1 = Rotor(combination[0], HARDCODED_PERIOD) rotor_2 = Rotor(combination[1], HARDCODED_PERIOD) rotor_3 = Rotor(combination[2], HARDCODED_PERIOD) rotor_4 = Rotor(combination[3], HARDCODED_PERIOD) enigma_machine = Enigma(plugboard, [rotor_1, rotor_2, rotor_3, rotor_4]) decrypted_message = enigma_machine.decrypt(encrypted_message) kb_divergences[combination] = ( self.language_model.fitness(decrypted_message), decrypted_message) #print(i) i += 1 sorted_divergences = sorted(kb_divergences.items(), key=lambda x: x[1]) return sorted_divergences[:1000]
def test_encryption_decryption_one_rotor(self): enigma = Enigma(reflector=Reflector(), plugboard=Plugboard(PLUGBOARD_TUPLES), rotors=[Rotor(offset=10, period=1)]) cyphertext = enigma.encrypt("Probando") self.assertEqual(enigma.decrypt(cyphertext), "PROBANDO")