Cybersecurity — Encryption
Symmetric encryption
from cryptography.fernet import Fernet
# Generate key
key = Fernet.generate_key()
cipher = Fernet(key)
# Encrypt
plaintext = b"secret message"
encrypted = cipher.encrypt(plaintext)
# Decrypt
decrypted = cipher.decrypt(encrypted)
AES encryption
from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes
key = get_random_bytes(16)
cipher = AES.new(key, AES.MODE_EAX)
plaintext = b"secret data"
ciphertext, tag = cipher.encrypt_and_digest(plaintext)
Asymmetric encryption
from cryptography.hazmat.primitives.asymmetric import rsa, padding
from cryptography.hazmat.primitives import hashes
# Generate key pair
private_key = rsa.generate_private_key(public_exponent=65537, key_size=2048)
public_key = private_key.public_key()
# Encrypt
encrypted = public_key.encrypt(
b"secret",
padding.OAEP(mgf=padding.MGF1(algorithm=hashes.SHA256()), algorithm=hashes.SHA256(), label=None)
)
# Decrypt
decrypted = private_key.decrypt(
encrypted,
padding.OAEP(mgf=padding.MGF1(algorithm=hashes.SHA256()), algorithm=hashes.SHA256(), label=None)
)
Key management
- Rotate keys regularly
- Store keys securely
- Use HSM for critical keys
- Never hardcode keys
Best practices
- Use strong algorithms
- Implement proper key management
- Encrypt at rest and in transit
- Use appropriate encryption modes
Mini Practice
- Implement symmetric encryption
- Use asymmetric encryption
- Set up key rotation
- Test encryption/decryption
Up Next
Continue with Cryptography - Security algorithms.
Related Topics
Frequently Asked Questions about Encryption
What is Encryption in Cybersecurity?
Encryption is a fundamental concept in Cybersecurity. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Encryption?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Encryption.
Why is Encryption important in Cybersecurity?
Encryption is essential for Cybersecurity development. Understanding this concept will help you write better code and solve real-world problems more effectively.