Click the blue words to follow us
Beginner: Jing Ge, I’ve been researching data security recently and heard that Python has a great library called cryptography. Can you tell me how to use it? It seems a bit complicated, and I don’t know where to start.
Jing Ge: No problem! The cryptography library is indeed powerful, but it’s not difficult to get started. Today, we will explore this cryptographic toolkit together, so you can quickly master practical skills to protect data security.
Python Encryption Tool: Cryptography Library
What is Cryptography?
Cryptography is a feature-rich Python library that provides various encryption algorithms and tools to protect the confidentiality and integrity of data. It supports symmetric encryption, asymmetric encryption, hashing algorithms, digital signatures, and other cryptographic operations to meet various security needs.
Installing Cryptography
Before we start, we need to install the cryptography library. It’s very simple to install using pip:
pip install cryptography
Symmetric Encryption: Fernet
Symmetric encryption uses the same key for both encryption and decryption. Fernet is the recommended symmetric encryption algorithm in the cryptography library, which is easy to use and secure.
from cryptography.fernet import Fernet
# Generate a key
key = Fernet.generate_key()
f = Fernet(key)
print(f"Key: <font color="blue">{key.decode()}</font>")
# Encrypt data
message = b"This is a secret message" # Note: Data to be encrypted must be of bytes type
encrypted_message = f.encrypt(message)
print(f"Encrypted data: <font color="red">{encrypted_message.decode()}</font>")
# Decrypt data
decrypted_message = f.decrypt(encrypted_message)
print(f"Decrypted data: <font color="green">{decrypted_message.decode()}</font>")
Tip: The key generated by Fernet is very important, so make sure to keep it safe! Losing the key means you cannot decrypt the data.
Hashing Algorithm: SHA256
A hashing algorithm can convert data of any length into a fixed-length hash value, commonly used to verify data integrity. The cryptography library provides various hashing algorithms, such as SHA256.
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.backends import default_backend
digest = hashes.Hash(hashes.SHA256(), backend=default_backend())
digest.update(b"This is a message to be hashed")
hashed_message = digest.finalize()
print(f"Hash value: <font color="blue">{hashed_message.hex()}</font>")
digest.update(b"This is a different message")
another_hashed_message = digest.finalize()
print(f"Another hash value: <font color="red">{another_hashed_message.hex()}</font>")
Note: Even a slight change in the input data will result in a completely different hash value. This makes hashing algorithms very suitable for detecting data tampering.
Asymmetric Encryption: RSA
Asymmetric encryption uses a pair of keys: a public key and a private key. The public key can be shared publicly for encrypting data, while the private key must be kept secret for decrypting data. RSA is a commonly used asymmetric encryption algorithm.
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.hazmat.primitives import serialization
# 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()
# Serialize the keys to PEM format
private_pem = private_key.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.PKCS8,
encryption_algorithm=serialization.NoEncryption()
)
public_pem = public_key.public_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PublicFormat.SubjectPublicKeyInfo,
)
print(f"Private key: <font color="red">{private_pem.decode()}</font>")
print(f"Public key: <font color="green">{public_pem.decode()}</font>")
# ... (subsequent operations can use these keys for encryption and decryption)
Tip: Asymmetric encryption is computationally intensive and is typically used to encrypt small amounts of data, such as symmetric encryption keys.
Friends, today’s journey into Python learning ends here! Thank you all for your support! I hope you can quickly master Python knowledge and become a Python expert soon!
Writing is not easy, feel free to forward this to your friends, let money and love flow to you
Tap ‘View’ to recommend it
View