Cryptography is a vital aspect of securing information, and Python provides an easy-to-use platform for implementing cryptographic techniques. Whether you’re interested in securing your data or learning more about how encryption works, these examples will help you understand the basics of cryptography in Python. Let’s dive into three practical examples that you can use for your science fair project!
Imagine you want to send a secret message to a friend without anyone else being able to read it. Using the cryptography
library in Python, you can easily encrypt and decrypt messages with a symmetric key. This means both you and your friend will use the same key to unlock the message.
First, make sure to install the cryptography
library if you haven’t already:
pip install cryptography
Next, you can use the following code to encrypt and decrypt a message:
from cryptography.fernet import Fernet
# Generate a key
key = Fernet.generate_key()
fernet = Fernet(key)
# Original message
message = b"This is a secret message."
# Encrypt the message
encrypted_message = fernet.encrypt(message)
print(f"Encrypted: {encrypted_message}")
# Decrypt the message
decrypted_message = fernet.decrypt(encrypted_message)
print(f"Decrypted: {decrypted_message.decode()}")
When creating a website where users can log in, it’s essential to store their passwords securely. Instead of saving passwords in plain text, you can hash them using Python. Hashing transforms the password into a fixed-size string of characters that cannot be reversed.
You can use the bcrypt
library for hashing passwords. First, install it:
pip install bcrypt
Now, you can hash and check a password as follows:
import bcrypt
# Hash a password
password = b"my_secure_password"
hash = bcrypt.hashpw(password, bcrypt.gensalt())
print(f"Hashed Password: {hash}")
# Check if the provided password matches the hash
if bcrypt.checkpw(password, hash):
print("Password matches!")
else:
print("Password does not match!")
Digital signatures are used to verify the authenticity of a message or document. If you want to ensure that a document hasn’t been altered and is indeed from the claimed sender, you can use a digital signature.
You can use the cryptography
library to create a digital signature:
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import rsa, padding
# Generate a private key
private_key = rsa.generate_private_key(
public_exponent=65537,
key_size=2048,
backend=default_backend()
)
# Generate a public key
public_key = private_key.public_key()
# Original message
message = b"This is a document."
# Sign the message
signature = private_key.sign(
message,
padding.PSS(
mgf=padding.MGF1(hashes.SHA256()),
salt_length=padding.PSS.MAX_LENGTH
),
hashes.SHA256()
)
print(f"Signature: {signature}")
# Verify the signature
try:
public_key.verify(
signature,
message,
padding.PSS(
mgf=padding.MGF1(hashes.SHA256()),
salt_length=padding.PSS.MAX_LENGTH
),
hashes.SHA256()
)
print("Signature is valid.")
except Exception as e:
print("Signature is invalid:", e)
These examples of implementing cryptography in Python not only demonstrate how to secure your data but also provide a great foundation for understanding more complex cryptographic concepts. Good luck with your science fair project!