Пример #1
0
    def test_emoji_machine(self):
        custom_alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ😀😃😄😁😆😅😂🤣😊😇🙂🙃😉😌😍🥰😘😗😚😋😛😝😜"
        rotor_I = Rotor("Test",
                        "🙃😉😌😍🥰😘😗😚😋😛😝😜😀😃😄😁😆😅😂🤣😊😇🙂EKMFLGDQVZNTOWYHXUSPAIBRCJ",
                        "A", custom_alphabet)
        rotor_II = Rotor("Test",
                         "AJDKSIRUXBLHWTMCQGZNPYFVOE🙃😉😌😍🥰😘😗😚😋😛😝😜😀😃😄😁😆😅😂🤣😊😇🙂",
                         "😍", custom_alphabet)
        rotor_III = Rotor("Test",
                          "😗😚😋😛😝😜😀😃😄😁😆BDFHJLCPRTXVZNYEIWGAKMUSQO🙃😉😌😍🥰😘😅😂🤣😊😇🙂",
                          "😂", custom_alphabet)
        reflector = Rotor("Reflector",
                          "😜😃😄😁😆😅😂🤣😊😇🙂🙃😉😌😍🥰😘😗😚😋😛😝😀EJMZALYXVBWFCRQUONTSPIKHGD",
                          None, custom_alphabet)

        machine = Machine([rotor_I, rotor_II, rotor_III], reflector)

        machine.set_rotor_positions(custom_alphabet[0])

        initial_str = "THIS ENIGMA MACHINE SUPPORTS EMOJIS! 😜😍🥰"
        encoded_str = "😆QK😗 😋🤣FUR😉 SX😚😇😜T😜 😃😂😃😄U😌A😚 😝😛😘😇W🙃! TO😍"

        # encodes string properly
        self.assertEqual(machine.encode(initial_str), encoded_str)

        machine.set_rotor_positions(custom_alphabet[0], custom_alphabet[0],
                                    custom_alphabet[0])

        # decodes string properly
        self.assertEqual(encoded_str, machine.encode(initial_str))
Пример #2
0
    def test_machine_encodes_custom_alphabets(self):
        custom_alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
        rotor = Rotor(
            "Test",
            "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890",
            "b", custom_alphabet)
        reflector = Rotor(
            "Reflector",
            "ejmzalyxvbwfcrquontspikhgdEJMZALYXVBWFCRQUONTSPIKHGD0987654321",
            None, custom_alphabet)

        machine = Machine([rotor], reflector)

        machine.set_rotor_positions(custom_alphabet[0])

        initial_str = "Enigma Can Encode Case Sensitively With numbers like 123"
        encoded_str = "ArvYce MeR ARmgpa Wexa 6aHxjwjug9Q Mj3d 8Za6gnt PtWg 50c"

        # encodes string properly
        self.assertEqual(machine.encode(initial_str), encoded_str)

        machine.set_rotor_positions(custom_alphabet[0])

        # decodes string properly
        self.assertEqual(encoded_str, machine.encode(initial_str))
Пример #3
0
 def test_straight_rotor(self):
     """Test rotor encoding does not change with straight wiring."""
     for position in ascii_uppercase:
         for ring_setting in range(1, len(ascii_uppercase) + 1):
             for reverse in (True, False):
                 rotor = Rotor(
                     wiring=ascii_uppercase,
                     position=position,
                     ring_setting=ring_setting,
                 )
                 output = [
                     rotor.encode(letter, reverse=reverse)
                     for letter in ascii_uppercase
                 ]
                 self.assertEqual(output, list(ascii_uppercase))
Пример #4
0
    def setUp(self):
        self.enigma1_rotorI = Rotor(list("ekmflgdqvzntowyhxuspaibrcj"),
                                    turnover_notch=["q"])
        self.enigma1_rotorII = Rotor(list("ajdksiruxblhwtmcqgznpyfvoe"),
                                     turnover_notch=["e"])
        self.enigma1_rotorIII = Rotor(list("bdfhjlcprtxvznyeiwgakmusqo"),
                                      turnover_notch=["v"])
        self.enigma1_wide_B_reflector = Reflector(
            list("yruhqsldpxngokmiebfzcwvjat"))

        self.my_enigma = Enigma(
            self.enigma1_wide_B_reflector,
            self.enigma1_rotorI,
            self.enigma1_rotorII,
            self.enigma1_rotorIII,
            double_step=True,
        )
Пример #5
0
    def test_custom_alphabet(self):
        rotor = Rotor(
            "Test",
            "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890",
            "A4",
            "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890")
        rotor.set_position('z')

        rotor.rotate()
        self.assertEqual(rotor.get_position(), "A")
        self.assertTrue(rotor.is_on_notch())
Пример #6
0
    def test_multiple_notches(self):
        notches = "ZABT"
        rotor = Rotor("Test", "ABCDEFGHIJKLMNOPQRSTUVWXYZ", notches)
        rotor.set_position('A')

        for i in range(ord("A"), ord("Z") + 1):
            should_be_on_position = chr(i)
            should_be_on_notch = should_be_on_position in notches

            self.assertEqual(rotor.get_position(), should_be_on_position)
            self.assertEqual(rotor.is_on_notch(), should_be_on_notch)

            rotor.rotate()

        self.assertEqual(rotor.get_position(), "A")
Пример #7
0
 def get_enigma(
     self,
     rotors=[
         {
             "wiring": "EKMFLGDQVZNTOWYHXUSPAIBRCJ",
             "position": "A",
             "ring_setting": 1,
             "turnover_positions": ["R"],
         },
         {
             "wiring": "AJDKSIRUXBLHWTMCQGZNPYFVOE",
             "position": "A",
             "ring_setting": 1,
             "turnover_positions": ["F"],
         },
         {
             "wiring": "BDFHJLCPRTXVZNYEIWGAKMUSQO",
             "position": "A",
             "ring_setting": 1,
             "turnover_positions": ["W"],
         },
     ],
     reflector="YRUHQSLDPXNGOKMIEBFZCWVJAT",
     plugboard=[],
 ):
     """Test enigma object can be created."""
     used_rotors = [
         Rotor(
             wiring=_["wiring"],
             position=_["position"],
             ring_setting=_["ring_setting"],
             turnover_positions=_["turnover_positions"],
         ) for _ in rotors
     ]
     used_reflector = Reflector(wiring=reflector)
     used_plugboard = Plugboard(plugboard)
     return Enigma(rotors=used_rotors,
                   reflector=used_reflector,
                   plugboard=used_plugboard)
Пример #8
0
import random
from enigma import ALPHABET, PlugBoard, Rotor, Reflector, EnigmaMachine

if __name__ == '__main__':

    get_random_alphabet = lambda: ''.join(
        random.sample(ALPHABET, len(ALPHABET)))
    p = PlugBoard(get_random_alphabet())
    r1 = Rotor(get_random_alphabet(), 3)
    r2 = Rotor(get_random_alphabet(), 2)
    r3 = Rotor(get_random_alphabet(), 1)

    r = list(ALPHABET)
    indexes = [i for i in range(len(ALPHABET))]
    for _ in range(int(len(indexes) / 2)):
        x = indexes.pop(random.randint(0, len(indexes) - 1))
        y = indexes.pop(random.randint(0, len(indexes) - 1))
        r[x], r[y] = r[y], r[x]
    reflector = Reflector(''.join(r))

    machine = EnigmaMachine(p, [r1, r2, r3], reflector)
    s = 'ATTACK SILICON VALLEY'
    e = machine.encrypt(s)
    print(e)
    d = machine.decrypt(e)
    print(d)
Пример #9
0
 def get_rotors(self, settings):
     """Return rotor set."""
     return [Rotor(**setting) for setting in settings]
Пример #10
0
 def test_disallow_notch_not_in_alphabet(self):
     with self.assertRaises(ValueError):
         Rotor("Test", "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "4")