Python Forum
Enigma Program not working properly
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Enigma Program not working properly
#1
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:

#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()
Reply
#2
So, where exactly are you going wrong?
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply
#3
main already has print statements, ans is called at end of script.
Reply
#4
(May-01-2020, 10:35 AM)Larz60+ Wrote: main already has print statements, ans is called at end of script.
I saw that, and changed the post.
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] Can’t get program to run properly KingC 2 2,320 Dec-10-2019, 12:20 AM
Last Post: micseydel
  Printing a number sequence not working properly deepsen 6 2,947 Oct-12-2019, 07:43 PM
Last Post: deepsen
  Supposedly simple program not working! Please help! BenjaminDJ 5 3,999 Feb-20-2018, 08:43 PM
Last Post: BenjaminDJ
  Find not working properly fad3r 2 2,440 Feb-11-2018, 03:18 PM
Last Post: fad3r
  Simple Python program not working AudioKev 3 3,170 Jan-23-2018, 09:57 PM
Last Post: Larz60+
  GTIN code validation program not working! AnonDxZ 1 2,760 Nov-29-2017, 11:25 PM
Last Post: Prrz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020