clean = [] for m in messages: swap = m.upper().replace(' ', '') current = '' for c in swap: if c in letters: current += c clean.append(current) messages = clean settings = [[4, 3, 2, 0, 13, 10, 24], [1, 3, 0, 1, 7, 10, 20], [0, 3, 4, 0, 8, 25, 24], [1, 2, 4, 1, 2, 20, 12], [1, 3, 2, 0, 0, 10, 18], [1, 3, 2, 1, 8, 13, 20], [0, 1, 4, 0, 8, 25, 19], [1, 2, 0, 0, 2, 20, 8]] recipients = [] #Make this a list of strings of recipients email addresses count = 0 for set in settings: machine = enigma.enigma() machine.setLeft(enigma.rotors[set[0]], set[4]) machine.setMiddle(enigma.rotors[set[1]], set[5]) machine.setRight(enigma.rotors[set[2]], set[6]) machine.setReflector(enigma.reflectors[set[3]]) plug = enigma.plugboard() machine.setPlugboard(plug) cipher = machine.operate(messages[count]) composeGeneral(cipher, recipients) time.sleep(20 * 60) count += 1 #print(enigma.reflectors[set[3]])
#We need to get all size 3 ordered sublists from our list of rotors letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' import itertools def allRotorSettings(S, m): mylist = [] for l in itertools.combinations(S, m): mylist.append(list(l)) rotorConfigs = [] for conf in mylist: permutes = itertools.permutations(conf) for p in permutes: rotorConfigs.append(list(p)) return rotorConfigs rotorConfigs = allRotorSettings( [0, 1, 2, 3, 4], 3) #this is a list of all 60 possible rotor configurations (5*4*3) for i in rotorConfigs: machine = enigma.enigma() machine.setLeft(enigma.rotors[0], i[0]) machine.setMiddle(enigma.rotors[1], i[1]) machine.setRight(enigma.rotors[2], i[2]) machine.setReflector(enigma.reflectors[0]) machine.setPlugboard(enigma.plugboard()) print(rotorConfigs)
#Copyright (c) 2018 Thomas Norell import enigma letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' machine = enigma.enigma() #Enigma Machine Constructors machine.setLeft(enigma.rotors[0], 0) #Set Left Rotor as Rotor I, Enigma object comes with a list of rotor objects machine.setMiddle(enigma.rotors[1], 0) #Set Middle Rotor as Rotor II machine.setRight(enigma.rotors[2], 0) #Set Right Rotor as Rotor III machine.setReflector(enigma.reflectors[0]) #Set Reflector as reflector C (We only have B & C) (object comes with list of reflectors) (reflectors are implemented as just a special case of rotors) machine.setPlugboard(enigma.plugboard()) text = 'Hello this is an example message to see if my program works properly'.upper().replace(' ', '') #create text to encrypt cipher = machine.operate(text) #The operate method encrypts a string. Keep in mind that Enigma's are mutable, so after encryption the rotors are no longer in the initial configuration print('Ciphertext: ' + cipher[:100]) #Let's reset the machine and send our ciphertext back through machine.setLeft(enigma.rotors[0], 0) machine.setMiddle(enigma.rotors[1], 0) machine.setRight(enigma.rotors[2], 0) machine.setReflector(enigma.reflectors[0]) machine.setPlugboard(enigma.plugboard()) b = machine.operate(cipher) print('Decoded Plaintext: ' + b[:100]) print('Verified: ' + str(b == text)) #Validate successful decryption ###################################### # Now an example using the plugboard # ###################################### letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' machine = enigma.enigma() machine.setLeft(enigma.rotors[3], 13) machine.setMiddle(enigma.rotors[1], 15)