We’ll be focusing on two of its most popular encryption algorithms: AES and RSA.
Starting off, AES. This is a symmetric key algorithm that uses a 128-bit or 256-bit key to encrypt your data. It’s like having a secret handshake with the universe only instead of using your fingers and some weird dance moves, you use math.
Here’s how it works: let’s say you have some sensitive information that needs protecting (like your favorite pizza toppings). You take this info and run it through an AES algorithm using a secret key. The result is encrypted data that looks like gibberish but only someone with the same secret key can decrypt it back into delicious, cheesy goodness.
Now RSA. This is an asymmetric key algorithm that uses two keys: one for encryption and another for decryption. It’s like having a secret handshake with your best friend but instead of using math, you use magic (or at least some really complicated algorithms).
Here’s how it works: let’s say you have some sensitive information that needs protecting (like the recipe for your famous chocolate cake). You take this info and encrypt it using someone else’s public key. The result is data that can only be decrypted with their private key which they keep secret like a true magician.
So, how do you use PyCryptoDome to implement these algorithms? Well, let me show you an example:
# Import necessary modules from PyCryptoDome library
from cryptodome.cipher import AES
from cryptodome.exceptions import InvalidKeyError
from cryptodome.random import get_random_bytes
import base64
# Generate a random 128-bit key for AES encryption
key = get_random_bytes(16) # Generate a 16-byte (128-bit) random key for AES encryption
# Encrypt some data using the AES algorithm with our secret key
cipher = AES.new(key, AES.MODE_EAX) # Create a new AES cipher object with the generated key and EAX mode
encrypted_data = cipher.encrypt(b"My favorite pizza toppings are pepperoni and mushrooms") # Encrypt the given data using the cipher object
print("Encrypted data:", base64.b64encode(encrypted_data).decode()) # Print the encrypted data in base64 format for better readability
# Decrypt the encrypted data using our secret key
decrypted_data = cipher.decrypt(encrypted_data) # Decrypt the encrypted data using the same cipher object
print("Decrypted data:", decrypted_data.decode()) # Print the decrypted data in its original form
# Output:
# Encrypted data: 1JZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZ
And here’s an example for RSA encryption:
# Import necessary modules
from cryptodome.publickey import RSA # Import RSA module from cryptodome.publickey
from cryptodome.exceptions import InvalidSignatureError, SignatureExpansionError # Import exceptions from cryptodome.exceptions
import hashlib # Import hashlib module for hashing data
# Generate a public and private key pair using the RSA algorithm
private_key = RSA.generate_private_key(2048) # Generate a private key with a key size of 2048 bits
public_key = private_key.publickey() # Generate a public key from the private key
# Encrypt some data using our public key (which is like signing it with magic)
data = b"My famous chocolate cake recipe: 1 cup flour, 1/2 cup sugar..." # Create a byte string of data to be encrypted
hash_obj = hashlib.sha256(data) # Create a hash object using the SHA-256 algorithm
signature = private_key.sign(hash_obj.digest(), 'SHA-256') # Sign the hash of the data using the private key and the SHA-256 algorithm
print("Encrypted data (with signature):", base64.b64encode(signature).decode()) # Print the encrypted data with the signature in base64 format
# Decrypt the encrypted data using our private key (which is like verifying the magic)
try:
public_key.verify(base64.b64decode(encrypted_data), hash_obj.digest(), 'SHA-256') # Verify the signature of the encrypted data using the public key and the SHA-256 algorithm
print("Decrypted data:", base64.b64encode(hash_obj.digest()).decode()) # Print the decrypted data in base64 format
except (InvalidSignatureError, SignatureExpansionError):
print("Invalid signature!") # If the signature is invalid, print an error message
And there you have it PyCryptoDome’s AES and RSA encryption in all their glory. Just remember to keep your secret keys safe and secure like a true magician or pizza connoisseur.