End-To-End Encrypted Communication using Python
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:
- 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:
To download these, first install Python. Go to ‘Command Prompt’. Type pip install <package name> and press Enter.
pip install urllib
Now, here’s the code that sends the message:
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:
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.
What the ‘Man in the Middle’ sees — The csv file:
Hope you’ve understood how End-to-End Encryption works with this little imitation!