The Wayback Machine - https://web.archive.org/web/20240915134323/https://www.geeksforgeeks.org/python-message-encode-decode-using-tkinter/
Open In App

Python | Message Encode-Decode using Tkinter

Last Updated : 06 Jul, 2021
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Prerequisite : Basics of Tkinter
Python offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, tkinter is most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with tkinter outputs the fastest and easiest way to create the GUI applications. 
Python provides the Tkinter toolkit to develop GUI applications. Now, it’s upto the imagination or necessity of developer, what he/she want to develop using this toolkit. Let’s try to implement a message encryption-decryption application according to the Vigenère cipher, which can encrypt the message using the key and can decrypt the encrypted hash using same key.
 

Modules used in the project :

Tkinter  -> GUI toolkittime datetime  base64   -> Vigenère cipher

Below is the implementation of above idea : 
 

Python3




# import tkinter module
from tkinter import *
 
# import other necessary modules
import random
import time
import datetime
 
# creating root object
root = Tk()
 
# defining size of window
root.geometry("1200x6000")
 
# setting up the title of window
root.title("Message Encryption and Decryption")
 
Tops = Frame(root, width = 1600, relief = SUNKEN)
Tops.pack(side = TOP)
 
f1 = Frame(root, width = 800, height = 700,
                            relief = SUNKEN)
f1.pack(side = LEFT)
 
# ==============================================
#                  TIME
# ==============================================
localtime = time.asctime(time.localtime(time.time()))
 
lblInfo = Label(Tops, font = ('helvetica', 50, 'bold'),
          text = "SECRET MESSAGING \n Vigenère cipher",
                     fg = "Black", bd = 10, anchor='w')
                      
lblInfo.grid(row = 0, column = 0)
 
lblInfo = Label(Tops, font=('arial', 20, 'bold'),
             text = localtime, fg = "Steel Blue",
                           bd = 10, anchor = 'w')
                         
lblInfo.grid(row = 1, column = 0)
 
rand = StringVar()
Msg = StringVar()
key = StringVar()
mode = StringVar()
Result = StringVar()
 
# exit function
def qExit():
    root.destroy()
 
# Function to reset the window
def Reset():
    rand.set("")
    Msg.set("")
    key.set("")
    mode.set("")
    Result.set("")
 
 
# reference
lblReference = Label(f1, font = ('arial', 16, 'bold'),
                text = "Name:", bd = 16, anchor = "w")
                 
lblReference.grid(row = 0, column = 0)
 
txtReference = Entry(f1, font = ('arial', 16, 'bold'),
               textvariable = rand, bd = 10, insertwidth = 4,
                        bg = "powder blue", justify = 'right')
                         
txtReference.grid(row = 0, column = 1)
 
# labels
lblMsg = Label(f1, font = ('arial', 16, 'bold'),
         text = "MESSAGE", bd = 16, anchor = "w")
          
lblMsg.grid(row = 1, column = 0)
 
txtMsg = Entry(f1, font = ('arial', 16, 'bold'),
         textvariable = Msg, bd = 10, insertwidth = 4,
                bg = "powder blue", justify = 'right')
                 
txtMsg.grid(row = 1, column = 1)
 
lblkey = Label(f1, font = ('arial', 16, 'bold'),
            text = "KEY", bd = 16, anchor = "w")
             
lblkey.grid(row = 2, column = 0)
 
txtkey = Entry(f1, font = ('arial', 16, 'bold'),
         textvariable = key, bd = 10, insertwidth = 4,
                bg = "powder blue", justify = 'right')
                 
txtkey.grid(row = 2, column = 1)
 
lblmode = Label(f1, font = ('arial', 16, 'bold'),
          text = "MODE(e for encrypt, d for decrypt)",
                                bd = 16, anchor = "w")
                                 
lblmode.grid(row = 3, column = 0)
 
txtmode = Entry(f1, font = ('arial', 16, 'bold'),
          textvariable = mode, bd = 10, insertwidth = 4,
                  bg = "powder blue", justify = 'right')
                   
txtmode.grid(row = 3, column = 1)
 
lblService = Label(f1, font = ('arial', 16, 'bold'),
             text = "The Result-", bd = 16, anchor = "w")
              
lblService.grid(row = 2, column = 2)
 
txtService = Entry(f1, font = ('arial', 16, 'bold'),
             textvariable = Result, bd = 10, insertwidth = 4,
                       bg = "powder blue", justify = 'right')
                        
txtService.grid(row = 2, column = 3)
 
# Vigenère cipher
import base64
 
# Function to encode
def encode(key, clear):
    enc = []
     
    for i in range(len(clear)):
        key_c = key[i % len(key)]
        enc_c = chr((ord(clear[i]) +
                     ord(key_c)) % 256)
                      
        enc.append(enc_c)
         
    return base64.urlsafe_b64encode("".join(enc).encode()).decode()
 
# Function to 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 Ref():
    print("Message= ", (Msg.get()))
 
    clear = Msg.get()
    k = key.get()
    m = mode.get()
 
    if (m == 'e'):
        Result.set(encode(k, clear))
    else:
        Result.set(decode(k, clear))
 
# Show message button
btnTotal = Button(f1, padx = 16, pady = 8, bd = 16, fg = "black",
                        font = ('arial', 16, 'bold'), width = 10,
                       text = "Show Message", bg = "powder blue",
                         command = Ref).grid(row = 7, column = 1)
 
# Reset button
btnReset = Button(f1, padx = 16, pady = 8, bd = 16,
                  fg = "black", font = ('arial', 16, 'bold'),
                    width = 10, text = "Reset", bg = "green",
                   command = Reset).grid(row = 7, column = 2)
 
# Exit button
btnExit = Button(f1, padx = 16, pady = 8, bd = 16,
                 fg = "black", font = ('arial', 16, 'bold'),
                      width = 10, text = "Exit", bg = "red",
                  command = qExit).grid(row = 7, column = 3)
 
# keeps window alive
root.mainloop()


Output : 
Encrypt Window – 
 

Decrypt Window – 
 

 



Previous Article
Next Article

Similar Reads

How To Encode And Decode A Message using Python?
Encryption is the process of converting a normal message (plain text) into a meaningless message (Ciphertext). Whereas, Decryption is the process of converting a meaningless message (Cipher text) into its original form (Plain text). In this article, we will take forward the idea of encryption and decryption and draft a python program. In this artic
3 min read
Create a Yes/No Message Box in Python using tkinter
Python offers a number Graphical User Interface(GUI) framework but Tk interface or tkinter is the most widely used framework. It is cross-platform which allows the same code to be run irrespective of the OS platform (Windows, Linux or macOS). Tkinter is lightweight, faster and simple to work with. Tkinter provides a variety of widgets that can be c
4 min read
Python Tkinter - Message
Python offers multiple options for developing a GUI (Graphical User Interface). Out of all the GUI methods, Tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with Tkinter is the fastest and easiest way to create GUI applications. Creating a GUI using Tkinter is an easy task
3 min read
How to create a pop up message when a button is pressed in Python - Tkinter?
In this article, we will see how to create a button and how to show a popup message when a button is pressed in Python. Tkinter is a standard Python Package for creating GUI applications. Tkinter may be a set of wrappers that implement the Tk widgets as Python classes. Python, when combined with Tkinter, provides a quick and straightforward way to
2 min read
Difference Between The Widgets Of Tkinter And Tkinter.Ttk In Python
Python offers a variety of GUI (Graphical User Interface) frameworks to develop desktop applications. Among these, Tkinter is the standard Python interface to the Tk GUI toolkit. Tkinter also includes the 'ttk' module, which enhances the visual appeal and functionality of the applications. In this article, we will cover the differences, providing a
4 min read
numpy.defchararray.encode() in Python
numpy.core.defchararray.encode(arr, encoding): This numpy function encodes the string(object) based on the specified codec. Parameters: arr : array-like or string. encoding : [str] Name of encoding being followed. error : Specifying how to handle error. Returns : Encoded string Code: C/C++ Code # Python Program illustrating # numpy.char.encode() me
1 min read
Python | Pandas Series.str.encode()
Series.str can be used to access the values of the series as strings and apply several methods to it. Pandas Series.str.encode() function is used to encode character string in the Series/Index using indicated encoding. Equivalent to str.encode(). Syntax: Series.str.encode(encoding, errors='strict') Parameter : encoding : str errors : str, optional
2 min read
IncrementalEncoder encode() in Python
With the help of IncrementalEncoder.encode() method, we can encode the string into the binary form by using IncrementalEncoder.encode() method. Syntax : IncrementalEncoder.encode(string) Return : Return the encoded string. Note : If you want to use this method you should have python 3.8.2 version or latest. Example #1 : In this example we can see t
1 min read
Python Encode Unicode and non-ASCII characters into JSON
This article will provide a comprehensive guide on how to work with Unicode and non-ASCII characters in Python when generating and parsing JSON data. We will look at the different ways to handle Unicode and non-ASCII characters in JSON. By the end of this article, you should have a good understanding of how to work with Unicode and non-ASCII charac
5 min read
How To Fix - UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in position 20: ordinal not in range(128) in Python
Several errors can arise when an attempt to change from one datatype to another is made. The reason is the inability of some datatype to get casted/converted into others. One of the most common errors during these conversions is Unicode Encode Error which occurs when a text containing a Unicode literal is attempted to be encoded bytes. This article
2 min read
Python Strings encode() method
Python String encode() converts a string value into a collection of bytes, using an encoding scheme specified by the user. Python String encode() Method Syntax:Syntax: encode(encoding, errors) Parameters: encoding: Specifies the encoding on the basis of which encoding has to be performed. errors: Decides how to handle the errors if they occur, e.g
5 min read
numpy.defchararray.decode() in Python
numpy.core.defchararray.decode(arr, encoding): This numpy function decode the string(object) based on the specified codec. Parameters: arr : array-like or string. encoding : [str] Name of codec being followed. error : Specifying how to handle error. Returns : Decoded string Code: # Python Program illustrating # numpy.char.decode() method import num
1 min read
Python | Pandas Series.str.decode()
Series.str can be used to access the values of the series as strings and apply several methods to it. Pandas Series.str.decode() function is used to decode character string in the Series/Index using indicated encoding. This function is equivalent to str.decode() in python2 and bytes.decode() in python3. Syntax: Series.str.decode(encoding, errors='s
2 min read
codecs.decode() in Python
With the help of codecs.decode() method, we can decode the binary string into normal form by using codecs.decode() method. Syntax : codecs.decode(b_string) Return : Return the decoded string. Example #1 : In this example we can see that by using codecs.decode() method, we are able to get the decoded string which can be in binary form by using this
1 min read
Python Strings decode() method
In Python we have decode() is a method specified in Strings. This method is used to convert from one encoding scheme, in which the argument string is encoded to the desired encoding scheme. This works opposite to the encode. It accepts the encoding of the encoding string to decode it and returns the original string. Python Decode() Function SyntaxS
4 min read
Send message to Telegram user using Python
Have you ever wondered how people do automation on Telegram? You may know that Telegram has a big user base and so it is one of the preferred social media to read people. What good thing about Telegram is that it provides a bunch of API's methods, unlike Whatsapp which restricts such things. So in this post, we will be sharing how to send messages
3 min read
Send Direct Message On Instagram using Selenium in Python
In this article, we will learn how we can send a direct message to users on Instagram without any manual action. We will be using the selenium module to do this task. Requirements:Chrome Driver for Chrome Browser (https://chromedriver.chromium.org/) or Gecko Driver for Firefox(https://github.com/mozilla/geckodriver/releases)Selenium Package. To ins
2 min read
Send Message on Instagram Using Instabot module in Python
In this article, we are going to see how to send messages on Instagram using the Instabot module in Python. Instagram is a nice platform for chatting but when it comes to sending the same message to all friends this is a really boring and time-consuming task especially if you have a large number of followers. To make this easy we make a bot using P
2 min read
Send message to FB friend using Python
The power of Python comes because of the large number of modules it has. This time we are going to use one of those. Every one of us, one time or another, has a wish of the message (or spamming -.-) our Facebook friend. This is a program that can do something similar. So without further delay, let’s jump right in. C/C++ Code import fbchat from getp
3 min read
Color game using Tkinter in Python
TKinter is widely used for developing GUI applications. Along with applications, we can also use Tkinter GUI to develop games. Let's try to make a game using Tkinter. In this game player has to enter color of the word that appears on the screen and hence the score increases by one, the total time to play this game is 30 seconds. Colors used in this
4 min read
Simple registration form using Python Tkinter
Prerequisites: Tkinter Introduction, openpyxl module.Python provides the Tkinter toolkit to develop GUI applications. Now, it’s upto the imagination or necessity of developer, what he/she want to develop using this toolkit. Let's make a simple information form GUI application using Tkinter. In this application, User has to fill up the required info
5 min read
Python | Simple GUI calculator using Tkinter
Prerequisite: Tkinter Introduction, lambda function Python offers multiple options for developing a GUI (Graphical User Interface). Out of all the GUI methods, Tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with Tkinter outputs the fastest and easiest way to create GUI a
6 min read
Python | Distance-time GUI calculator using Tkinter
Prerequisites : Introduction to Tkinter | Using google distance matrix API Python offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, tkinter is most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with tkinter outputs the fastest and easiest wa
7 min read
Python | Random Password Generator using Tkinter
With growing technology, everything has relied on data, and securing this data is the main concern. Passwords are meant to keep the data safe that we upload on the Internet. An easy password can be hacked easily and all personal information can be misused. In order to prevent such things and keep the data safe, it is necessary to keep our passwords
4 min read
How to visualize selection and insertion sort using Tkinter in Python?
In this article, we are going to create a GUI application that will make us visualize and understand two of the most popular sorting algorithms better, using Tkinter module. Those two sorting algorithms are selection sort and insertion sort. Selection sort and Insertion sort are the two most popular algorithms. Selection sort is a comparison-based
6 min read
Python | Real time currency converter using Tkinter
Prerequisites : Introduction to tkinter | Get the real time currency exchange ratePython offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with tkinter outputs the fastest and
4 min read
Python | Real time weather detection using Tkinter
Prerequisites : Introduction to tkinter | Find current weather of any cityPython offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with tkinter outputs the fastest and easiest
5 min read
Python | Simple FLAMES game using Tkinter
Prerequisites: Introduction to TkinterProgram to implement simple FLAMES game Python offers multiple options for developing a GUI (Graphical User Interface). Out of all the GUI methods, Tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with Tkinter outputs the fastest and e
5 min read
Python | Simple calculator using Tkinter
Prerequisite : Tkinter IntroductionPython offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with tkinter outputs the fastest and easiest way to create the GUI applications. Cr
3 min read
Python | Create a digital clock using Tkinter
As we know Tkinter is used to create a variety of GUI (Graphical User Interface) applications. In this article we will learn how to create a Digital clock using Tkinter. Prerequisites: Python functions Tkinter basics (Label Widget) Time module https://www.youtube.com/watch?v=1INA9AmaDtQ Using Label widget from Tkinter and time module: In the follow
2 min read
Article Tags :
Practice Tags :