Python Project — Encoding and Decoding Message

Rahul Patodi
Wiki Flood
Published in
4 min readOct 21, 2023

In today’s digital era our personal data can be stolen or used by hackers. With Encoding and Decoding we can keep personal conversation secure. Encoding is a process of converting text into an unrecognisable language and its reverse is known as Decoding, that is converting the encoded text into its original form.

About Python Encoding Decoding Message Project:

In this project we are going to build a Python Application to Encode and Decode the text or messages. Users have to enter the data and click the encode button to encode it and to retrieve the original data ,users have to click the decode button.

python encode decode project image

Prerequisites For Python Encoding Decoding Message Project:

First we will install the required library and module in our system using pip installer.

pip install tkinter
pip install base64

Importing the required Library and Modules:

from tkinter import *
import base64

tkinter-This library will help us in creating a GUI window for our program.

base64-It is used in encoding and decoding the text.

Initializing GUI Window:

root=Tk()    
root.geometry("450x400")
root.title("Encode Decode")
root.config(bg='#CAE1FF')

root-It is the name of our GUI window.

Tk()-It initialises tkinter which means GUI window is created.

geometry()-This method provides the length and breadth to the GUI window.

resizeable()-This method allows the window to change its size as per user need.

title()-This method gives title to the window

confg()-This method sets the configuration of the window.

Creating Labels,Entry Fields and Buttons:

message=StringVar()
key=StringVar()
mode=IntVar()
result=StringVar()


lb=Label(root,text="Encode & Decode Message with Python",bg='#EEE0E5',font=('Arial,bold',18),width=40)
lb.pack(pady=10)


lb1=Label(root,text="Message",font=('Arial,bold',15),bg='#CAE1FF')
lb1.place(x=30,y=70)
lb2=Label(root,text="Key",font=('Arial,bold',15),bg='#CAE1FF')
lb2.place(x=30,y=130)
lb3=Label(root,text="Text",font=('Arial,bold',15),bg='#CAE1FF')
lb3.place(x=30,y=190)

Stringvar()-It is a variable that holds a string data where we can set text value and can retrieve it.

Label()- It is used to display one line or more than one line of text.

text-It is used to display text on labels.

font-It is a style in which font is written.

bg-It is the background Colour of the label.

pack()-This helps in displaying widgets in the GUI window.

place()-It is used to set the position.

E1=Entry(root,textvariable=message,font=('Arial,bold',15))
E1.place(x=170,y=70)
E2=Entry(root,textvariable=key,font=('Arial,bold',15))
E2.place(x=170,y=130)
E3=Entry(root,textvariable=result,font=('Arial,bold',15))
E3.place(x=170,y=190)

Entry-It is used to create an input field and to display a single line of text.

rb1=Radiobutton(root,text="Encode",font=('Arial,15,bold'),variable=mode,value=1,bg='#CAE1FF')
rb1.place(x=100,y=240)
rb2=Radiobutton(root,text="Decode",font=('Arial,15,bold'),variable=mode,value=2,bg='#CAE1FF')
rb2.place(x=250,y=240)

Here we have created two RadioButtons for Encoding and Decoding functions.

btn1=Button(root,text=" Show ",font=('Arial,15,bold'),bd=5,bg='#6E7B8B',fg='white',command=Mode)
btn1.place(x=70,y=300)
btn2=Button(root,text="Reset",font=('Arial,15,bold'),bd=5,bg='#6E7B8B',fg='white',command=Reset)
btn2.place(x=185,y=300)
btn3=Button(root,text=" Exit ",font=('Arial,15,bold'),bd=5,bg='#6E7B8B',fg='white',command=Exit)
btn3.place(x=290,y=300)


root.mainloop()

Button()-It is a button used to display on our window.

bd-It is the border of the Button.

bg-It is the background colour.

fg-It is the colour of letters in text.

command-It is used as a function of a button when it is clicked.

place()-This Tkinter method is used to set the position.

root.mainloop()-It is simply a method in the main window that executes what we wish to execute in an application and ends the mainloop.

Defining Encoding and Decoding function:

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


def decode(key,message):
dec=[]
message=base64.urlsafe_b64decode(message).decode()
for i in range (len(message)):
key1=key[i % len(key)]
dec.append(chr((256 + ord(message[i])-ord(key1)) % 256))
return "".join(dec)

base64.urlsafe_b64encode-It encodes the string.

base64.urlsafe_b64decode-It decodes the string.

dec.append(chr((256 + ord(message[i])-ord(key1))%256))-It performs the inverse operation of the encoding process, effectively reversing the encryption.

ord(message[i])-ord(key1)-It is used to retrieve the ASCII values of the character in the message and key.

chr((256 + ord(message[i])-ord(key1))%256)-The addition of 256 is used to handle the negative values which may occur during the decoding. The division of %256 is used to keep the resulting value range between 0 to 255.

chr- This function takes an integer argument and converts it into character.

ord-This function takes string argument of a single unicode character and return its integer unicode value

get()-It returns the value from the Entry widget.

set()-It sets the value in the specified position.

join()-This method joins each element of list, string and tupple by a string separator.

Defining function for mode:

def Mode():
i=mode.get()
if i==1:
result.set(encode(key.get(),message.get()))
elif i==2:
result.set(decode(key.get(),message.get()))
else:
result.set("Invalid Input")

Defining function for Reset:

def Reset():
message.set("")
key.set("")
result.set("")

Defining function for Exit:

def Exit():
root.destroy()

Python Encoding and Decoding Message Output-

python encode decode message output
python encode decode project

Conclusion-

We have successfully created an application that encodes and decodes messages. Hope you had fun while making this project and learned how to use the tkinter module for making the interface of the application.

--

--