Introduction to Python Cryptography: Encryption, Decryption, and Hashing
267
Introduction
Cryptography, the practice of secure communication, is essential for protecting sensitive information. Python provides robust tools for implementing cryptographic techniques. This blog will guide you through the basics of cryptography in Python, covering encryption, decryption, and hashing using the cryptography
library.
Steps
Step 1: Installing the Cryptography Library
First, install the cryptography
library if you haven't already. You can install it using pip:
pip install cryptography
Step 2: Understanding Symmetric Encryption and Decryption
Symmetric encryption uses the same key for both encryption and decryption. The cryptography
library makes it easy to implement symmetric encryption with the Fernet module.
from cryptography.fernet import Fernet def encrypt_message(message, key): """ Encrypts a message using the provided key. :param message: The message to encrypt in bytes. :param key: The encryption key in bytes. :return: The encrypted message in bytes. """ cipher = Fernet(key) encrypted_message = cipher.encrypt(message) return encrypted_message
def decrypt_message(encrypted_message, key):
"""
Decrypts an encrypted message using the provided key.
:param encrypted_message: The encrypted message in bytes.
:param key: The decryption key in bytes.
:return: The decrypted message as a string.
"""
cipher = Fernet(key)
decrypted_message = cipher.decrypt(encrypted_message)
return decrypted_message.decode()
#Generate a random symmetric key
key = Fernet.generate_key()
# Original message
message = b"Secret message"
# Encrypt the message
encrypted_message = encrypt_message(message, key)
print(f"Encrypted message: {encrypted_message}")
# Decrypt the message
decrypted_message = decrypt_message(encrypted_message, key)
print(f"Decrypted message: {decrypted_message}")
Step 3: Implementing Asymmetric Encryption and Decryption
Asymmetric encryption uses a pair of keys: a public key for encryption and a private key for decryption. This method is commonly used for secure communications.
from cryptography.hazmat.primitives.asymmetric import rsa from cryptography.hazmat.primitives import serialization from cryptography.hazmat.primitives.asymmetric import padding from cryptography.hazmat.primitives import hashes # Generate RSA keys private_key = rsa.generate_private_key( public_exponent=65537, key_size=2048 ) public_key = private_key.public_key() # Serialize the keys pem_private_key = private_key.private_bytes( encoding=serialization.Encoding.PEM, format=serialization.PrivateFormat.PKCS8, encryption_algorithm=serialization.NoEncryption() ) pem_public_key = public_key.public_bytes( encoding=serialization.Encoding.PEM, format=serialization.PublicFormat.SubjectPublicKeyInfo ) # Encrypting a message message = b"Secret message" encrypted_message = public_key.encrypt( message, padding.OAEP( mgf=padding.MGF1(algorithm=hashes.SHA256()), algorithm=hashes.SHA256(), label=None ) ) print(f"Encrypted message: {encrypted_message}") # Decrypting the message decrypted_message = private_key.decrypt( encrypted_message, padding.OAEP( mgf=padding.MGF1(algorithm=hashes.SHA256()), algorithm=hashes.SHA256(), label=None ) ) print(f"Decrypted message: {decrypted_message.decode()}")
Step 4: Hashing Data
Hashing is a one-way cryptographic function that converts data into a fixed-size string of characters. It's commonly used for data integrity verification.
from cryptography.hazmat.primitives import hashes # Hashing a message digest = hashes.Hash(hashes.SHA256()) digest.update(b"Message to hash") hashed_message = digest.finalize() print(f"Hashed message: {hashed_message.hex()}")
Conclusion
Cryptography is crucial for securing data in our digital world. Python's cryptography
library offers comprehensive tools for implementing encryption, decryption, and hashing. By understanding and utilizing these techniques, you can protect sensitive information effectively. Whether you're securing communications or ensuring data integrity, cryptographic methods provide the necessary security layer to keep your data safe.