Python Forum
Enigma Machine, I need help!
Thread Rating:
  • 3 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Enigma Machine, I need help!
#11
To keep you up to date, here is what I have written so far.. But still cannot manage to find where the error lies... :(

I hope you can help!

from tkinter import *

#create window
Start = Tk()
Start.title("Enigma Machine")
Start.geometry("400x360+500+200")
Start.resizable(width=False, height=False)

def base():
    """Machine module opening a window with Tk inter to recreate the
    Enigma Machine which was used in WWII by the german army."""
    global PAD1,PAD2,PAD3,text,counter,ecounter,enclist
    Rotors = Frame(bg='dark grey')
    Rotors.pack(side=TOP)

    #in these Frames we will pack the keys for each key of the Keyboard
    Keyrow1 = Frame(pady=5)
    Keyrow1.pack(side=TOP)
    Keyrow2 = Frame(pady=3)
    Keyrow2.pack(side=TOP)
    Keyrow3 = Frame()
    Keyrow3.pack(side=TOP)
    Keyrow4 = Frame(pady=10)
    Keyrow4.pack(side=TOP)

    #this will be the first text field, containing the entered message
    NONEncryptF = Frame()
    NONEncryptF.pack(side=TOP)
    #This Frame contains the second text field with the encripted message
    EncryptF = Frame()
    EncryptF.pack(side=TOP)

    #First text field, M being Message, T for text and L for label
    NONEncryptM = "Inputted Text will be written here."
    NONEncryptT = StringVar()
    NONEncryptL = Label(NONEncryptF,width=50,height=4,textvariable=NONEncryptT,bg="black",fg="white")
    NONEncryptL.pack()
    NONEncryptT.set(str(NONEncryptM))

    #same is done for the encripted text box
    EncryptM = "Encrypted Text will be written here."
    EncryptT = StringVar()
    EncryptL = Label(EncryptF,width=50,height=4,textvariable=EncryptT,bg="grey55",fg="black")
    EncryptL.pack()
    EncryptT.set(str(EncryptM))

    PAD1=1          #these are the three rotors, counters for us
    PAD2=1
    PAD3=1

    counter = 0          #these both counters add a paragraph if the line exceeds 40 characters (see later)
    ecounter = 0

    #Keyboard
    LR1 = ["q","w","e","r","t","z","u","i","o","p"]
    LR2 = ["a","s","d","f","g","h","j","k","l"]
    LR3 = ["y","x","c","v","b","n","m",".",]
    LR4 = ["space"]

    text=[]         #these lists will be the containers for the both texts
    enclist = []

    #First Part: having the GUI of the machine work
    def GUI():
        def ROTORMOVER():
            """for each pressed key, we will augment the counter,
            counter moves back to 0 if it reaches 26,
            moving the one to its right"""
            global PAD1,PAD2,PAD3,counter,ecounter
            PAD3+=1
            if PAD3>26:
                PAD3=1
                PAD2+=1
                if PAD2>26:
                    PAD2=1
                    PAD1+=1
                    if PAD1>26:PAD1=1
            #increases counters
            counter+=1
            ecounter+=1
            ROTOR3.set(str(PAD3))
            ROTOR2.set(str(PAD2))
            ROTOR1.set(str(PAD1))

        def printer(l):
            """this pastes the normal, inputted text in the Message"""
            global text,NONEncryptM,counter
            ROTORMOVER()
            text+=[l]
            if counter==40:         # if characters exceed 40, we append a paragraph
                text.append("\n")
                counter=0
            NONEncryptM = "".join(text)
            NONEncryptT.set(str(NONEncryptM))
            encrypt()            #we launch encrypt() to encript the message that was just given

        #We create Labels and Stringvars for each row of keys in the keyboard
        ROTOR1 = StringVar()
        ROTOR2 = StringVar()
        ROTOR3 = StringVar()
        LabelPad1 = Label(Rotors, textvariable = ROTOR1 , bg ="grey")
        ROTOR1.set(str(PAD1))
        LabelPad1.pack(padx=5,pady=10,side=LEFT)
        LabelPad2 = Label(Rotors,textvariable = ROTOR2, bg = "grey")
        ROTOR2.set(str(PAD2))
        LabelPad2.pack(padx=5,pady=10,side=LEFT)
        LabelPad3 = Label(Rotors, textvariable = ROTOR3 , bg ="grey")
        ROTOR3.set(str(PAD3))
        LabelPad3.pack(padx=5,pady=10)

        for char in LR1:        #creating the keyboard
            Button(Keyrow1,text=char,width=3,pady=5,command=lambda q=char:printer(q)).pack(side=LEFT)
        for char in LR2:
            Button(Keyrow2,text=char,width=3,pady=5,command=lambda q=char:printer(q)).pack(side=LEFT)
        for char in LR3:
            Button(Keyrow3,text=char,width=3,pady=5,command=lambda q=char:printer(q)).pack(side=LEFT)
        Button(Keyrow4,text="Space",width=3,padx=85,pady=5,command=lambda:printer(" ")).pack()
    GUI()    #Executing code above

    def encrypt():
        """In this second section, we take what was written in the text field and let it "pass through" the rotors"""
        global EncryptM,text,PAD3,PAD2,PAD1,enclist,c,d,e,ENCMESS,x,ecounter,Rotor1,Rotor2,Rotor3,Mirror
        char = ["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"," "]
        c = PAD3-2          #c,d,e are variables that act as counters: for each pressed key they increase, meaning pressing twice 'a' will not exit the same key
        if c==-1:c=26
        d = PAD2-1
        e = PAD1-1
        ENCMESS=0

        #The modifications for the 3 rotors and mirror, I did not insert a plugboard yet in the program
        Rotor1 = [7,1,13,23,6,17,14,10,26,2,5,19,12,18,21,3,25,9,15,24,4,11,20,8,22,16]
        Rotor2 = [19,17,6,3,25,16,7,11,23,4,1,20,9,5,26,22,14,2,18,10,24,15,8,12,21,13]
        Rotor3 = [24,13,9,21,10,5,17,2,12,25,4,11,15,18,7,20,1,14,23,6,16,19,22,3,26,8]
        Mirror = [21,7,17,18,11,3,1,23,15,12,8,25,13,16,9,22,2,26,20,14,6,19,24,4,10,5]

        def starter():
            """Converts our last character of the inputted text into a cipher"""
            global ENCMESS,text,x
            x = text[len(text)-1]
            for cha in char:
                ENCMESS+=1
                if x==cha: break

        def coder(seq,rev=0,m=0):
            """Acts as our rotors"""
            global ENCMESS
            if rev==0 or rev==2:
                ENCMESS =(ENCMESS+m)%27
                ENCMESS = seq[ENCMESS-1]
                ENCMESS =(ENCMESS-m)%27
            elif rev==1:
                lst=["empty"]*26
                ENCMESS =(ENCMESS+m)%27
                for i,v in enumerate(seq):
                    lst[v-1] = i
                ENCMESS = lst[ENCMESS-1]
                ENCMESS =(ENCMESS-m)%27

        def letters():
            """Converts the cipher back to a letter"""
            global ENCMESS
            ENCMESS = char[ENCMESS-1]

        starter()
        coder(Rotor1,0,c)
        coder(Rotor2,0,d)
        coder(Rotor3,0,e)
        coder(Mirror,2)
        coder(Rotor3,1,e)
        coder(Rotor2,1,d)
        coder(Rotor1,1,c)
        #appends parapgraph if the characters are over 40
        for l in enclist:
            if ecounter==40:
                enclist.append("\n")
                ecounter=0
        if x==" ": enclist.append(" ")      #If the last charcater (a.k.a. inputted charcter is a space or dot, it inserts respectively space or dot)
        elif x==".":enclist.append(".")
        elif x=="\n":pass
        else:       # If not, it appends our encripted character
            letters()
            enclist.append(ENCMESS)
        EncryptM = "".join(enclist)
        EncryptT.set(str(EncryptM))

base()
Start.mainloop()        #keeps the window open
Reply


Messages In This Thread
Enigma Machine, I need help! - by Kimkuq - Nov-12-2017, 07:15 PM
RE: Enigma Machine, I need help! - by Larz60+ - Nov-12-2017, 10:37 PM
RE: Enigma Machine, I need help! - by AceScottie - Nov-13-2017, 05:00 AM
RE: Enigma Machine, I need help! - by Kimkuq - Nov-13-2017, 06:29 PM
RE: Enigma Machine, I need help! - by Larz60+ - Nov-13-2017, 06:36 PM
RE: Enigma Machine, I need help! - by Kimkuq - Nov-13-2017, 06:42 PM
RE: Enigma Machine, I need help! - by Larz60+ - Nov-13-2017, 06:45 PM
RE: Enigma Machine, I need help! - by Kimkuq - Nov-13-2017, 08:22 PM
RE: Enigma Machine, I need help! - by Larz60+ - Nov-13-2017, 09:27 PM
RE: Enigma Machine, I need help! - by Kimkuq - Nov-13-2017, 10:09 PM
RE: Enigma Machine, I need help! - by Kimkuq - Nov-14-2017, 05:38 PM
RE: Enigma Machine, I need help! - by Larz60+ - Nov-14-2017, 05:47 PM
RE: Enigma Machine, I need help! - by Kimkuq - Nov-14-2017, 06:25 PM
RE: Enigma Machine, I need help! - by Larz60+ - Nov-15-2017, 12:54 AM
RE: Enigma Machine, I need help! - by sparkz_alot - Nov-15-2017, 02:26 PM
RE: Enigma Machine, I need help! - by Larz60+ - Nov-16-2017, 06:08 AM
RE: Enigma Machine, I need help! - by sparkz_alot - Nov-16-2017, 02:02 PM
RE: Enigma Machine, I need help! - by Kimkuq - Nov-16-2017, 09:56 PM
RE: Enigma Machine, I need help! - by Larz60+ - Nov-17-2017, 04:23 AM
RE: Enigma Machine, I need help! - by Larz60+ - Nov-17-2017, 10:08 PM
RE: Enigma Machine, I need help! - by Larz60+ - Nov-18-2017, 01:48 AM
RE: Enigma Machine, I need help! - by Kimkuq - Nov-18-2017, 10:09 PM
RE: Enigma Machine, I need help! - by Larz60+ - Nov-19-2017, 01:40 PM
RE: Enigma Machine, I need help! - by Larz60+ - Nov-19-2017, 10:06 PM
RE: Enigma Machine, I need help! - by Larz60+ - Nov-20-2017, 05:45 PM
RE: Enigma Machine, I need help! - by Kimkuq - Nov-20-2017, 08:28 PM
RE: Enigma Machine, I need help! - by Larz60+ - Nov-21-2017, 01:53 AM
RE: Enigma Machine, I need help! - by sparkz_alot - Nov-21-2017, 02:21 AM
RE: Enigma Machine, I need help! - by Larz60+ - Nov-21-2017, 11:50 AM
RE: Enigma Machine, I need help! - by Larz60+ - Nov-21-2017, 10:36 PM
RE: Enigma Machine, I need help! - by Kimkuq - Nov-22-2017, 09:18 AM
RE: Enigma Machine, I need help! - by sparkz_alot - Nov-21-2017, 11:52 PM
RE: Enigma Machine, I need help! - by Larz60+ - Nov-22-2017, 11:00 AM
RE: Enigma Machine, I need help! - by sparkz_alot - Nov-22-2017, 02:20 PM
RE: Enigma Machine, I need help! - by Larz60+ - Nov-22-2017, 04:24 PM
RE: Enigma Machine, I need help! - by sparkz_alot - Nov-22-2017, 05:12 PM
RE: Enigma Machine, I need help! - by Larz60+ - Nov-22-2017, 08:32 PM
RE: Enigma Machine, I need help! - by Kimkuq - Nov-22-2017, 09:03 PM
RE: Enigma Machine, I need help! - by sparkz_alot - Nov-22-2017, 08:48 PM
RE: Enigma Machine, I need help! - by Larz60+ - Nov-22-2017, 08:51 PM
RE: Enigma Machine, I need help! - by sparkz_alot - Nov-22-2017, 09:01 PM
RE: Enigma Machine, I need help! - by Larz60+ - Nov-22-2017, 09:06 PM
RE: Enigma Machine, I need help! - by sparkz_alot - Nov-22-2017, 09:28 PM
RE: Enigma Machine, I need help! - by Larz60+ - Nov-22-2017, 10:41 PM
RE: Enigma Machine, I need help! - by Larz60+ - Nov-24-2017, 09:18 PM
RE: Enigma Machine, I need help! - by Larz60+ - Nov-24-2017, 11:48 PM
RE: Enigma Machine, I need help! - by Larz60+ - Nov-25-2017, 05:09 AM
RE: Enigma Machine, I need help! - by Larz60+ - Nov-26-2017, 11:31 AM
RE: Enigma Machine, I need help! - by sparkz_alot - Nov-26-2017, 01:56 PM
RE: Enigma Machine, I need help! - by Kimkuq - Nov-26-2017, 09:53 PM
RE: Enigma Machine, I need help! - by Larz60+ - Nov-27-2017, 03:03 AM
RE: Enigma Machine, I need help! - by Larz60+ - Nov-28-2017, 08:29 PM
RE: Enigma Machine, I need help! - by Larz60+ - Nov-29-2017, 01:42 AM
RE: Enigma Machine, I need help! - by sparkz_alot - Dec-02-2017, 02:20 PM
RE: Enigma Machine, I need help! - by Larz60+ - Dec-02-2017, 02:50 PM
RE: Enigma Machine, I need help! - by Larz60+ - Dec-04-2017, 01:49 AM
RE: Enigma Machine, I need help! - by Kimkuq - Dec-06-2017, 09:58 PM
RE: Enigma Machine, I need help! - by Larz60+ - Dec-06-2017, 11:04 PM
RE: Enigma Machine, I need help! - by Larz60+ - Dec-07-2017, 02:08 PM
RE: Enigma Machine, I need help! - by Larz60+ - Dec-08-2017, 11:35 AM
RE: Enigma Machine, I need help! - by Larz60+ - Dec-16-2017, 02:08 PM
RE: Enigma Machine, I need help! - by Larz60+ - Dec-18-2017, 05:53 AM
RE: Enigma Machine, I need help! - by Kimkuq - Dec-18-2017, 09:06 PM
RE: Enigma Machine, I need help! - by Larz60+ - Dec-19-2017, 03:26 AM
RE: Enigma Machine, I need help! - by Larz60+ - Dec-23-2017, 02:22 AM
RE: Enigma Machine, I need help! - by sparkz_alot - Dec-23-2017, 02:19 PM
RE: Enigma Machine, I need help! - by Larz60+ - Dec-27-2017, 08:26 PM
RE: Enigma Machine, I need help! - by sparkz_alot - Dec-27-2017, 09:27 PM
RE: Enigma Machine, I need help! - by Larz60+ - Dec-28-2017, 01:49 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Numeric Enigma Machine idev 9 699 Mar-29-2024, 06:15 PM
Last Post: idev
  Enigma Decoding Problem krisarmstrong 4 833 Dec-14-2023, 10:42 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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