Python based GUI application to encrypt and decrypt text using Caesar Cipher.

Rohit Shubankar
Analytics Vidhya
Published in
5 min readFeb 24, 2020

In this tutorial, I am going to show you how to build a GUI app that can encrypt and decrypt text. We are going to implement Caesar Cipher algorithm to perform the above operations.

The basic working of the app is that the app consists of a text bar where the user enters the text. There are 2 option available, either the user can encrypt the text or can decrypt the text. When the user clicks on any one of the option, the operation is performed and the user is presented with the encrypted / decrypted text.

Caesar Cipher

One of the earliest and the simplest form of encryption technique, Caesar cipher involves replacing the letters of the alphabet with the letter three places down the current letter. It is a type of substitution cipher.

For Example:

Plain Text : HelloWorld

Cipher Text : KhoorZruog

The encryption/decryption of the letter is performed by first converting the letters into numbers, according to the scheme A=0, B =1, C =2,…..,X=23, Y=24, Z=25 and performing a modular arithmetic.

The Algorithm can be represented as follows:

Encryption: C = E(K,P) = (P + K)mod26

Decryption: P = D(K,C) = (C - K)mod26

where

C : Cipher Text

K : Key (Key=3 for Caesar cipher)

P: Plain Text

Implementation

First, we start by loading the required packages:

Now that we have our required packages loaded successfully, we start by creating our GUI app. We initialize tkinter by creating a root widget, which is a frame/window where we are going to attach our radio buttons and the text bar. Please remember that there can be only one root widget and it has to be created before any other widgets.

Now, to change the title and the icon image in the title bar :

We can set the dimensions of our window:

If you have followed the above steps correctly, just add root.mainloop() at the end of your code and run the program. We use the mainloop() method when we want to run our program. You will get a window as displayed below:

Lets add a canvas to our window. Canvas is a rectangular area where we can place our text and widgets. In the below line of code, we are attaching our canvas to the root window or the parent window and we give the same dimensions as our root window. We also give a background color to our canvas.

If we run the program now, we may get something like this:

We create a variable “bold_font” which uses “tkFont” module which enables us to create font instances. We will use it to set the font properties like family, size and style which we will use it with our labels.

Now, lets add a text in our canvas. We create a label which basically allows us to display text and images. User cannot interact with the labels but can just view it.

Let’s run the program.

We will create a text bar where the user can enter the text. We use the Entry widget to enter and display single line of text.

We will get something like this:

We create another text label which tells the user to “choose an operation” from the given options.

Running the program, we get:

Tkinter provides us with variables which we can use to manipulate the values of Tkinter widget. The connection works both ways: if the variable changes for any reason, the widget it’s connected to will be updated to reflect the new value. This control variables are sub classed from a class called Variable, defined in the Tkinter module. These control variables are used like regular Python variables to keep certain values. We construct an integer variable, which we connect to our radio buttons.

We define them as follow:

We now define a function “choice”, which will get the value of the radio button selected by the user and will perform either encryption or decryption based on the option selected by the user. The value of the radio button selected is stored in the variable we created above. To retrieve the value, we use get() method.

Now we define our Encryption and decryption function. The function basically traverses through the given text character by character. For each character, we convert it according to the algorithm we defined earlier. Then we return the newly generated text. We also create text labels which will show the output after the execution of the function.

Encryption Function
Decryption Function

Now, we create the radio buttons. The command parameter is used to link the radio buttons to the encryption and decryption function defined above.

Lastly, We create a text label “Converted Text” below which the user can find the converted text.

Finally, when we run the program :

Output for Encryption :

Output for Decryption :

Well here you go. You have just created an GUI application that can encrypt and decrypt any text.

The code described above is available in my Github account :

If anyone has any questions/comments feel free to contact me here or by mail: rohit.shubankar@gmail.com

Thank you

--

--