Hi Everyone,
I made a program for my computer programming final that acts like the German machine "enigma" however I cannot get it to encode things properly so that they are decoded back to the original message. This is an intro to computer programming class so any help in the simplest ways possible would be great. Thanks so much!
Here's the code:
I made a program for my computer programming final that acts like the German machine "enigma" however I cannot get it to encode things properly so that they are decoded back to the original message. This is an intro to computer programming class so any help in the simplest ways possible would be great. Thanks so much!
Here's the code:
#get input from user def main(): print("Welcome to Enigma") plugboard = input("Enter the plugboard settings (Format: WX YZ): ") ringSettings = input("Enter the ring setting (3 letters): ") plaintext = input("Enter the sentence to encode or decode: ") outcome = encode(plaintext, ringSettings, plugboard) print(outcome) #rotating the rotors to the correct starting place def start_rotors(stop, rotorlist, alphabet): i = rotorlist[0] while i != stop: rotorlist.append(rotorlist.pop(0)) alphabet.append(alphabet.pop(0)) i = rotorlist[0] return rotorlist def list_plugs(plugset): # make into a list plugset = plugset.upper() plugset = plugset.split() # double and reverse the pairs so that they are encoded both ways plugset += [x[::-1] for x in plugset] return plugset def sub(plugboard, plaintext): plain_sub = [] plaintext = plaintext.upper() plain_edit = plaintext #for each pair, separate the first and second character for pair in range(0, len(plugboard)): char = plugboard[pair][0] switch = plugboard[pair][1] #switch each character in the plaintext that matches 'char' with 'switch for i in range(0, len(plaintext)): if plaintext[i] == char: plain_edit = plain_edit.replace(char, switch) return plain_edit def reflector(reflect_dict, rotors1): reflect = reflect_dict[rotors1] return reflect def encode(plaintext, ringSettings, plugboard): # Rotor 3 Substitution rotor3 = ['B', 'D', 'F', 'H', 'J', 'L', 'C', 'P', 'R', 'T', 'X', 'V', 'Z', 'N', 'Y', 'E', 'I', 'W', 'G', 'A', 'K', 'M', 'U', 'S', 'Q', 'O'] rotor3_alpha = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'] rotor3Notch = "V" # Rotor 2 Substitution rotor2 = ['A', 'J', 'D', 'K', 'S', 'I', 'R', 'U', 'X', 'B', 'L', 'H', 'W', 'T', 'M', 'C', 'Q', 'G', 'Z', 'N', 'P', 'Y', 'F', 'V', 'O', 'E'] rotor2_alpha = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'] rotor2Notch = "E" # Rotor 1 Substitution rotor1 = ['E', 'K', 'M', 'F', 'L', 'G', 'D', 'Q', 'V', 'Z', 'N', 'T', 'O', 'W', 'Y', 'H', 'X', 'U', 'S', 'P', 'A', 'I', 'B', 'R', 'C', 'J'] rotor1_alpha = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'] rotor1Notch = "Q" # Reflector reflect_dict = {'A': 'Y', 'B': 'R', 'C': 'U', 'D': 'H', 'E': 'Q', 'F': 'S', 'G': 'L', 'H': 'D', 'I': 'P', 'J': 'X', 'K': 'N', 'L': 'G', 'M': 'O', 'N': 'K', 'O': 'M', 'P': 'I', 'Q': 'E', 'R': 'B', 'S': 'F', 'T': 'Z', 'U': 'C', 'V': 'W', 'W': 'V', 'X': 'J', 'Y': 'A', 'Z': 'T'} alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" #rotor1_start = ringPositions[0] #rotor2_start = ringPositions[1] #rotor3_start = ringPositions[2] ringSettings = ringSettings.upper() rotor1_start = ringSettings[0] rotor2_start = ringSettings[1] rotor3_start = ringSettings[2] rotor1 = start_rotors(rotor1_start, rotor1, rotor1_alpha) rotor2 = start_rotors(rotor2_start, rotor2, rotor2_alpha) rotor3 = start_rotors(rotor3_start, rotor3, rotor3_alpha) plugboard = list_plugs(plugboard) plaintext = sub(plugboard, plaintext) rotors_output = [] count3 = 0 count2 = 0 for i in range(0, len(plaintext)): #dont try to encode the spaces or other characters if plaintext[i] in rotor1_alpha: rotor3.append(rotor3.pop(0)) rotor3_alpha.append(rotor3_alpha.pop(0)) if plaintext[i] == rotor3Notch: rotor3.append(rotor3.pop(0)) rotor3_alpha.append(rotor3_alpha.pop(0)) if plaintext[i] == rotor2Notch: rotor2.append(rotor2.pop(0)) rotor2_alpha.append(rotor2_alpha.pop(0)) if plaintext[i] == rotor1Notch: rotor1.append(rotor1.pop(0)) rotor1_alpha.append(rotor1_alpha.pop(0)) rotor2.append(rotor2.pop(0)) rotor2_alpha.append(rotor2_alpha.pop(0)) place = rotor3_alpha.index(plaintext[i]) rotors3value = rotor3[place] place = rotor2_alpha.index(rotors3value) rotors2value = rotor2[place] place = rotor1_alpha.index(rotors2value) rotors1 = rotor1[place] # reflect the letter to change it and then put it back through the rotors reflected = reflector(reflect_dict, rotors1) place = rotor1.index(reflected) rotors1 = rotor1_alpha[place] place = rotor2.index(rotors1) rotors2 = rotor2_alpha[place] place = rotor1.index(rotors2) rotors3 = rotor3_alpha[place] rotors_output.append(rotors3) rotors_output = ''.join(rotors_output) rotors_output = sub(plugboard, rotors_output) return rotors_output def list_plugs(plugset): # make into a list plugset = plugset.upper() plugset = plugset.split() # double and reverse the pairs so that they are encoded both ways plugset += [x[::-1] for x in plugset] return plugset main()