Cryptography is defined as the process of keeping important information safe and secure by converting them into something humans can’t directly understand to keep the actual message safe and away from the wrong hands.
In the modern era of modern technology, everyone should crypt the data being sent because not only it’s a great practice but also it keeps personal and official information safe and secure.
One must also have a strong cryptography algorithm to make sure the encrypted text is much harder to hack through and your information is more secure from going into the wrong hands.
Why is Cryptography important?
Cryptography is important for the following reasons:
- Guarding important information and communication info against unauthorized people and prevent access of information to them.
- Have digital signatures which are helpful in protecting important information against forgeries.
- It is also essential to maintain the integrity of the information.
Implementing Cryptography in Python
Now that we learned a lot about Cryptography. Now let’s learn how to implement it yourself using the Python programming language.
1. Importing Modules
To perform cryptography, we will be using the cryptography
module and we will be making use of the Fernet
objects.
from cryptography.fernet import Fernet
2. Implementing Cryptography
To implement cryptography we will be generating a Fernet key (known as the “secret key”) and then we create a Fernet object using the key.
This key is very important and it needs to be kept safe! If someone finds your key he/she can decrypt all your secret messages and if you lose it then you will no longer be able to decrypt your own messages.
key = Fernet.generate_key()
Fernet_obj= Fernet(key)
The next step is to encrypt the text where we use of the encrypt function and pass the message to the function. The function will return the encrypted message.
Along with this let’s also store the decrypted message from the encrypted message using the decrypt
function and pass the encrypted message.
Encry_text = Fernet_obj.encrypt(b"I am a secret! I will get encrypted into something you will never understand")
Org_text= Fernet_obj.decrypt(Encry_text)
3. Printing Results
Now let’s have a print the encrypted and decrypted message we obtained.
print("The Encrypted text is: ", cipher_text)
print("\nThe Decrypted text is: ",plain_text)
The output looked something like what’s shown below.
The Encrypted text is: b'gAAAAABgsSrnZRaDQbApvKL_xiXfCXHV_70u5eXZKDqYIkMKwxochYNy0lmVrvPFtQWya22MZh92rimscuA5VBuoN-B5YfCRHvpBYhKsbIiuPuz-CklJ-EFyZtZ_S7TRe-b9VSoee03Z8jkxwQpR8FatZ1XWA7xZvm5WpGSQFZkN8w7Ix8riyOo='
The Decrypted text is: b'I am a secret! I will get encrypted into something you will never understand'
Conclusion
Congratulations! Today you learned about Cryptography and how to implement the same on your own. Try the same out yourself and keep your messages a secret from the outside world! Happy Coding!