Introduction
Have you ever felt, It would be nice if I could talk to my secret friend(secret means girlfriend/boyfriend) in secret. In teenage many students fall in love and tries to hide his/her friend from their parents, relatives, etc. They want to be connected with each other, but secretly.
There is a chance to caught by someone every time. But today, I came up here with a solution of it with python programming.
In this tutorial, weāll create a python program to Encrypt and Decrypt our Secret Messages. Using this program you can communicate with your friends secretly by encrypting your messages.
Not only senders are gonna get help through it, also receivers will get too. So letās get started.
šVisit Also:Ā Wish Your Friends with Stylish Text in Python ā ASCII Art
Program Details
The program will ask you to choose an option between āencryptā and ādecryptā. Then it will ask to type the message in a text box. If you choose āencryptā, then your message will be formed into a penniless form. But if you choose ādecryptā, your meaningless message will be turned into a text you can read.
How the program works
The program puts a random letter between each pair of letters in the message. Iteration starts from the first two, then the next two, and so on. The program also makes the encrypted text readable again by removing the extra letters from the meaningless message.
Requirements
The Tkinter module has been used to make the GUI of the program. The only third-party module, youāve to install before running it.
tkinter: pip install tkinter
Import the modules
You need to import the tkinter module along with some widgets. These will let you use some of the GUI features of the module.
from tkinter import *
from tkinter import ttk, messagebox, simpledialog
from random import choice
Encrypt or Decrypt?
Now create a function: controller(), to show the options of āencryptā or ādecryptā in a combo box for choosing by the users.
def controller():
while True:
if task.get() == 'encrypt':
message = get_message()
encrypted = encrypt(message)
messagebox.showinfo('Encrypted message is:', encrypted)
elif task.get() == 'decrypt':
message = get_message()
decrypted = decrypt(message)
messagebox.showinfo('Decrypted message is:', decrypted)
else:
break
Get the secret message
Now create a new function: āget_message()ā, to show a dialog box. It asks the users to enter the message.
def get_message():
message = simpledialog.askstring('Message', 'Enter the secret message: ')
return message
Encrypt the text
Create a function: āencrypt()ā, to create the logic of the encryption technique. Weāll put some random fake letters between each pair of letters of the original message. See the image below.
def encrypt(message):
encrypted_list = []
fake_letters = ['a', 'b', 'c', 'd', 'e', 'f', 's', 't', 'y', 'z']
for counter in range(0, len(message)):
encrypted_list.append(message[counter])
encrypted_list.append(choice(fake_letters))
new_message = ''.join(encrypted_list)
return new_message
Decrypt the text
Add this ādecrypt()ā function just after the āencrypt()ā function, to remove the fake letters from the meaningless text and to get back the original one.
def decrypt(message):
even_letters = get_even_letters(message)
new_message = ''.join(even_letters)
return new_message
Get the even letters
Letās create a function āget_even_letters()ā, which takes texts and returns a list containing all the even-numbered letters.
def get_even_letters(message):
even_letters = []
for counter in range(0, len(message)):
if is_even(counter):
even_letters.append(message[counter])
return even_letters
Is it even?
āis_even()ā, will tell the program if thereās an even number of characters or not, in the text message.
def is_even(number):
return number % 2==0
The Main Function
Now you need to create the main function to manage the GUI of this program.
if __name__ == '__main__':
root = Tk()
root.geometry("280x130")
root.title('Task')
label_1 = Label(root, text='Do you want to encrypt or decrypt?',
font=("times new roman",14))
label_1.place(x=10, y=10)
# Get the task
task = StringVar()
task_combobox = ttk.Combobox(root, width=30, height=10, textvariable=task)
task_combobox['values'] = ['encrypt', 'decrypt']
task_combobox.current(0)
task_combobox.place(x=10, y=40)
# Buttons
ok_button = Button(root, text='OK', bg="white", fg="black",
width= 12,command=controller)
ok_button.place(x=10, y=70)
cancel_button = Button(root, text='Cancel', bg="white",
fg="black", width= 12,command=root.quit)
cancel_button.place(x=140, y=70)
root.mainloop()
The Output
āVisit Also:Ā Create a Screen Recorder using Python ā Very Easy to Use
Summary
In this tutorial, we build a python program which can turn a meaningful message into a gibberish one. We used a simple encryption technique in this program.
āDo share this code with your friend to communicate with each other secretly.Ā
I hope you loved this tutorial. Please share your love, and do comment below for whom youāre gonna use this Python program.
To get more lovely Python topics, visit the separate page created only for Cool Python Programs. Some examples are given below.
šDraw the Sketch of Lionel Messi using a Python Program
šBlurring an image in Python ā using OpenCV and Pillow library
šCheck the strength of your password using python
Thanks for reading!š
PySeek