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_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_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()]))
Beispiel #7
0
                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]


plugboard_pairs = []
# TODO: Discuss if plugboard pairs are random.
characters = [a.upper() for a in GERMAN_ALPHABET_CHARACTERS]
shuffle(characters)
for i in range(10):
    plugboard_pairs.append((characters[i], characters[-i - 1]))

plugboard = Plugboard(plugboard_pairs)

language_model2 = CharacterFrequencyKLDLanguageModel(
    "probarunanuevamaneradeescribir".upper())
cracker = Cracker(language_model2, 10)

enigma = Enigma(plugboard, [
    Rotor(1, HARDCODED_PERIOD),
    Rotor(10, HARDCODED_PERIOD),
    Rotor(12, HARDCODED_PERIOD),
    Rotor(15, HARDCODED_PERIOD)
])

encrypted = enigma.encrypt("probarunanuevamaneradeescribir".upper())

print(cracker.crack(encrypted))
Beispiel #8
0
           ring_setting=0,
           stepping='Q')
r2 = Rotor('my rotor2',
           'AJDKSIRUXBLHWTMCQGZNPYFVOE',
           ring_setting=5,
           stepping='E')
r3 = Rotor('my rotor3',
           'BDFHJLCPRTXVZNYEIWGAKMUSQO',
           ring_setting=15,
           stepping='V')

#Reflector
reflector = Rotor('my reflector', 'FVPJIAOYEDRZXWGCTKUQSBNMHL')

#Plugboard
pb = Plugboard.from_key_sheet('PO ML IU KJ NH YT GB VF RE DC')

#Machine
machine_encrypt = EnigmaMachine([r1, r2, r3], reflector, pb)
machine_decrypt = EnigmaMachine([r1, r2, r3], reflector, pb)


def encrypter():

    global message
    global ciphertext_encrypt
    # Set the initial position of the Enigma rotors
    machine_encrypt.set_display('AFP')
    print("AFP")

    # Encrypt the text 'BFR' and sotre it as msg_key
 def test_empty_plugboard(self):
     plugboard = Plugboard()
     self.assertEqual(plugboard.transform('A'), 'A')
     self.assertEqual(plugboard.transform('Z'), 'Z')
     self.assertEqual(plugboard.transform('C'), 'C')
 def test_plugboard_double_wiring_error(self):
     with self.assertRaises(Exception):
         Plugboard([('A', 'C'), ('A', 'B')])
 def test_plugboard_transform_to_the_same(self):
     plugboard = Plugboard([('A', 'A')])
     self.assertEqual(plugboard.transform('A'), 'A')
     self.assertEqual(plugboard.transform('Z'), 'Z')
     self.assertEqual(plugboard.transform('C'), 'C')
 def test_simple_plugboard(self):
     plugboard = Plugboard([('A', 'Z')])
     self.assertEqual(plugboard.transform('A'), 'Z')
     self.assertEqual(plugboard.transform('Z'), 'A')
     self.assertEqual(plugboard.transform('C'), 'C')
 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")