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_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_rotor_mechanism_1(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], 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.assertFalse(False)
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_reflector_2(self): reflector = Reflector(EnigmaSettings.get_reflectors()['A']) self.assertEqual(reflector.wiring, dict({1: 5, 2: 10, 3: 13, 4: 26, 5: 1, 6: 12, 7: 25, 8: 24, 9: 22, 10: 2, 11: 23, 12: 6, 13: 3, 14: 18, 15: 17, 16: 21, 17: 15, 18: 14, 19: 20, 20: 19, 21: 16, 22: 9, 23: 11, 24: 8, 25: 7, 26: 4}))
def test_reflector_1(self): reflector = Reflector(EnigmaSettings.get_reflectors()['A']) self.assertEqual(reflector.rotor_description, EnigmaSettings.get_reflectors()['A'])
def test_reflector_5(self): reflector = Reflector(EnigmaSettings.get_reflectors()['A']) reflector.set_position('D') self.assertEqual(reflector.ring_settings_to_keys, dict({4: 1, 5: 2, 6: 3, 7: 4, 8: 5, 9: 6, 10: 7, 11: 8, 12: 9, 13: 10, 14: 11, 15: 12, 16: 13, 17: 14, 18: 15, 19: 16, 20: 17, 21: 18, 22: 19, 23: 20, 24: 21, 25: 22, 26: 23, 1: 24, 2: 25, 3: 26}))
def test_reflector_4(self): reflector = Reflector(EnigmaSettings.get_reflectors()['A']) reflector.set_position('D') self.assertEqual(reflector.pins_to_ring_settings, dict({1: 4, 2: 5, 3: 6, 4: 7, 5: 8, 6: 9, 7: 10, 8: 11, 9: 12, 10: 13, 11: 14, 12: 15, 13: 16, 14: 17, 15: 18, 16: 19, 17: 20, 18: 21, 19: 22, 20: 23, 21: 24, 22: 25, 23: 26, 24: 1, 25: 2, 26: 3}))