Site icon TechVidvan

Message Encode Decode using Python with GUI

python project encode decode messages

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:

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

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

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.

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:

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.

Exit mobile version