Encryption and hashing are fundamental techniques used to secure data in Python applications. Encryption involves converting plaintext data into ciphertext using an encryption algorithm and a secret key, while hashing generates a fixed-size string of characters (hash value) from input data using a hash function.
Encryption is the process of converting plaintext data into ciphertext using an encryption algorithm and a secret key. Encrypted data can only be decrypted back to its original form using the corresponding decryption key.
Hashing is the process of converting data of any size into a fixed-size string of characters, typically using a hash function. Hash functions generate a unique hash value for each unique input, making it useful for data integrity verification and password storage.
Symmetric encryption uses a single key for both encryption and decryption. It’s fast and efficient but requires securely sharing the key between the sender and receiver.
from cryptography.fernet import Fernet
# Generate a symmetric key
key = Fernet.generate_key()
# Create a Fernet symmetric encryption object
cipher = Fernet(key)
# Encrypt plaintext
plaintext = b"Hello, world!"
encrypted_text = cipher.encrypt(plaintext)
# Decrypt ciphertext
decrypted_text = cipher.decrypt(encrypted_text)
print("Plaintext:", plaintext)
print("Encrypted Text:", encrypted_text)
print("Decrypted Text:", decrypted_text)
In this example, we generate a symmetric key using Fernet.generate_key()
, create a Fernet
cipher object with the key, and then use it to encrypt and decrypt the plaintext.
SHA-256 is a cryptographic hash function that produces a 256-bit (32-byte) hash value. It’s commonly used for data integrity verification and password hashing.
import hashlib
# Hash plaintext using SHA-256
plaintext = b"Hello, world!"
hashed_text = hashlib.sha256(plaintext).hexdigest()
print("Plaintext:", plaintext)
print("Hashed Text:", hashed_text)
hashlib.sha256()
, and then convert the resulting hash value to a hexadecimal string using .hexdigest()
.Asymmetric encryption uses a pair of keys: a public key for encryption and a private key for decryption. It’s slower than symmetric encryption but offers enhanced security and key distribution.
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.hazmat.backends import default_backend
# Generate RSA key pair
private_key = rsa.generate_private_key(
public_exponent=65537,
key_size=2048,
backend=default_backend()
)
public_key = private_key.public_key()
# Encrypt plaintext with public key
plaintext = b"Hello, world!"
encrypted_text = public_key.encrypt(
plaintext,
padding.OAEP(
mgf=padding.MGF1(algorithm=hashes.SHA256()),
algorithm=hashes.SHA256(),
label=None
)
)
# Decrypt ciphertext with private key
decrypted_text = private_key.decrypt(
encrypted_text,
padding.OAEP(
mgf=padding.MGF1(algorithm=hashes.SHA256()),
algorithm=hashes.SHA256(),
label=None
)
)
print("Plaintext:", plaintext)
print("Encrypted Text:", encrypted_text)
print("Decrypted Text:", decrypted_text)
In this example, we generate an RSA key pair, encrypt the plaintext using the public key, and then decrypt the ciphertext using the private key.
Digital signatures use asymmetric cryptography to provide data integrity and non-repudiation. They involve signing data with a private key and verifying the signature with the corresponding public key.
KDFs derive cryptographic keys from a given secret value, such as a password. They enhance security by generating keys with sufficient entropy and resistance to brute-force attacks.
In this topic, we explored the essential concepts and techniques of encryption and hashing in Python. We started with basic techniques like symmetric encryption and hashing with popular algorithms like SHA-256. Then, we moved on to more advanced concepts such as asymmetric encryption using RSA and digital signatures.Encryption and hashing are indispensable tools for securing data in Python applications. By encrypting sensitive information and hashing passwords and other data, developers can protect against unauthorized access and ensure data integrity. Happy coding! ❤️