Message Encode Decode using Python with GUI

In today’s world, data can easily be stolen and used by hackers. With data encryption and decryption we can secure our messages. This python project will encode and decode messages in real-time.

Encoding is the process of converting a text into an unrecognizable form and Decoding is the reverse process, ie converting the encoded data back to the original text.

Python Message Encode Decode Project

In this python project, we are going to encode and decode strings (messages). We are going to use tkinter and base64 libraries.

This python project provides a gui using tkinter where user can work easily:

  • The user has to enter a message to encode or decode.
  • User must select the option encode or decode, it depends on the operation he wants to perform.
  • Providing the same key is mandatory, ie while decoding user must enter the same key which he used for encoding

Project Prerequisites

To develop message encode decode project you require the basic concepts of python, tkinter, and base64.

To install tkinter and base64, please run below commands:

pip install tkinter
pip install base64

Follow TechVidvan on Google & Stay updated with latest technology trends

Download Message Encode and Decode Python Program

Please download the source code of python Message Encode and Decode: Message Encode Decode Project Code

Project File Structure

First, let’s look into the message encode and decode python program overview:

  1. Importing the base64 and tkinter modules.
  2. Creating the GUI using tkinter.
  3. Creating the functions and the button.

1. Creating the Program

Import the required tkinter and random packages.

from tkinter import *
import base64
from tkinter import messagebox

Explanation:

Let’s begin with the start of the program by importing the required libraries

  • Tkinter library: Tkinter is the most popular GUI library in Python. Tkinter is used to create simple GUI applications in python
  • base64: This library provides a function to encode binary data to ASCII characters and decode the ASCII characters back to binary data.

2. Creating the GUI:

root = Tk()

root.geometry("600x600")

root.title("TechVidvan Message Encryption and Decryption")


Msg = StringVar()
key = StringVar()
mode = StringVar()
Result = StringVar()

label = Label(root, text='Enter Message', font=('Helvetica',10))
label.place(x=10,y=0)

mes = Entry(root,textvariable=Msg, font=('calibre',10,'normal'))
mes.place(x=200,y=0)

label1 = Label(root, text='e for encrypt and d for decrypt', font=('Helvetica',10))
label1.place(x=10,y=50)

l_mode = Entry(root, textvariable=mode, font=('calibre',10,'normal'))
l_mode.place(x=200,y=50)

label2 = Label(root, text='Enter key', font=('Helvetica',10))
label2.place(x=10,y=100)

l_key = Entry(root, textvariable=key, font=('calibre',10,'normal'))
l_key.place(x=200,y=100)

label3 = Label(root, text='Result', font=('Helvetica',10))
label3.place(x=10,y=150)

res = Entry(root,textvariable=Result, font=('calibre',10,'normal'))
res.place(x=200,y=150)

Explanation:

The main function part involves creation of the Tk root widget. We create the GUI of this program.

  • Label(): This function creates a label that can make text appear on the GUI.
  • Pack(): The pack() method declares the position of widgets with respect to one and another.
  • Grid(): This method allows managing the layout of the widget.

3. Creating the function and the buttons:

def encode(key, msg):
    enc = []
    for i in range(len(msg)):
        key_c = key[i % len(key)]
        enc_c = chr((ord(msg[i]) +
                     ord(key_c)) % 256)
        enc.append(enc_c)
    return base64.urlsafe_b64encode("".join(enc).encode()).decode()



def decode(key, enc):
    dec = []
    enc = base64.urlsafe_b64decode(enc).decode()
    for i in range(len(enc)):
        key_c = key[i % len(key)]
        dec_c = chr((256 + ord(enc[i]) - ord(key_c)) % 256)

        dec.append(dec_c)
    return "".join(dec)


def Results():
    msg = Msg.get()
    k = key.get()
    m = mode.get()
    m.lower()
    if (m == 'e'):
        Result.set(encode(k, msg))
    elif(m== 'd'):
        Result.set(decode(k, msg))
    else:
        messagebox.showinfo('TechVidvan', 'Wrong mode entered. Try again.')



def qExit():
    root.destroy()



def Reset():
    Msg.set("")
    key.set("")
    mode.set("")
    Result.set("")


btnshow = Button(root, text='Show Message', foreground='green', command=Results)
btnshow.place(x=10,y=200)

btnreset = Button(root, text='Reset', foreground='red', command=Reset)
btnshow.place(x=150,y=200)

btnexit = Button(root, text='Exit', foreground='black', command=qExit)
btnshow.place(x=300,y=200)


root.mainloop()

Explanation:

  • Encode: encode the string and return the encoded string.
    • Enc is an empty list
    • The loop runs till the end of the message
    • i% of len(key) returns the remainder of division between i and len (key) and that remainder will be used as an index of key and the value of key at that index is stored in key_c
    • ord()- generates integer unicode corresponding to the string argument of single unicode character
    • chr()- This function takes an integer argument and converts it into string.
    • base64.urlsafe_b64encode encodes a string.
    • The join() method is used to joins each element of list, string, and tuple by a string separator (can be a custom separator) and gives final concatenated string.
    • encode()- returns the utf-8 encoded string.
    • decode()- return the decoded string.
  • Decode: Decode the string and return the decoded string.
    • Dec is an empty list.
    • 256 + ord(message[i]) – ord(key_c)) % 256 gives the remainder of addition of 256 with subtraction of ord(message[i]) – ord( key_c) and then division with 256 and passes the final remainder to chr() function
    • chr() function convert integer value to string and store to dec_c
    • Return the decoded string.
  • Results- Checks which function needs to be run and updates the result.
  • Reset– Resets the input area.
  • qExit- Ends the program.
  • Button()– This function creates a button and sets a function to perform when pressed.
  • Btntotal when pressed calls the result function.
  • Btnreset when pressed calls the reset function.
  • Btnexit when pressed calls the qExit function.
  • Mainloop()- This function updates the GUI and waits for the events to happen.

Python Message Encode Decode Output

python message encode decode output

Summary

We have successfully created python message encryptor and decryptor., which encode and decode the messages in real-time. We used popular libraries such as tkinter and base64. We learned how to create a GUI, create buttons and call the functions when the buttons were pressed. We also learned how to encode and decode the strings in python.

Did you know we work 24x7 to provide you best tutorials
Please encourage us - write a review on Google | Facebook


2 Responses

  1. Manish says:

    Make any video

  2. Shyam says:

    Which algorithm is used for encryption and decryption???
    Please tell the algorithm

Leave a Reply

Your email address will not be published. Required fields are marked *