Cryptography Basics
The math-based techniques that keep your data secret and verified, even over an open network.
Why Networks Need Cryptography
Data traveling across a network can pass through many devices you don't control (routers, ISPs) before reaching its destination. Cryptography ensures that even if someone intercepts this data, they can't read it (confidentiality) or tamper with it undetected (integrity) โ without cryptography, essentially all sensitive online activity (banking, private messaging) would be dangerously exposed.
Symmetric Encryption
Symmetric encryption uses the SAME key for both encrypting and decrypting data. It's fast and efficient, making it ideal for encrypting large amounts of data โ but it has a critical challenge: both parties need to somehow securely share that same secret key beforehand, without anyone else intercepting it during that exchange.
Asymmetric Encryption
Asymmetric (public-key) encryption uses TWO mathematically linked keys: a public key (freely shared with anyone) and a private key (kept strictly secret). Data encrypted with someone's public key can ONLY be decrypted with their matching private key. This elegantly solves symmetric encryption's key-sharing problem โ you can openly share your public key without any risk, since only your private key can decrypt what it encrypts.
How They Work Together in Practice (HTTPS)
In practice, most secure connections (like HTTPS) use BOTH types together for efficiency: asymmetric encryption is used briefly at the START of a connection (during the TLS handshake) to securely exchange a temporary symmetric key. Then, that fast symmetric key encrypts the actual bulk of the session's data โ combining asymmetric's secure key exchange with symmetric's raw speed.
Hashing โ A Different but Related Tool
Hashing is distinct from encryption โ it's a ONE-WAY function that converts data into a fixed-size 'fingerprint' (hash), and it's designed to be practically impossible to reverse back into the original data. Hashing is used to verify data hasn't been tampered with (comparing hashes before/after transfer) and to securely store passwords (storing a password's hash instead of the actual password, so even a database breach doesn't expose real passwords).
๐ Real-World Use
Every HTTPS website you visit uses exactly this asymmetric-then-symmetric combination during its TLS handshake. Password storage systems (when done correctly) never store your actual password โ they store a HASH of it, and check your login attempt by hashing what you typed and comparing it to the stored hash.
๐ก Pro Tip
A very commonly asked interview question: 'why not just use asymmetric encryption for everything, since it's more secure?' โ the honest answer is performance: asymmetric encryption is computationally much slower than symmetric encryption, making it impractical for encrypting large volumes of ongoing data โ this is exactly why HTTPS uses asymmetric only briefly, for the initial key exchange.
๐งช Quick Self-Test
Check what you just learned โ no pressure, just practice.
1. What is the key difference between symmetric and asymmetric encryption?
2. What is hashing primarily used for?
3. Why does HTTPS use asymmetric encryption only briefly, at the start of a connection?