Beispiel #1
0
    def get_crypto(type: dict, settings: dict, *args) -> (Dict[str, dict], ICrypto):
        type = CryptoMaker.get_type(type)

        if type == 'enigma':
            settings = CryptoMaker.get_settings(type, settings)
            enigma_str_key, enigma_reflector, enigma_rotors, enigma_plugboard = settings['key'], settings['reflector'], settings['rotors'], settings['plug_board']

            plug_board = PlugBoard(enigma_plugboard)
            reflector = Reflector(EnigmaSettings.get_reflectors()[enigma_reflector])  # A/B/C
            rotor_1 = Rotor(EnigmaSettings.get_rotors()[enigma_rotors['1']['type']],
                            enigma_rotors['1']['ring'])  # I / II / III / IV / V
            rotor_2 = Rotor(EnigmaSettings.get_rotors()[enigma_rotors['2']['type']],
                            enigma_rotors['2']['ring'])  # I / II / III / IV / V
            rotor_3 = Rotor(EnigmaSettings.get_rotors()[enigma_rotors['3']['type']],
                            enigma_rotors['3']['ring'])  # I / II / III / IV / V

            enigma_key = Key[str]()
            for i, char in enumerate(enigma_str_key):
                enigma_key.set_key(str(i), char)
            rotor_mechanism = RotorMechanism([rotor_3, rotor_2, rotor_1], reflector)
            return {type: dict(settings)}, Enigma(enigma_key, plug_board, rotor_mechanism)
        elif type == 'caesar':
            key_exchange: IKeyExchangeGenerator = args[0]
            caesar_key = Key[int]({'key': key_exchange.get_key_exchange().get_k()})
            key_exchange.set_crypto(Caesar(caesar_key))
        else:
            return None
 def test_rotor_mechanism_4(self):
     reflector = Reflector(EnigmaSettings.get_reflectors()['B'])  # A/B/C
     rotor_1 = Rotor(EnigmaSettings.get_rotors()['I'], 'A')  # I / II / III / IV / V
     rotor_2 = Rotor(EnigmaSettings.get_rotors()['II'], 'A')  # I / II / III / IV / V
     rotor_3 = Rotor(EnigmaSettings.get_rotors()['III'], 'A')  # I / II / III / IV / V
     rotor_mechanism = RotorMechanism([rotor_3, rotor_2, rotor_1], reflector)
     self.assertEqual('A', chr(rotor_mechanism.process(ord('B') - ord('A') + 1) - 1 + ord('A')))
     self.assertEqual('A', chr(rotor_mechanism.process(ord('D') - ord('A') + 1) - 1 + ord('A')))
     self.assertEqual('A', chr(rotor_mechanism.process(ord('Z') - ord('A') + 1) - 1 + ord('A')))
     self.assertEqual('A', chr(rotor_mechanism.process(ord('G') - ord('A') + 1) - 1 + ord('A')))
     self.assertEqual('A', chr(rotor_mechanism.process(ord('O') - ord('A') + 1) - 1 + ord('A')))
 def test_rotor_mechanism_2(self):
     rotor_1 = Rotor(EnigmaSettings.get_rotors()['I'], 'A') #I / II / III / IV / V
     rotor_2 = Rotor(EnigmaSettings.get_rotors()['II'], 'A')  # I / II / III / IV / V
     rotor_3 = Rotor(EnigmaSettings.get_rotors()['III'], 'A')  # I / II / III / IV / V
     rotor_mechanism = RotorMechanism([rotor_3, rotor_2, rotor_1], None)
     try:
         rotor_mechanism.process(25)
         self.assertFalse(True)
     except AttributeError as ae:
         self.assertEqual(str(ae), str(AttributeError('\'NoneType\' object has no attribute \'wiring\'')))
     try:
         rotor_mechanism.process(225)
         self.assertFalse(True)
     except KeyError as ke:
         self.assertTrue(True)
 def test_enigma_2(self):
     plug_board = PlugBoard(['ab', 'ce'])
     reflector = Reflector(EnigmaSettings.get_reflectors()['B'])  # A/B/C
     rotor_1 = Rotor(EnigmaSettings.get_rotors()['I'], 'A')  # I / II / III / IV / V
     rotor_2 = Rotor(EnigmaSettings.get_rotors()['II'], 'A')  # I / II / III / IV / V
     rotor_3 = Rotor(EnigmaSettings.get_rotors()['III'], 'A')  # I / II / III / IV / V
     rotor_mechanism = RotorMechanism([rotor_3, rotor_2, rotor_1], reflector)
     enigma_key = Key()
     enigma_key.set_key('I', 'D')
     enigma_key.set_key('II', 'O')
     enigma_key.set_key('III', 'G')
     enigma = Enigma(enigma_key, plug_board, rotor_mechanism)
     p_text = "ENIGMA"
     c_text = enigma.encryption(p_text)
     p_text_2 = enigma.decryption(c_text)
     self.assertEqual(p_text, p_text_2)
 def test_enigma_3(self):
     plug_board = PlugBoard(['ab', 'ce'])
     reflector = Reflector(EnigmaSettings.get_reflectors()['B'])  # A/B/C
     rotor_1 = Rotor(EnigmaSettings.get_rotors()['I'], 'A')  # I / II / III / IV / V
     rotor_2 = Rotor(EnigmaSettings.get_rotors()['II'], 'A')  # I / II / III / IV / V
     rotor_3 = Rotor(EnigmaSettings.get_rotors()['III'], 'A')  # I / II / III / IV / V
     rotor_mechanism = RotorMechanism([rotor_3, rotor_2, rotor_1], reflector)
     enigma_key = Key()
     enigma_key.set_key('I', 'D')
     enigma_key.set_key('II', 'O')
     enigma_key.set_key('III', 'G')
     enigma = Enigma(enigma_key, plug_board, rotor_mechanism)
     p_text = "3"
     try:
         c_text = enigma.encryption(p_text)
         self.assertFalse(True)
     except ValueError as ve:
         self.assertEqual(str(ve), str(ValueError('EnigmaStatic_valid_character: unexpected character, enigma only supports A-Z characters')))
 def test_cbc_mode_3(self):
     pain_text = 'ABCC'
     plug_board = PlugBoard(['ab', 'ce'])
     reflector = Reflector(EnigmaSettings.get_reflectors()['B'])  # A/B/C
     rotor_1 = Rotor(EnigmaSettings.get_rotors()['III'], 'A')  # I / II / III / IV / V
     rotor_2 = Rotor(EnigmaSettings.get_rotors()['II'], 'A')  # I / II / III / IV / V
     rotor_3 = Rotor(EnigmaSettings.get_rotors()['III'], 'A')  # I / II / III / IV / V
     rotor_mechanism = RotorMechanism([rotor_3, rotor_2, rotor_1], reflector)
     enigma_key = Key[str]()
     enigma_key.set_key('I', 'D')
     enigma_key.set_key('II', 'O')
     enigma_key.set_key('III', 'G')
     enigma = Enigma(enigma_key, plug_board, rotor_mechanism)
     cbc_key = Key[str]()
     cbc_key.set_key('cbc_key', '0SD')
     cbc_mode = InverseCBCMode(enigma, cbc_key)
     c_msg = cbc_mode.encryption(pain_text)
     p_msg = cbc_mode.decryption(c_msg)
     pain_text += (len(p_msg) - len(pain_text)) * 'F'
     self.assertEqual(pain_text, p_msg)
 def test_rotor_2(self):
     rotor = Rotor(EnigmaSettings.get_rotors()['I'])
     end_char = ord(EnigmaSettings.get_rotors()['I']['notch']) - ord('A')
     rotor.set_position('A')
     for i in range(0, end_char - 1):
         self.assertEqual(False, rotor.click())
     self.assertEqual(True, rotor.click())
 def test_rotor_1(self):
     rotor = Rotor(EnigmaSettings.get_rotors()['I'])
     self.assertEqual(rotor.rotor_description, EnigmaSettings.get_rotors()['I'])