Cryptography Basics
Cryptography underpins almost every security control: HTTPS, password storage, and secure messaging all depend on it. You don't need to implement algorithms yourself, but you do need to know which tool fits which job.
Hashing vs. encryption
These are often confused, but solve different problems:
| Hashing | Encryption | |
|---|---|---|
| Direction | One-way, cannot be reversed | Two-way, can be decrypted with the right key |
| Use case | Verifying passwords, detecting tampering | Protecting data you need to read again later |
| Example | bcrypt, SHA-256 | AES, RSA |
Never store passwords in plain text, and never encrypt them either, always hash them with a slow, salted algorithm designed for passwords, like bcrypt:
bcrypt is deliberately slow and includes a random salt per password, so two identical passwords produce different hashes, and brute-forcing many hashes at once can't be sped up with a precomputed table (a "rainbow table").
Symmetric vs. asymmetric encryption
- Symmetric: the same key encrypts and decrypts (AES). Fast, but both parties need to already share the key securely.
- Asymmetric: a public key encrypts, only the matching private key can decrypt (RSA). Slower, but solves the key-sharing problem, you can publish your public key freely.
TLS/HTTPS
HTTPS uses both: an asymmetric handshake to securely agree on a shared secret, then fast symmetric encryption for the actual data transfer. This is why the browser padlock icon means the connection is encrypted and the server's identity was verified by a certificate, but it says nothing about whether the site itself is trustworthy.
TIP
"Encrypted" is not the same as "secure": a phishing site can have a valid HTTPS certificate too. TLS protects data in transit, it doesn't vouch for who's on the other end.