My Digital Garden

JPNAS.NET / Digital Garden

🌿 GrowingLast updated:

How Encryption Works

From Caesar and Enigma to AES and public keys: how encryption really works, the difference between encrypting and hashing, why passwords are stored with salt, and a few small examples you can try on your own computer.

Open in the interactive Digital Garden

What You Need
Curiosity – no math beyond adding numbers
For the examples: a terminal on Windows or Linux, optionally GnuPG (on Windows as Gpg4win)
Related tools: my Privacy & Security resources

Jump to Examples

Encryption is everywhere in my notes – in HTTPS, in the VPN, in backups, in the phone. But I never really wrote down how it works. People think it's something only mathematicians understand. The details are hard, sure, but the ideas are surprisingly simple, and knowing them makes a lot of security advice suddenly make sense.

From Caesar to Enigma

The oldest well-known cipher is named after Julius Caesar: move every letter three places in the alphabet, so A becomes D and B becomes E. The method (move the letters) and the key (how many places) are already separate – and that's the most important idea in all of cryptography. The method can be public; only the key must be secret.

Caesar's cipher is easy to break: try all 25 shifts. Later ciphers like the Vigenère cipher used a whole word as the key and changed the shift for every letter. It was called "the unbreakable cipher" for about 300 years – until it wasn't.

Then came machines. The German Enigma had rotors that turned after every letter, so the same letter was encrypted differently every time, with an astronomical number of settings. Polish mathematicians led by Marian Rejewski broke it already in 1932, and during the war the team at Bletchley Park, with Alan Turing, built electromechanical machines to find the daily keys. Breaking Enigma is often said to have shortened the war by years – and it pushed the development of the first computers.

Symmetric Encryption: One Key

Modern encryption works on bits instead of letters, but the idea is the same: a public method and a secret key. With symmetric encryption, the same key locks and unlocks.

The first public standard was DES in 1977, with a 56-bit key. By the late 1990s computers were fast enough to try all the keys in a few days. In 2001 it was replaced by AES, chosen in an open competition – the winner was Rijndael, made by two Belgians, Joan Daemen and Vincent Rijmen. AES with a 256-bit key has 2256 possible keys. That's a number with 78 digits; trying them all is not "hard", it's physically impossible.

AES is what encrypts your disk with BitLocker or VeraCrypt, your Wi-Fi with WPA2/WPA3, your restic backups and most of the data in an HTTPS connection. Modern processors even have special instructions for it, so it costs almost nothing.

So encryption itself is rarely the weak point. The weak point is the key – a short password, a key stored next to the data, or a key that somebody can simply ask you for.

Public Keys: Two Keys

Symmetric encryption has one big problem: how do you give the key to somebody you have never met, over a network where anybody can listen? For thousands of years the answer was "you can't" – keys were carried by people.

In 1976 Whitfield Diffie and Martin Hellman published a way for two parties to agree on a secret key over a public line, and in 1977 Rivest, Shamir and Adleman published RSA. (Later it came out that British GCHQ had found the same ideas a few years earlier, but kept them secret.) The trick is a pair of keys:

  • the public key – you can give it to everybody; what is encrypted with it can only be opened with
  • the private key – which never leaves your computer.

It works the other way round too: what you "sign" with your private key, everybody can check with your public key. That's a digital signature, and it's how your computer knows that an update really comes from Microsoft or Debian, and how the browser knows that the server is really jpnas.net.

In practice both kinds are used together: public keys to agree on a fresh symmetric key, and AES (or ChaCha20) for the actual data, because it's much faster. That's what HTTPS, SSH, Signal and WireGuard do.

Hashing: One Way Only

A hash is not encryption, but it lives in the same family. A hash function like SHA-256 makes a fixed-length "fingerprint" from any data. The same data always gives the same hash; change one letter and the hash is completely different; and there is no way back from the hash to the data.

Hashes are used to check downloads (Example 1), in Git, in backups for deduplication – and for passwords. A good website never stores your password, only its hash. When you log in, it hashes what you typed and compares. Two more tricks make this safe:

  • Salt – a random value added to every password before hashing, so two people with the same password get different hashes, and pre-computed tables of common passwords are useless.
  • Slow hashing – special functions like bcrypt or Argon2 are made to be slow on purpose, so an attacker who steals the database can try only a few thousand passwords per second instead of billions.

That's also why a long password matters more than a "complicated" one: every extra character multiplies the work. And why a password manager (KeePassXC, Bitwarden) is the best security tool most people can have.

Examples

Example 1: Check a Download

Many projects publish the SHA-256 hash next to the download. If your file gives the same hash, it's exactly the file they published.

# Windows PowerShell
Get-FileHash .\linuxmint-22-cinnamon-64bit.iso -Algorithm SHA256

# Linux
sha256sum linuxmint-22-cinnamon-64bit.iso

Try changing one character in a small text file and hashing it again – the result has nothing in common with the first one.

Example 2: Encrypt a File with a Password

# encrypts with AES, asks for a password, makes secret.pdf.gpg
gpg --symmetric --cipher-algo AES256 secret.pdf

# and back
gpg --output secret.pdf --decrypt secret.pdf.gpg

Good for sending one file. For a whole folder that should stay encrypted, Cryptomator or VeraCrypt are easier.

Example 3: Your Own Key Pair

Coming soon. Making a GPG key pair, signing a file and checking the signature – the same thing Linux distributions do with every package.

How HTTPS Uses All of This

Coming soon. The TLS handshake step by step: certificate, signature, key agreement, and then AES for the data – with a picture.

A Few More Thoughts

What I find fascinating is that the strongest encryption in the world is free, open and built into every phone. The methods are public and checked by thousands of people – that's exactly why we can trust them. "Secret" encryption that nobody is allowed to look at is the one to worry about. So the real question is almost never "is AES strong enough?" but "where is my key, and who else can get to it?"

← All notes