def test_codifica_frase(self): rotor1 = enigma.Rotor(abecedario, rotor_types['I'][0], rotor_types['I'][1]) rotor2 = enigma.Rotor(abecedario, rotor_types['I'][0], rotor_types['I'][1]) rotor3 = enigma.Rotor(abecedario, rotor_types['I'][0], rotor_types['I'][1]) reflector = enigma.Reflector(UKW) maquina = enigma.Enigma([rotor1, rotor2, rotor3], reflector=reflector, ini='ZAA') self.assertEqual(maquina.codifica("HOLA"), "LCHD") self.assertEqual(maquina.ini, "DAA")
def test_creaEnigmaErrores(self): with self.assertRaises(AttributeError) as e: rotor = enigma.Rotor(abecedario, rotor_types['I'][0]+'Ñ', rotor_types['I'][1]) rotor = enigma.Rotor(abecedario, rotor_types['I'][0], rotor_types['I'][1]) rotor.ini = 'K' reflector = enigma.Reflector(conf=['ABC', 'CBA']) with self.assertRaises(AttributeError): maquina = enigma.Enigma(rotores=[rotor], reflector=reflector)
def test_codificaOK_con_pos_ini(self): rotor = enigma.Rotor("ABCDEFG", "CFAGBDE") rotor.pos_ini = "C" self.assertEqual(rotor.pos_ini, "C") self.assertEqual(rotor._pos_ini, 2) self.assertEqual(rotor.codifica(0), 5) self.assertEqual(rotor.decodifica(4), 2)
def test_creaEnigmaOk(self): rotor = enigma.Rotor(abecedario, rotor_types['I'][0], rotor_types['I'][1]) rotor.ini = 'K' reflector = enigma.Reflector(conf=UKW) maquina = enigma.Enigma(rotores=[rotor], reflector=reflector) self.assertEqual(maquina.ini, "A") self.assertEqual(rotor.pos_ini, "A")
def test_cambia_ini(self): rotor = enigma.Rotor(abecedario, rotor_types['I'][0], rotor_types['I'][1]) rotor.ini = 'K' reflector = enigma.Reflector(conf=UKW) maquina = enigma.Enigma(rotores=[rotor], reflector=reflector, ini="C") self.assertEqual(maquina.ini, "C") self.assertEqual(maquina.rotores[0].pos_ini, "C") maquina.ini = "T" self.assertEqual(maquina.ini, "T") self.assertEqual(maquina.rotores[0].pos_ini, "T")
def test_RotorWiring(rotor_n, rings, pos, fwd_in, rev_in, fwd_out, rev_out): rotor = enigma.Rotor(rotor_n, rings, pos) if rotor.produceLetterOutput( fwd_in) == fwd_out and rotor.produceReverseOutput( rev_in) == rev_out: print('Test for rotor PASS') else: print('Test for rotor FAIL') print('## CORRECT OUTPUT\n' + fwd_out + '\n' + rev_out + '\n## ACTUAL OUTPUT') print(rotor.produceLetterOutput(fwd_in)) print(rotor.produceReverseOutput(fwd_out))
def test_rotor(self): rotor = enigma.Rotor(n_positions=10, seed=42) rotor.set_position(9) carryover = rotor.rotate_return_carryover(1) self.assertEqual(rotor.position, 0) self.assertEqual(carryover, 1) rotor.position = 6 in_ = 7 out = rotor.get_permuted_output_forward(in_) self.assertEqual(rotor.get_permuted_output_backward(out), in_) rotor.rotate_return_carryover(1) out2 = rotor.get_permuted_output_forward(in_) self.assertNotEqual(out, out2)
def setup_enigma_and_msg(self, len_msg, n_rotors, n_plugs): self.charset = string.ascii_lowercase self.n_chars = len(self.charset) self.reflector = enigma.Swapper(n_positions=self.n_chars) self.reflector.assign_random_swaps(n_swaps=self.n_chars // 2, seed=3) rotor_seeds = list(range(n_rotors)) self.rotors = [ enigma.Rotor(n_positions=self.n_chars, seed=seed) for seed in rotor_seeds ] self.rotor_positions = n_rotors * [15] self.plugboard = enigma.Swapper(n_positions=self.n_chars) self.plugboard.assign_random_swaps(n_swaps=n_plugs, seed=41) message = ( "The Enigma machine is a cipher device developed and used in the early- to mid-20th century to protect" " commercial, diplomatic, and military communication. It was employed extensively by Nazi Germany during " "World War II, in all branches of the German military. The Germans believed, erroneously, that use of the" " Enigma machine enabled them to communicate securely and thus enjoy a huge advantage in World War II. " "The Enigma machine was considered to be so secure that even the most top-secret messages were enciphered" " on its electrical circuits. Enigma has an electromechanical rotor mechanism that scrambles the 26 letters " "of the alphabet. In typical use, one person enters text on the Enigma's keyboard and another person writes" " down which of 26 lights above the keyboard lights up at each key press. If plain text is entered, the " "lit-up letters are the encoded ciphertext. Entering ciphertext transforms it back into readable plaintext. " "The rotor mechanism changes the electrical connections between the keys and the lights with each keypress. " "The security of the system depends on a set of machine settings that were generally changed daily during the " "war, based on secret key lists distributed in advance, and on other settings that were changed for " "each message. The receiving station has to know and use the exact settings employed by the transmitting " "station to successfully decrypt a message." ) message = message.lower() self.message_full = "".join(c for c in message if c.islower()) encoder = enigma.Enigma( self.rotors, self.plugboard, self.reflector, charset=self.charset ) encoder.set_rotor_positions(self.rotor_positions) self.message = self.message_full[:len_msg] self.encrypted_message = encoder.encode_message(self.message) # reset all rotors so MC does not start with a good value encoder.set_rotor_positions(n_rotors * [0])
def test_encrypt_decrypt(self): plugboard = enigma.Swapper(n_positions=self.n_chars) plugboard.assign_random_swaps(n_swaps=10, seed=41) rotor_seeds = [21, 32, 34] rotors = [ enigma.Rotor(n_positions=self.n_chars, seed=seed) for seed in rotor_seeds ] reflector = enigma.Swapper(n_positions=self.n_chars) reflector.assign_random_swaps(n_swaps=self.n_chars // 2, seed=3) encoder = enigma.Enigma(rotors, plugboard, reflector, charset=self.charset) rotor_positions = [3, 4, 7] encoder.set_rotor_positions(rotor_positions) encoded_message = encoder.encode_message(self.test_message) self.assertNotEqual(encoded_message, self.test_message) encoder.set_rotor_positions(rotor_positions) decoded_message = encoder.encode_message(encoded_message) self.assertEqual(decoded_message, self.test_message)
def test_codificaOK(self): rotor = enigma.Rotor("ABCDEFG", "CFAGBDE") self.assertEqual(rotor.codifica(0), 2) self.assertEqual(rotor.decodifica(4), 1)
def test_construyeOK(self): rotor = enigma.Rotor("ABCDEFG", "CFAGBDE") self.assertEqual(rotor.abecedario, "ABCDEFG") self.assertEqual(rotor.cortocircuito, "CFAGBDE") self.assertEqual(rotor.pos_ini, "A")
import enigma machine = enigma.Enigma(enigma.Plugboard.STATIC_WIRING, enigma.Rotor(1, 'A', 'A'), enigma.Rotor(2, 'A', 'A'), enigma.Rotor(3, 'A', 'A'), enigma.Reflector('REFLECTOR_B')) def test_RotorWiring(rotor_n, rings, pos, fwd_in, rev_in, fwd_out, rev_out): rotor = enigma.Rotor(rotor_n, rings, pos) if rotor.produceLetterOutput( fwd_in) == fwd_out and rotor.produceReverseOutput( rev_in) == rev_out: print('Test for rotor PASS') else: print('Test for rotor FAIL') print('## CORRECT OUTPUT\n' + fwd_out + '\n' + rev_out + '\n## ACTUAL OUTPUT') print(rotor.produceLetterOutput(fwd_in)) print(rotor.produceReverseOutput(fwd_out)) def test_letterToPosition(rotor, letter, good_out): t = machine.letterToPosition(rotor, letter) if t == good_out: print('Test for letterToPosition PASS') else: print('Test for letterToPosition FAIL') print('## CORRECT OUTPUT\n' + str(good_out) + '\n## ACTUAL OUTPUT') print(t)
def test_codifica(self): rotor = enigma.Rotor() self.assertEqual(rotor.conexion(0, ["ABCD", "CABD"]), 1) self.assertEqual(rotor.conexion_decodifica(2, ["ABCD", "CABD"]), 1)
def test_construye(self): rotor = enigma.Rotor() c = [] self.assertEqual( rotor.crea_rotor(c), ['LQRWNZÑEPBXJVKFYHUMAOCDSTGI', 'VPZIDBNCGTHÑQJLORUAEKFSXYMW'])
import time import string import random import tqdm import enigma n_messages = 3000 chars_per_message = 256 charset = string.ascii_uppercase n_chars = len(charset) plugboard = enigma.Swapper(n_positions=n_chars, n_swaps=10, seed=41) rotor_seeds = [21, 32, 34] rotors = [enigma.Rotor(n_positions=n_chars, seed=seed) for seed in rotor_seeds] reflector = enigma.Swapper(n_positions=n_chars, n_swaps=n_chars // 2, seed=3) encoder = enigma.Enigma(rotors, plugboard, reflector, charset=charset) rotor_positions = [3, 4, 7] messages = [ "".join(random.choices(charset, k=chars_per_message)) for _ in range(n_messages) ] tick = time.time() for message in tqdm.tqdm(messages): encoder.set_rotor_positions(rotor_positions) encoded_message = encoder.encode_message(message) tock = time.time() avg_time = (tock - tick) / n_messages