UNLOCKING SECRETS: PYTHON'S POWER IN CRYPTOGRAPHY
Understanding cryptographic algorithms is crucial as they form the basis of cryptography, ensuring data confidentiality, integrity, and authenticity. These algorithms are broadly classified into Symmetric Key Algorithms and Asymmetric Key Algorithms. Symmetric Key Algorithms, for instance, use a single secret key for both encryption and decryption. The AES (Advanced Encryption Standard), a widely used symmetric encryption, provides robust security with different key lengths such as 128, 192, and 256 bits. It's particularly useful for bulk encryption/decryption tasks. On the other hand, asymmetric Key Algorithms, also known as Public Key Cryptography, use two keys- a public key for encryption (known to everyone) and a private key for decryption (kept secret). RSA (Rivest–Shamir–Adleman) and ECC (Elliptic Curve Cryptography) are the most commonly used asymmetric key algorithms, offering high security for key distribution and digital signatures.
This article shows a practical demonstration of how to perform encryption and decryption using the AES algorithm, leveraging the capabilities of the Python library. cryptography is the Python library that is used for this. It provides a secure way to perform various cryptographic operations, ensuring the data's confidentiality, integrity, and authenticity. The library is designed to be easy to use and understand, making it a popular choice for implementing secure communication and data storage systems.
Before using the cryptography library, it must be installed using the pip install command, as shown below.
pip install cryptography #installs the library
The Fernet class in cryptography.fernet library contains the methods, namely encrypt() and decrypt(), for encrypting and decrypting a message. Further, it contains a static method called generate_key() for generating a random key. So, it needs to be imported using the below command.
from cryptography.fernet import Fernet #importing the library
The gen_key() is a user-defined function that generates a cryptographically secure random key. It contains two statements. The statement Fernet.generate_key() generates 32 bytes of random key, which are stored in secret_key and returned to the caller using return(secret_key).
def gen_key():
secret_key = Fernet.generate_key()
return(secret_key) #returns the secret_key
#calling the gen_key() function
key=gen_key() #key contains the generated key
#Create a Fernet object with the secret key
fernet = Fernet(key) #fernet is the object of Fernet class
# Message to encrypt – original message
Recommended by LinkedIn
message = b"This is a confidential message!"
# Encrypt the message
encrypted_message = fernet.encrypt(message)
#The encrypt method of the Fernet class is called using the object fernet that #encrypts the text in the variable message and stores it in the variable called #encrypted_message.
Note: The Fernet utilizes AES (Advanced Encryption Standard) in CBC (Cipher Block Chaining) mode for symmetric encryption.
# Decrypt the encrypted message
decrypted_message = fernet.decrypt(encrypted_message)
#The decrypt method of the Fernet class decrypts the encrypted_message and #extracts the original message.
# The below statements print the original, encrypted, and decrypted message
print("Original message:", message)
print("Encrypted message:", encrypted_message)
print("Decrypted message:", decrypted_message)
NOTE: the original message and decrypted message remain the same.
CONCLUSION:
The Fernet class methods, viz., encrypt() and decrypt(), are used for AES encryption and decryption. It is noted that the Fernet key must be 32 url-safe base64-encoded bytes.