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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
#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() |