Python Encryptor/Decrypter

During my time of studying for the CompTIA Network+ exam last week (I passed), there was a heavy emphasis on keeping everything secure. Encryption, hashing, VPNs, port security, etc. It is insane to me that past protocols and systems just sent information between computers completely naked out in the open. Anybody listening could see your password and other information in plain text.

But to be fair, I’m assuming the people who made all these protocols did not expect networks/the internet to get so big and widely used.

As time went on, more secure protocols were made, but became too weak with time. For example, WEP is no longer used. Why? Even though it did have a key generation algorithm, it was very weak. It only had a 24 bit – initialization vector. Which means that there are 16,777,216 possible values. Which can be easily brute forced with todays computing.

This prompted me to create an Encryptor/Decrypter with Python.

First we Import the Fernet module from the cryptography library. Which gives us the ability to encrypt/decrypt.
This line generates a secret key using the Fernet algorithm, using the generate_key() method of the fernet module we imported just before this.
This creates an object using the Fernet algorithm. Which will be used to encrypt and decrypt messages.

I wanted to make this so the user could input a certain message they wanted to encrypt, so to do that I made it so it would prompt the user to enter a message. And from here the user would type in their message.
This converts the message into a byte string, which will then allow it to be encrypted by the Fernet algorithm.
Then we want to encrypt the message. The encryption will returns a byte string representation of the encrypted message. So it will have a b in front of the message and it will be surrounded by single quotes.
Then we will print the encrypted message back out to the user.
Here, I also wanted to give the user the option of decrypting the message after receiving their encrypted message. If the user wants to decrypt the message by typing “y”, the code uses the decrypt method from Fernet that we installed earlier decrypt the message. And if the user inputs “n”, the program stops and says “Okay, bye!”

Here is an example of me running the script.

Leave a Reply

Your email address will not be published. Required fields are marked *