Introduction
This project is aimed at making a Simple Message Encrypter using Python Tkinter. The application will be using a simple Symmetric Key algorithm. Symmetric key algorithms are algorithms for which we use the same cryptographic keys for both encryption of plaintext and decryption of ciphertext (ie. we should use the same password, which the sender used at the time of encryption, for retrieving the message back to its original form).
Pre-requisites
- Basic knowledge of python
- Understanding of tkinter
Project Demo Image
Final Output
Programming
import tkinter root = tkinter.Tk() root.title("Message Encrypter") root.iconbitmap("resources/icons/encrypter.ico") root.mainloop()
- Import the Tkinter module, for making the GUI for the application. All we need to do, inorder to use Tkinter is a single import statement
- initialize an instance of tkinter named "root". You can use your own specific names for this.
- specify a title for our "root" window.
- specify the icon. This icon will appear on the top left corner of our application. Even if you didn't specify anything, tkinter will give a default icon.
- tkinter works by continuously looping through the code, which is specified by the mainloop(). If you forget to mention this line, your program will run without any error. But you will not be able to see it, since it will execute and close, within a fraction of second. If you run the code(at this instance), the application window will look like this. You can see the title of the application and the icon.
#Defining frames input_frame = tkinter.Frame(root) button_frame = tkinter.Frame(root,bg=button_frame_color) input_frame.pack() button_frame.pack(fill=BOTH,expand=True) #Defining widgets for frames input_data = tkinter.Entry(input_frame,width=80) output_data = tkinter.Label(input_frame,text="Your output will appear here",bg=output_bg,fg=button_color,width=50,wraplength=200) copy_button = tkinter.Button(input_frame,text="Copy",bg=button_color) passwd_label = tkinter.Button(button_frame,text="Password",bg=button_frame_color,borderwidth=0) passwd_data = tkinter.Entry(button_frame) encrypt_button = tkinter.Button(button_frame,text="Encrypt",bg=button_color) decrypt_button = tkinter.Button(button_frame,text="Decrypt",bg=button_color) input_data.grid(row=0,column=0,columnspan=2,padx=10,pady=10,ipady=50) output_data.grid(row=1,column=0,padx=10,pady=10,ipady=40,sticky="we") copy_button.grid(row=1,column=1,pady=10,ipady=10,ipadx=20,padx=(0,10)) passwd_label.grid(row=0,column=0,pady=10,padx=(100,0),ipadx=20,ipady=10) passwd_data.grid(row=0,column=1,pady=10,padx=(0,10),ipadx=50,ipady=10) encrypt_button.grid(row=1,column=0,pady=10,padx=(80,0),ipadx=40,ipady=10) decrypt_button.grid(row=1,column=1,pady=20,ipadx=40,ipady=10)
- Define two Frames for the application
- Place it on to the root window, using the pack() method.
- Input frame will have message entry region, output region and a copy button.
- Define an Entry widget (named "input_data") to get the message from user.
- Place it on the input frame using grid().
- Define a display region, for outputting the result after processing, using a Label widget from tkinter. Wraplength specifies the length of the line after which a new line character is to be inserted.
- Place a button widget for copying the content after encrypting/decrypting. Th efunction to be performed while clicking the button will be described below.
- Button frame will have two buttons : one for encrypting and other for decrypting the message, along with an entry widget for getting the password from the user.
expected_char="qazwsxedcrfvtgbyhnujmikolp!@#$%^&*()1234567890POIUYTREWQASDFGHJKLMNBVCXZ~`<,>.?/:;'[{]}+=_-| " expected_char+='"'
def encrypt():
cipher_text=''
plain_text=input_data.get()
if passwd_data.get()!='':
passwd=int(passwd_data.get())
for i in plain_text:
index=expected_char.find(i)
updated_index=(index+passwd)%94
cipher_text+=expected_char[updated_index]
output_data.config(text=cipher_text)
#input_data.delete(0,END)
else:
messagebox.showerror("Invalid Password","You must enter a password")
def decrypt():
plain_text=''
cipher_text=input_data.get()
if passwd_data.get()!='':
depasswd=int(passwd_data.get())
for i in cipher_text:
new_index=expected_char.find(i)
new_updated_index=(new_index-depasswd)%94
plain_text+=expected_char[new_updated_index]
output_data.config(text=plain_text)
#input_data.delete(0,END)
else:
messagebox.showerror("Invalid Password","You must enter a password inorder to decrypt the message")
def copy_data(): pyperclip.copy(output_data['text'])
Complete Source Code
''' program: Simple Message Encrypter author : akr github : a-k-r-a-k-r ''' #import necessary modules import tkinter import pyperclip from tkinter import DISABLED,messagebox,BOTH,END #Defining root window root=tkinter.Tk() root.title("Message Encrypter") root.iconbitmap("resources/icons/encrypter.ico") #mostly used characters expected_char="qazwsxedcrfvtgbyhnujmikolp!@#$%^&*()1234567890POIUYTREWQASDFGHJKLMNBVCXZ~`<,>.?/:;'[{]}+=_-| " expected_char+='"' #colors button_frame_color="grey" button_color="green" output_bg="black" #Defining functions def encrypt(): cipher_text='' plain_text=input_data.get() if passwd_data.get()!='': passwd=int(passwd_data.get()) for i in plain_text: index=expected_char.find(i) updated_index=(index+passwd)%94 cipher_text+=expected_char[updated_index] output_data.config(text=cipher_text) #input_data.delete(0,END) else: messagebox.showerror("Invalid Password","You must enter a password") def decrypt(): plain_text='' cipher_text=input_data.get() if passwd_data.get()!='': depasswd=int(passwd_data.get()) for i in cipher_text: new_index=expected_char.find(i) new_updated_index=(new_index-depasswd)%94 plain_text+=expected_char[new_updated_index] output_data.config(text=plain_text) #input_data.delete(0,END) else: messagebox.showerror("Invalid Password","You must enter a password inorder to decrypt the message") def copy_data(): pyperclip.copy(output_data['text']) #Defining frames input_frame=tkinter.Frame(root) button_frame=tkinter.Frame(root,bg=button_frame_color) input_frame.pack() button_frame.pack(fill=BOTH,expand=True) #Defining widgets for frames input_data=tkinter.Entry(input_frame,width=80) output_data=tkinter.Label(input_frame,text="Your output will appear here",bg=output_bg,fg=button_color,width=50,wraplength=200) copy_button=tkinter.Button(input_frame,text="Copy",bg=button_color,command=copy_data) passwd_label=tkinter.Button(button_frame,text="Password",bg=button_frame_color,borderwidth=0) passwd_data=tkinter.Entry(button_frame) encrypt_button=tkinter.Button(button_frame,text="Encrypt",bg=button_color,command=encrypt) decrypt_button=tkinter.Button(button_frame,text="Decrypt",bg=button_color,command=decrypt) input_data.grid(row=0,column=0,columnspan=2,padx=10,pady=10,ipady=50) output_data.grid(row=1,column=0,padx=10,pady=10,ipady=40,sticky="we") copy_button.grid(row=1,column=1,pady=10,ipady=10,ipadx=20,padx=(0,10)) passwd_label.grid(row=0,column=0,pady=10,padx=(100,0),ipadx=20,ipady=10) passwd_data.grid(row=0,column=1,pady=10,padx=(0,10),ipadx=50,ipady=10) encrypt_button.grid(row=1,column=0,pady=10,padx=(80,0),ipadx=40,ipady=10) decrypt_button.grid(row=1,column=1,pady=20,ipadx=40,ipady=10) root.mainloop()
Stand-alone Executable
pip install auto-py-to-exe
- Click on the "Browse" button and choose the ".py" file for the program.
- Choose "One Directory". You can also choose to go with "One File". But in most cases, it will give errors.
- Choose to create Window Based instead of Console based
- In the Icon field, browse the ".ico" file (this will appear on the exe file as the icon)
- Add the Additional files. Since we have our icon in the resources folder, add the folder, using "Add Folder" button. This icon will appear on the application window's top left corner.
- Now click on convert
- Once complete, open the output folder and find the ".exe" file.
- Make a shortcut of it and send it to desktop, for easy access.