After this lesson, you will be able to: Understand symmetric/asymmetric encryption and encrypt files hands-on with Python.
Encryption converts readable plaintext into ciphertext using a key. Done right, it makes leaked data useless to the attacker. This lesson covers the math at a conceptual level and gets you encrypting files with Python's `cryptography` library.
Symmetric (AES), one key encrypts and decrypts. Fast, ideal for bulk data. Problem: how do you share the key? Asymmetric (RSA, ECC), keypair: public encrypts, private decrypts. Slow but solves key sharing. Used to wrap symmetric keys (TLS, S/MIME).
Using the `cryptography` library, install with `pip install cryptography`.
from cryptography.fernet import Fernet# 1. Generate a key (save this securely!)key = Fernet.generate_key()f = Fernet(key)# 2. Encryptwith open('secret.txt', 'rb') as src:plaintext = src.read()ciphertext = f.encrypt(plaintext)with open('secret.txt.enc', 'wb') as dst:dst.write(ciphertext)# 3. Decryptwith open('secret.txt.enc', 'rb') as src:decrypted = f.decrypt(src.read())print(decrypted)
SHA-256, bcrypt, argon2 are one-way functions, used for passwords, integrity. You cannot 'decrypt' a hash. Confusing the two is a classic interview trap.
Sign in and purchase access to unlock this lesson.