</>
Skip to content
Cybersecurity lessons (13/46)

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

  1. Rotate keys regularly
  2. Store keys securely
  3. Use HSM for critical keys
  4. Never hardcode keys

Best practices

  1. Use strong algorithms
  2. Implement proper key management
  3. Encrypt at rest and in transit
  4. Use appropriate encryption modes

Mini Practice

  1. Implement symmetric encryption
  2. Use asymmetric encryption
  3. Set up key rotation
  4. 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.