Encryption explained: how a key turns data into ciphertext, symmetric vs asymmetric encryption, how HTTPS uses both, and why passwords are hashed instead.
Encryption, explained by cats. Kitty has a secret. Very important tuna plans. She wants to send the message to another cat. But the internet is full of snooping cats. Anyone could read it. So Kitty uses encryption. She takes her message and locks it in a box.
Only the right key can open it. Now if a sneaky cat intercepts the message, they see gibberish. No tuna. No clues. The cat on the other side has the key. They unlock the box. Message revealed. That’s encryption. Sometimes both cats share the same key.
That’s symmetric encryption. Fast. Simple. Sometimes they use two keys. One to lock. One to unlock. That’s asymmetric encryption. Websites use encryption every day. Passwords. Messages. Payments. Encryption keeps secrets a secret. Keeps data safe. And keeps snooping cats out of your tuna.
Encryption scrambles data so that only someone holding the right key can read it. Anyone else who gets hold of it, on the network or on a stolen laptop, sees gibberish.
What encryption does
Every encryption scheme has the same four parts:
Plaintext: the readable data, such as a message or a card number.
Ciphertext: the scrambled version that travels or sits on disk.
The algorithm (or cipher): the recipe that turns one into the other.
The key: a value the algorithm uses to lock or unlock the data. Symmetric keys and private keys are secret; public keys are shared.
The algorithm is not the secret. Well-known ciphers such as AES are published and attacked by researchers for years before anyone trusts them. All of the security lives in the key. That is why a good cipher with a leaked key protects nothing, and why a secret home-made cipher with no outside review is a bad bet.
Decryption is the reverse trip: ciphertext plus the right key gives back the plaintext. With authenticated encryption, such as Fernet below, a wrong key makes decryption fail rather than return unauthenticated data.
A worked example in Python
Here is symmetric encryption with the cryptography package's Fernet recipe, which wraps a standard cipher and a tamper check behind two calls:
from cryptography.fernet import Fernetfrom cryptography.fernet import InvalidToken
Encryption, explained by cats. Kitty has a secret. Very important tuna plans. She wants to send the message to another cat. But the internet is full of snooping cats. Anyone could read it. So Kitty uses encryption. She takes her message and locks it in a box.
Only the right key can open it. Now if a sneaky cat intercepts the message, they see gibberish. No tuna. No clues. The cat on the other side has the key. They unlock the box. Message revealed. That’s encryption. Sometimes both cats share the same key.
That’s symmetric encryption. Fast. Simple. Sometimes they use two keys. One to lock. One to unlock. That’s asymmetric encryption. Websites use encryption every day. Passwords. Messages. Payments. Encryption keeps secrets a secret. Keeps data safe. And keeps snooping cats out of your tuna.
The token is safe to send over an open network or store in a database. Without key it is useless, and the last few lines show that a different key fails outright.
Symmetric encryption: one shared key
In symmetric encryption, the same key locks and unlocks the data. The Fernet example above is symmetric, and so is AES, the cipher behind most encrypted traffic and disks today.
Symmetric ciphers are fast. Modern processors have instructions built for AES, so encrypting gigabytes costs very little. That makes symmetric encryption the workhorse for bulk data: files, database backups, video calls, every byte of a web page.
The problem is key distribution. Both sides need the same key, and you can't just send it in a message, because anyone listening would grab it along with everything it protects. Two people could meet and swap keys in person, but a browser and a shop it has never visited can't. The number of keys also grows quickly: every pair of people who want to talk privately needs their own key.
Asymmetric encryption: a pair of keys
Asymmetric encryption, also called public-key cryptography, uses two keys that are made together as a pair:
the public key, which you can hand to anyone;
the private key, which never leaves its owner.
Data locked with the public key can only be unlocked with the matching private key. Think of the public key as an open padlock you give out freely: anyone can snap it shut on a box, but only you hold the key that opens it. RSA and elliptic-curve cryptography are the common families.
This solves the problem symmetric encryption has. A stranger can send you something private without any earlier meeting, because the key they need is public.
The cost is speed. Asymmetric operations are much slower than symmetric ones and are used on small pieces of data, such as a key, so nobody encrypts a whole video stream this way.
Public-key cryptography also supports digital signatures: you sign with a signing private key, and anyone can check the signature with its public key. Signing is a separate operation, not encryption run backwards, and some key types support only signing. A signature shows who sent something rather than hiding it, and it is how a website proves it is really the site you asked for.
How websites use both
In practice, systems combine the two, playing to each one's strength. When you open a site over HTTPS, the TLS protocol roughly does this:
The browser and the server agree on a fresh symmetric key for this session, using a key exchange such as Diffie-Hellman. The key itself is never sent.
The server sends its certificate, which holds its public key and is signed by an authority your browser already trusts, and signs the handshake with its private key.
The browser checks the certificate and the signature, so it knows it is talking to the real site and not an impostor.
Everything after that, including your password, messages and card details, is encrypted with the fast symmetric session key.
Asymmetric cryptography handles the trust and the introduction, and symmetric encryption carries the data. Picture paying for something on café Wi-Fi. Someone on the same network can capture every packet you send, but all they collect is ciphertext, and the session key that would unlock it was never sent across the network.
In transit and at rest
Encryption is usually described by where the data is:
In transit: data moving between machines. HTTPS and SSH protect data on the move. End-to-end encrypted messaging goes a step further, so that only the two devices in the conversation hold the keys, not the company running the service.
At rest: data sitting in storage. Full-disk encryption on laptops and phones, and encrypted database backups, mean that a stolen drive or a leaked backup file is unreadable without the key.
Most systems need both. HTTPS does nothing for a database dump left in a public bucket, and an encrypted disk does nothing for traffic sent over plain HTTP.
Encryption is not hashing or encoding
Three ideas often get mixed up:
Encryption is reversible with the key. Use it when you need the original data back later.
Hashing is one-way. It turns data into a fixed-size fingerprint that cannot be turned back. Use a salted, slow password-hashing algorithm such as bcrypt or Argon2 to store passwords. Hashes cannot be decrypted, but attackers can try guesses and compare the results; slow hashing makes each guess more costly.
Encoding, such as Base64, is reversible by anyone, with no key at all. It changes the format of data, not who can read it.
Passwords show the difference well. Your password is encrypted on its way to a site, inside HTTPS. Once it arrives, a well-built site stores a hash of it, not an encrypted copy, so that even the site cannot read it back.
Common mistakes
Writing your own cipher: use your platform's vetted library and its high-level recipes. Small mistakes in home-made cryptography break it completely, and the code still appears to work.
Hard-coding keys: a key committed to a repository is a leaked key. Keep keys in a secrets manager or key management service, and rotate them.
Encrypting passwords instead of hashing them: if the key leaks, every password leaks with it.
Treating Base64 as protection: it hides nothing from anyone who recognises it.
Using outdated algorithms: DES and other retired ciphers can be broken. Stick to what current guidance recommends, such as AES.
Forgetting tampering: encryption hides data, but an attacker can still alter ciphertext. Modern authenticated modes such as AES-GCM, and recipes like Fernet, detect changes and refuse to decrypt.
Key takeaways
Encryption turns readable data into ciphertext that only the right key can turn back.
Symmetric encryption uses one shared key: fast, but the key has to be shared safely.
Asymmetric encryption uses a public key to lock and a private key to unlock: slower, but no shared secret is needed up front.
HTTPS uses both: public-key cryptography to set up a session key, then symmetric encryption for the data.
Encrypt data you need back, hash passwords, and never treat encoding as security.