Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Can you please fix the rotation cipher below, so that it encrypts and decrypts. and displays in the message box. from tkinter import * from
Can you please fix the rotation cipher below, so that it encrypts and decrypts. and displays in the message box.
from tkinter import * from tkinter import messagebox from tkinter import ttk root = Tk() root.geometry('500x500') # declares the size of the form root.title("Two Integer Rotation Cipher") # variable to store all the alphabets letters = 'abcdefghijklmnopqrstuvwxyz' def encrypt(text, shift): # variable to store the encrypted text enc = '' # for every word in encrypted text for word in text.split(): # for every character in the word for c in word: # if character is in letters if c in letters: # finding the index of 'c' in letters i = letters.find(c) # adding the number of shifts from the index i = i + 2 + int(shift) # adds 2 to initial rotation = 3 if i > 25: i = i - len(letters) # finding the corresponding characters for the index in letters and stored in enc(encryption) enc += letters[i] else: enc += c enc += ' ' #textAns.delete(1.0, 'end') textAns.insert(0, END) def decrypt(text, shift): # variable to store the decrypted text dec = '' # for every word in decrypted text for word in text.split(): # for every character in the word for a in word: # changes the rotation integer # if character is in letters if a in letters: # finding the index of 'c' in letters i = letters.find(a) # subtracting the number of shifts from the index i = i - int(shift) if i < 0: i = i + len(letters) # finding the corresponding characters for the index in letters and stored in dec(decryption) dec += letters[i] else: dec += a dec += ' ' textAns.delete(1.0, 'end') textAns.insert(0, dec) def Convert(): if c.get() == "Select Choice": tkMessageBox.showinfo("Error", "Please select the Choice") else: try: r = int(entry_2.get()) if c.get() == "Encrypt a message": encrypt(entry_1.get(), r) elif c.get() == "Decrypt a message": encrypt(entry_1.get(), r) except ValueError: # Handle the exception tkMessageBox.showinfo("Error", "Invalid input. Rotation step should be an integer") label_1 = Label(root, text="Enter Text", width=20, font=("bold", 10)) label_1.place(x=10, y=30) entry_1 = Entry(root, width=50) # adding the entry entry_1.place(x=150, y=30) label_2 = Label(root, text="Select Choice", width=20, font=("bold", 10)) label_2.place(x=10, y=70) # adding the drop down list list1 = ['Encrypt a message', 'Decrypt a message'] c = StringVar() droplist = OptionMenu(root, c, *list1) droplist.config(width=15) c.set('Select Choice') droplist.place(x=150, y=70) label_3 = Label(root, text="Size of Rotation", width=20, font=("bold", 10)) label_3.place(x=10, y=120) entry_2 = Entry(root, width=10) # adding the entry entry_2.place(x=150, y=120) Button(root, text='Click', width=20, bg='brown', fg='white', command=Convert).place(x=180, y=180) label_3 = Label(root, text="Answer: ", width=20, font=("bold", 10)) label_3.place(x=10, y=220) textAns = Text(root, height=10, width=37) textAns.place(x=150, y=220) root.mainloop()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started