Site icon DataFlair

Message Encode Decode in Python with Tkinter

Python course with 57 real-time projects - Learn Python

Python Message Encode-Decode – Secure your Information by Encoding the messages

Encoding is the process that transforms the text or information to the unrecognizable form and decryption is the process to convert the encrypted message into original form.

Message Encode-Decode

Message encoding and decoding is the process to first convert the original text to the random and meaningless text called ciphertext. This process is called encoding. Decoding is the process to convert that ciphertext to the original text. This process is also called the Encryption-Decryption process.

Message Encode-Decode Python Project

This objective of this project is to encode and decode messages using a common key. This project will be built using the Tkinter and base64 library.

In this project, users have to enter the message to encode or decode. Users have to select the mode to choose the encoding and decoding process. The same key must be used to process the encoding and decoding for the same message.

Project Prerequisites

To build this project we will use basic concept of python, Tkinter, and base64 library.

To install the library we use pip install command on the command prompt

pip install tkinter
pip install base64

Download Message Encode-Decode python code

Download source code of Message Encode-Decode Project – Python Message Encode Decode Project

Project File Structure

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

These are the step to build message encode – decode python project

So this is what we will do in this python project. Let’s start

Steps to develop message encode-decode project

1. Import Libraries

from tkinter import *
import base64

The first step is to import tkinter and base64 libraries.

2. Initialize Window

root = Tk()

root.geometry('500x300')
root.resizable(0,0)
root.title("DataFlair - Message Encode and Decode")
Label(root, text ='ENCODE DECODE', font = 'arial 20 bold').pack()

Label(root, text ='DataFlair', font = 'arial 20 bold').pack(side =BOTTOM)

Label() widget use to display one or more than one line of text that users aren’t able to modify.

3. Define variables

Text = StringVar()
private_key = StringVar()
mode = StringVar()
Result = StringVar()

4. Function to encode

def Encode(key,message):
    enc=[]

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

Encode function have arguments – key and message

5. Function to decode

def Decode(key,message):
    dec=[]
    message = base64.urlsafe_b64decode(message).decode()

    for i in range(len(message)):
        key_c = key[i % len(key)]
        dec.append(chr((256 + ord(message[i])- ord(key_c)) % 256))
    return "".join(dec)

Decode() function has arguments – key, message

6. Function to set mode

def Mode():
    if(mode.get() == 'e'):
        Result.set(Encode(private_key.get(), Text.get()))
    elif(mode.get() == 'd'):
        Result.set(Decode(private_key.get(), Text.get()))
    else:
        Result.set('Invalid Mode')

7. Function to exit window

def Exit():
    root.destroy()

root.destroy() will quit the program by stopping the mainloop()

8. Function to reset window

def Reset():
    Text.set("")
    private_key.set("")
    mode.set("")
    Result.set("")

This function set all variables to empty string

9. Labels and Buttons

Label(root, font= 'arial 12 bold', text='MESSAGE').place(x= 60,y=60)
Entry(root, font = 'arial 10', textvariable = Text, bg = 'ghost white').place(x=290, y = 60)

Label(root, font = 'arial 12 bold', text ='KEY').place(x=60, y = 90)
Entry(root, font = 'arial 10', textvariable = private_key , bg ='ghost white').place(x=290, y = 90)

Label(root, font = 'arial 12 bold', text ='MODE(e-encode, d-decode)').place(x=60, y = 120)
Entry(root, font = 'arial 10', textvariable = mode , bg= 'ghost white').place(x=290, y = 120)
Entry(root, font = 'arial 10 bold', textvariable = Result, bg ='ghost white').place(x=290, y = 150)

Button(root, font = 'arial 10 bold', text = 'RESULT'  ,padx =2,bg ='LightGray' ,command = Mode).place(x=60, y = 150)

Button(root, font = 'arial 10 bold' ,text ='RESET' ,width =6, command = Reset,bg = 'LimeGreen', padx=2).place(x=80, y = 190)

Button(root, font = 'arial 10 bold',text= 'EXIT' , width = 6, command = Exit,bg = 'OrangeRed', padx=2, pady=2).place(x=180, y = 190)

root.mainloop()

Label() widget use to display one or more than one line of text that users aren’t able to modify.

Entry() widget used to create an input text field.

Button() widget used to display button on our window

Message Encode-Decode Project output

Summary

We have successfully developed Message encode – decode project in Python. We used the popular tkinter library for rendering graphics on a display window and base64 to encode & decode. We learned how to encode and decode the string, how to create button, widget, and pass the function to the button.

In this way, we can encode our message and decode the encoded message in a secure way by using the key. We hope you enjoyed building this python project.

Exit mobile version