01 logo

End-To-End Encrypted Communication using Python

by Nikhil Vemu 15 days ago in how to

By Fernet Encryption Algorithm

Photo by Jason Leung on Unsplash

Have you ever wondered how WhatsApp maintains your chats secure to prevent the ‘Man in the Middle’ from viewing them. That’s called ‘End-to-End Encryption’, which encrypts the message before sending using a key, and decrypts the message after the receiver receives it.

Here’s the mini-version of it. I created it for my University project.

Platforms I used:

• Python

• Thingspeak (Ignore if you don’t know. It’s ok.)

(ThingSpeak is an open-source IoT cloud to log data from programs and IoT devices and perform Data Analytics.)

Python Packages to be downloaded:

• urllib

• cryptography

To download these, first install Python. Go to ‘Command Prompt’. Type pip install <package name> and press Enter.

For example:

pip install urllib

Now, here’s the code that sends the message:

Run it:

Here's how you interact with the program now

Now, type some message and give your own key. The message gets encrypted based on the key, and is sent to the ThingSpeak channel of the access key mentioned in the code.

Observe line 27. When you attach some number or string at the end of the URL, and open it, the data is sent to the cloud.

Encrypted text is shown for your reference.

Important Note: To access ThingSpeak, you need to have a MathWorks account. But in this code, I’m giving my access code. So, you can use it like you don’t need ThingSpeak!

Here’s the code that receives the message:

In lines 10 and 15, notice 'YourName'. Replace it with your PC's name.

Run it:

Here's how the program shows the receiver the message

Give the same key that’s given when sending. You’ll receive the message.

ThingSpeak allows you to download all the history of your input logs in a .csv file. Fortunately, it’s the same download URL every time. Observe it on line 9.

When you run, the program downloads the csv file with name 1.csv. It iterates through the file and reads out the last value, and then deletes the file from your system.

Give these codes to your friend and you both can communicate securely.

What if you give a wrong key? — It doesn’t show the message.

Don't ask me what these complicated exceptions are. XD

What the ‘Man in the Middle’ sees — The csv file:

Hehe, no one understands these unless they’re decrypted. One can’t decrypt these unless he gets the key. So, keep your key confidential every time.

Hope you’ve understood how End-to-End Encryption works with this little imitation!

This story was originally published on Medium in 'Data Driven Investor'. 

----------------------------------------------------------------------------------------If you've enjoyed reading this article, please check out the rest of my work on Vocal, and share them with your friends on Social Media. They'll enjoy it.

You can also find me on Medium  .

Did you love this article? Please consider rewarding my writing with a little tip. Thank you!

how to
Nikhil Vemu
Nikhil Vemu
Read next: Wearables vs The Virus | João Bocas | Engati Engage
Nikhil Vemu

A hobby writer who never leaves his readers discontent.

University Student | NCC Cadet | Hobby Writer | Curious Experimenter | 🇮🇳

Connect with me on Linkedin

Find me on Medium

See all posts by Nikhil Vemu

Find us on socal media

Miscellaneous links