Encode and Decode Messages Using Tkinter Module

by | Mar 28, 2022 | Coding, Python

Introduction of the Project

Do you like cryptography? Encryption – decryption techniques make you feel worth digging deeper into? If yes, today, we will implement the Caesar Cipher to encrypt and decrypt the message along with some GUI using the Tkinter module. To encode and decode messages using the Tkinter module, let us know a bit about the Caesar cipher before implementing it.

Caesar cipher is based on shifting the alphabets by a fixed number known as the key to get an encrypted or, say, encoded message and reverse it to get decrypted or decoded message. Some terminologies:

  • Original message = PlainText = Decrypted/ Decoded message
  • Cyphertext = Encrypted/Encoded message
  • A numeric key is used to convert from first to second and vice versa.

 

Requirements

1. Python 3.9 interpreter, you can use VSCode or any python IDE.

NOTE: A certain file needs to be preinstalled for the code’s smooth running, due to which IDLE or IDE is a preferred choice.

2. Pre-installed Tkinter module to create GUI.

Steps To Encode And Decode Messages With Python Using Tkinter Module

Step 1: Do the following as per your Operating System:

Windows: Open Command Prompt and type

macOS: Open Terminal and type

pip3 install tk

Installing Tkinter

Step 2: Paste the below piece of code in your editor/IDE.

Source Code

# Import the module for GUI

import tkinter as tk




# Initialising the font type and size

FONT = ("timesnewroman", 25, "bold")







class CaesarCypherGUI:

    def __init__(self, master):




        # Title for the window

        master.title("Caesar Cypher GUI")




        # String type for plaintext and cyphertext

        self.plaintext = tk.StringVar(master, value="")

        self.cyphertext = tk.StringVar(master, value="")




        # Integer value for the key

        self.key = tk.IntVar(master)




        # Plaintext GUI and controls

        self.plain_label = tk.Label(master, text="Plaintext", fg="green", font=FONT).grid(row=0, column=0)

        self.plain_entry = tk.Entry(master, textvariable=self.plaintext, width=40, font=FONT)

        self.plain_entry.grid(row=0, column=1, padx=20)




        # Button constructs a button

        self.encrypt_button = tk.Button(master, text="Encrypt", command=lambda: self.encrypt_callback(), font=FONT).grid(row=0, column=2)

        self.plain_clear = tk.Button(master, text="Clear", command=lambda: self.clear('Plain'), font=FONT).grid(row=0, column=3)




        # Key controls

        self.key_label = tk.Label(master, text="Key", font=FONT).grid(row=1, column=0)

        self.key_entry = tk.Entry(master, textvariable=self.key, width=10, font=FONT).grid(row=1, column=1, sticky=tk.W, padx=20)




        # Cyphertext controls

        self.cypher_label = tk.Label(master, text="Cyphertext", fg="red", font=FONT).grid(row=2, column=0)

        self.cypher_entry = tk.Entry(master, textvariable=self.cyphertext, width=40, font=FONT)

        self.cypher_entry.grid(row=2, column=1, padx=20)

        self.decrypt_button = tk.Button(master, text="Decrypt", command=lambda: self.decrypt_callback(), font=FONT).grid(row=2, column=2)

        self.cypher_clear = tk.Button(master, text="Clear", command=lambda: self.clear('Cypher'), font=FONT).grid(row=2, column=3)




    # Defination of clear function

    def clear(self, str_val):

        if str_val == 'cypher':

            self.cypher_entry.delete(0, 'end')

        elif str_val == 'plain':

            self.plain_entry.delete(0, 'end')




    # Defination of key function

    def get_key(self):

        try:

            key_val = self.key.get()

            return key_val

        except tk.TclError:

            pass




    # Encryption method

    def encrypt_callback(self):

        key = self.get_key()

        cyphertext = encrypt(self.plain_entry.get(), key)

        self.cypher_entry.delete(0, tk.END)

        self.cypher_entry.insert(0, cyphertext)




    # Decryption method

    def decrypt_callback(self):

        key = self.get_key()

        plaintext = decrypt(self.cypher_entry.get(), key)

        self.plain_entry.delete(0, tk.END)

        self.plain_entry.insert(0, plaintext)




# Encryption formula of Caeser cipher

def encrypt(plaintext, key):

    cyphertext = ""

    for char in plaintext.upper():

        if char.isalpha():

            cyphertext += chr((ord(char) + key - 65) % 26 + 65)

        else:

            cyphertext += char

    return cyphertext




# Decryption formula of Caser cipher

def decrypt(cyphertext, key):

    plaintext = ""

    for char in cyphertext.upper():

        if char.isalpha():

            plaintext += chr((ord(char) - key - 65) % 26 + 65)

        else:

            plaintext += char

    return plaintext




# Run the program

if __name__ == "__main__":

    root = tk.Tk()

    caesar = CaesarCypherGUI(root)

    root.mainloop()

Explanation Of The Code

To begin with, we have imported the Tkinter module.

1. Then, we have initialized the font type and size of the text in the window.

2. After that, we used the title function to give the window’s title.

3. Then, we constructed a sting and integer variable for the cryptographical text and key.

4. Then, we have used the label, Entry, & button function for the control of the GUI of Plaintext, key, and ciphertext.

5. Then, we have written the definition for the clear and key function to clear the textbox and get the key.

6. After this, we have implemented the formula of encryption and decryption of Caesar cipher using python.

7. At last, we are running the application.

Output

The below snip shows the window of encoding decoding GUI that we have implemented using the Tkinter module.

1. Encryption using Caeser cipher and Tkinter module in python

Encode and Decode Messages Using Tkinter Module - Encryption

2. Decryption using Caeser cipher and Tkinter module in python

Encode and Decode Messages Using Tkinter Module - Decryption

Conclusion

Using the Tkinter module and some basic python functions, we have implemented cryptography for encoding and decoding messages using caesar cipher along with its GUI.

 

You May Also Like To Create…

0 Comments

Submit a Comment

Your email address will not be published.