Hello, readers. Want to try out Cryptography in Python? As a kid, I loved reading books on how to encrypt and decrypt secret messages. So here I bring to you some interesting implementations of cryptography in Python. Let’s get started.
Table of Contents
- 1 What is cryptography?
- 2 What are the features of good cryptography algorithms?
- 3 Types of Cryptography
- 4 Implementing Cryptography in Python with FERNET
- 5 Ending Note
What is cryptography?
Cryptography protects data and correspondence with the use of codes. So, it can be interpreted and accessed by only certain persons by whom the information is intended.
Unauthorized access is thereby avoided. “The “crypt” prefix means “encrypted” and the graphic suffix means reading”.
The mechanisms to encrypt knowledge derive their roots from mathematical principles in cryptography. And, a series of rule-based formulas as algorithms to transform messages in ways that make it impossible to decipher them.
These algorithms generate cryptographic keys, digital signing, data protection authentication, internet web browsing, and sensitive transactions such as credit card and debit card transactions.
What are the features of good cryptography algorithms?
- Confidentiality: Information is accessible only for the intended user and by no one else.
- Completeness: Information cannot be altered between the sender and the intended recipient in storage or transfer without any addition to the information being detected.
- Non-rejecting: At a later point, the creator/sender of data does not reject his or her intent to send information.
- Authentication: It confirms the identity of the sender and the recipient. The details are verified as well as the destination/origin.
Types of Cryptography
- Symmetric Key: It is an encryption scheme where a single universal key is used by the sender and recipient of messages to encrypt and decrypt messages. Symmetric Key Schemes are quicker and easier, but the issue is that in a safe way, the sender and recipient have to swap the key somewhere. The Data Encryption Method(DE) is the most common symmetric key cryptography system.
- Functions of Hash: There is no use in this algorithm of any key. A fixed-length hash value is computed as per the plain text, which makes it difficult to retrieve plain text content. Hash functions are used by many operating systems to encrypt passwords.
- Asymmetric Key: Information is encrypted and decrypted using a pair of keys. For encryption, a public key is used and a private key is used for decryption. The private key and public key are separate. And if anyone knows the public key, the intended recipient will only decipher it because he knows the private key himself.
Implementing Cryptography in Python with FERNET
The framework I am first going to introduce today is called Fernet. It is a symmetric key encryption technique.
1. Install FERNET
We can simply use Python pip to install the FERNET module on our system:
!pip install cryptography from cryptography.fernet import Fernet
2. Generate Encryption Keys
We can use the Fernet function directly:
key = Fernet.generate_key()
We can also create our own key using the random function
import base64 import os base64.urlsafe_b64encode(os.urandom(32))
And then we’ll call the FERNET function on the key.
f = Fernet(key)
3. Encrypt Our Data
Now we are ready to encrypt our data:
token = f.encrypt(b"Meet me at 9pm. The bus stop. Bring the case.") print(token)
As you can see from the resulting token:
Without the key, the above data is complete illegible. And so, a malicious user trying to get hold of the information will be stuck without the key.
4. Using Passwords for Cryptography in Python
We can also use specific passwords, instead of saving randomly generated tokens.
So, for this, we have to get into primitives, which is a little dangerous and can be used incorrectly.
They require making decisions and having an in-depth knowledge of the cryptographic concepts at work.
But follow this code below and you should be fine:
from cryptography.hazmat.primitives import hashes from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
Now we specify the password, and generate a random byte sequence:
password = b"journaldev" salt = os.urandom(16) print(salt)
which gives us:
Then, we use kdf to iterate 100k times using a hashing algorithm like SHA256 to get the key:
kdf = PBKDF2HMAC(algorithm=hashes.SHA256(), length=32, salt=salt, iterations=100000, )
We need to use this key to encrypt and decrypt.
key = base64.urlsafe_b64encode(kdf.derive(password)) print(key)
5. Decrypting Text
Once the receiver gets the message, the steps to decrypt the text are easy. So, you start out with entering the key in the Fernet object, and use the decrypt() function as below:
f = Fernet(#enter your key here) f.decrypt(token)
You can send secret messages to your friends, of course. But instead, you can combine this with a chatbot or messaging applications to have real-time safe communication.
We hope you enjoyed reading through the article and have gained a lot of information from it! What are your next steps now? If you are venturing into machine learning, don’t forget to check out our beginners guide to machine learning here.
And, for those of you who are already working on Machine learning models, here are some of the top machine learning datasets that you can use for your upcoming projects.
Stay tuned for more tutorials on Python!