Over the last few weeks I have been looking at ciphers and encryption, which has included a Python program to brute force the Caesar cipher, Python to encrypt / decrypt with the Vigenère cipher , some Morse code and RSA Public Keys.
With my recent looks into encryption, especially the look into the Vigenere Cipher, I decided today to try and create a messaging application that includes some encryption. Messaging applications with encryption are quite popular and it seemed like a good way to put my knowledge to the test. With that in mind I turned to my favourite language, Python and the sockets library. I’ve not used sockets (as far as I can remember) since my port scanner project last summer.
The application has four functions:
- message_server
- message_client
- vigenere_enc
- vigenere_dec
vigenere_enc and vigenere_dec are both straight from my Vigenère Python and handle the encryption and decryption of the message.
message_server is run on the device that will act as the messaging server. Once running it waits for a message from the device acting as the messaging client.
message_client runs on the device that will act as the client. With a device running as the server it can connect and start messaging.
The HOST variable is set to the IP address of the device that is being the server. Most port numbers below 1023 are already allocated, so I would recommend choosing a higher number. Remember to make sure the port number is allowed through your firewall if you have a firewall enabled.
The message_server uses the bind options of socket, and then listens for a connection.
The message_client uses connect option of socket to connect to the IP address and port number that the server has bind.
A loop is used to keep the connection open so that messages can be sent back and forth. Once the loop is closed (by sending /q to cause a break), the close option of socket is used to cleanly close the socket connection. If the program errors or this command is not received then the address/port may stay open until the device is rebooted.
UTF-8 encoding is used to send the encrypted message. UTF-8 is widely used across the world wide web and supports both ASCII and Unicode characters. Check out: https://en.wikipedia.org/wiki/UTF-8for more information about UTF-8.
Time stamps of the server starting up and when the messages are received are pulled from the datetime module.
The encryption key in the below is “test” and the as the Vigenère cipher is symmetric encryption the same key is used for decryption. Both client and server need to use the same key.
Further improvements needed: automatic recovery if the pipe is broken (lost connection / lost packets), option to send messages without awaiting a reply, logging (of server errors and messages). Possibly change from Vigenère cipher to something a little more recent.
I’ve used the program between my Raspberry Pi 4 and my MacBook, with the Pi as message_server() and my MacBook as message_client().





—
—
I have uploaded the code for the Python instant messaging app to my GitHub and it is available at: https://github.com/geektechdude/Python_Encryption/blob/master/geektechstuff_vigenere_socket_chat.py
I have included a few links to my own blog posts but if you want more to learn more about encryption / ciphers then I recommend the FutureLearn course from the Raspberry Pi Foundation – https://www.futurelearn.com/courses/encryption-and-cryptography , and it is free.
You must be logged in to post a comment.