Encryption is the part of computing where algorithms protect secrets. It is also the part where good intentions go most badly wrong, because a design that looks clever can be broken by someone who knows the field. This article explains the ideas with locks and keys, so you can read about security with understanding. It is deliberately about concepts: it does not teach you to build anything, and it should not be used that way.
Locks, keys and three words
Expert viewKerckhoffs's principle
A long-standing rule in the field, known as Kerckhoffs's principle, says a system should stay secure even if everything about it is public except the key. In practice this means the method is published and examined by many experts, and only the key is secret. Secret, home-made methods ("security through obscurity") have a poor record, because the people who most need to examine them never get the chance.
Symmetric encryption: one shared key
Symmetric (shared key)
- One secret key locks and unlocks
- Fast, good for large amounts of data
- Both sides must somehow share the key safely
Public-key (key pair)
- A public lock anyone can close
- Only the owner's private key opens it
- Slower, so used for small jobs such as protecting keys
Expert viewAuthenticated encryption, in words
Hiding data is not enough: an attacker who cannot read a message might still change it. Modern designs therefore use authenticated encryption, which both hides the data and attaches a check so that any tampering is detected when the message is opened. This is one more reason to use a well-tested library mode and not assemble pieces by hand: the order and combination of the pieces matters, and mistakes are easy to make and hard to see.
Public-key encryption: a lock anyone can close
- Make a key pairYou keep the private key and publish the public key
- Others lock with itAnyone can snap the public padlock shut on a message
- Only you unlockThe private key never leaves your hands
- No shared secret neededThe key-sharing problem is solved
Expert viewWhy not use it for everything?
Public-key methods rest on mathematical problems believed to be hard to reverse, and they are much slower than symmetric methods. So they are typically used for small jobs: agreeing on a shared key, or protecting a short key, and for signatures. Standards in this area evolve, including work on methods designed for a future where much larger computers might exist. Follow current guidance from a trusted standards body and do not rely on summaries like this one for choices.
Hybrid: both together
The slow method is used briefly, to set up a secret that two sides share. The fast method then does the heavy lifting. Nearly every secure connection you use is built this way.
Hashing: a fingerprint, not a lock
Hash
- Turns data into a short fixed-size fingerprint
- Not meant to be reversed
- Any small change gives a very different fingerprint
Encryption
- Turns data into a scrambled form
- Designed to be reversed with the key
- The goal is secrecy
Hashing was covered in the hashing part of this series. Here it matters for two reasons: it detects changes to data, and it is the right tool for storing passwords, because a service should never need to read your password back.
- 1Need to keep the content secret?Yes Encrypt it with a vetted libraryNo keep going
- 2Need to prove who made it and that it is unchanged?Yes Sign it with a private keyNo keep going
- 3Need to detect changes, or store a password you never read back?Yes Hash it (slow and salted for passwords)No keep going
- 4OtherwiseEncoding such as Base64 is none of these: it gives no secrecy
Takeaway. Encrypt to hide, sign to prove, hash to fingerprint.
Digital signatures: proof of who and what
- Signer (to itself) The signer computes a hash of the message
- Signer (to itself) The private key turns it into a signature
- Signer to Checker The message and signature are sent together
- Checker (to itself) The checker hashes the message it received
- Checker (to itself) The public key checks the signature against that hash
Takeaway. A match proves the key holder signed it and the message is unchanged; it does not hide the message.
Expert viewWhat a signature does and does not give
In outline, the signer computes a hash of the message and uses the private key to produce a signature over it. A verifier recomputes the hash and uses the public key to check the signature. Two properties follow: authenticity (it was made by the holder of the private key) and integrity (the message has not changed since signing). A signature does not hide the message, since signing and encrypting are separate operations, and it only proves that the key was used, not that the key's owner is who you assume. That last gap is why certificates exist.
HTTPS in one picture
- Browser to Website Browser and site agree on how to talk
- Website to Browser (reply) The site shows a certificate as proof of its name
- Browser (to itself) The browser checks the signature, the name and the expiry date
- Browser to Website Public-key methods set up a shared secret
- Browser to Website Data now flows under symmetric encryption
Takeaway. Slow public-key methods set up the secret; fast symmetric encryption carries the data.
- HelloBrowser and site agree on how to talk
- CertificateThe site shows proof of its identity
- Agree keysPublic-key methods set up a shared secret
- Fast and privateData flows under symmetric encryption
Expert viewForward secrecy, in words
Some key agreement methods use fresh, temporary values for each connection. The benefit, called forward secrecy, is that if a long-term key is stolen later, old recorded conversations still cannot be unlocked with it. Current standards for secure connections favour designs with this property.
Certificates and authorities
Expert viewTrust is a chain
Authorities can sign certificates for other authorities, forming a chain from the site up to a trusted root. A certificate is checked for a valid signature, a matching name and whether it has expired or been revoked. The weakness is human and organisational: you are trusting the authorities, so the system's safety depends on their care and on the software's checks.
Common mistakes
Also avoid hard-coded keys (they leak with the source code) and storing passwords unhashed (keep only slow, salted hashes).
Base64 hides my data, so it is encrypted
Base64 is an encoding anyone can reverse, with no key and no secrecy
I made up my own cipher, so nobody knows how to break it
Secret methods are weak. Public, widely examined standards are the safe choice
Expert viewKey management is the hard part
In most real incidents the mathematics held and the handling failed: keys were left in code repositories, shared by email, never rotated or kept next to the data they protected. Good practice means generating keys with trusted tools, storing them in purpose-built secret stores or hardware, limiting who can use them, rotating them, and planning for loss. Treat key handling as the main design problem, not an afterthought.
What encryption does not protect
Expert viewLimits worth remembering
Encryption protects data from people who cannot get the key. It does not hide the fact that a conversation happened, it does not help when an attacker controls the device that holds the decrypted data, and it does not fix tricking a person into giving access away. Security is a chain of such links, and a strong cipher is only one of them.
Try it by hand: a toy shift
Pick the key 3 and move each letter three places along the alphabet, wrapping from Z to A. The word CAT becomes FDW. To decrypt, shift back by 3.
- Choose a short wordFor example DOG
- Shift each letter by 3D to G, O to R, G to J
- Write the ciphertextGRJ
- Hand over only the keyYour friend shifts back by 3
- Try breaking itWith only 25 possible keys you can try them all in minutes
That last step is the lesson: a method with few possible keys fails at once to trial and error, and clever tricks can break far bigger designs. This is why experts design, review and publish the real methods.
Everything can be public except the key, so protect the key, and never invent the lock.
Frequently asked questions
Is a signature the same as encryption?
No. A signature proves who made a message and that it is unchanged; it does not hide the message.
Is hashing a way to hide passwords?
Hashing is used to store passwords without keeping them, but it must be a slow, salted method from a trusted library, not a quick hash.
Can I write my own encryption if I keep it secret?
No. Secret designs are the weakest. Use vetted libraries and standard protocols.
Does the padlock in the browser mean a site is safe?
It means the connection is encrypted and the site proved its name. It does not mean the site's owner is trustworthy.