Module 1 · Lesson 1.1

Secrets and keys

What a key is, and why the secret is the key, not the method.

  • 4-minute video
  • 8-minute read
  • No background needed

The lock on your front door isn't a secret. Thousands of identical locks are sold in hardware stores, and anyone can take one apart to see how it works. So why can't a stranger open your door?

The lock and the key

A common door lock contains a row of small pins of different lengths. The lock only turns when every pin is pushed to exactly the right height, and the only thing that does that is the pattern cut into your key.

The design of the lock is public. Millions of people own the same model. What makes your lock yours is the cut of your key: a small piece of information that only you hold. If you lose the key, you don't redesign the lock. You can rekey it, so the old key no longer opens it.

Cryptography works the same way. It has two parts:

  • The method, called the algorithm. This is the recipe for scrambling, unscrambling or signing information. It plays the role of the lock's design.
  • The key. This is a value that controls how the algorithm works on your data. It plays the role of the cut of your key.

Keys come in more than one kind. This lesson starts with a symmetric key: a secret shared by the people or devices that use it, like copies of the same door key. Later you'll meet key pairs, where one key is private and the other can be public. And some cryptographic tools, such as the hash functions in the next lesson, use no key at all.

The method is public, the symmetric key is secret On the left, a lock labeled "The method: public, anyone can study it". On the right, a glowing key labeled "The symmetric key: secret, shared only by those who use it". The method (algorithm) Public: anyone can study it The symmetric key Secret: shared only by those who use it
Anyone can study the design. Knowing the design should still leave an attacker unable to decrypt without the secret key.

What a cryptographic key looks like

A physical key is a piece of cut metal. A cryptographic key is just a number: a long string of bits, the ones and zeros a computer works with. People usually write it in hexadecimal, a compact notation that uses the digits 0–9 and the letters a–f. Here's a 128-bit key:

3f9a 61c0 d42e 7b18 95a3 0ce7 f16b 28d4

128 bits32 hexadecimal characters

An example key. Real keys are generated at random and never shown to anyone.

There's nothing special about this number. It isn't a code word and it doesn't mean anything. Its only job is to be unknown to everyone who shouldn't have it.

Why the method can be public

It can seem safer to keep the method secret too. In practice, the opposite is true. A well-designed system should stay secure even when an attacker knows exactly how it works; only the secret key needs to stay hidden. This idea is so central that it has a name: Kerckhoffs's principle, after the 19th-century cryptographer who first wrote it down. It doesn't make the design unimportant. The algorithm, the way it's used and the way it's built all still have to be sound, as the example below shows.

There are three good reasons for it:

  • Methods don't stay secret. Devices get taken apart, software gets analyzed and documents leak. A system that depends on a hidden design is only safe until someone looks closely.
  • Public methods get tested. When an algorithm is published, experts around the world try to break it. The ones that survive years of attack are the ones we can trust.
  • Keys are easier to change than methods. If a key is exposed, you switch to a new one, and the system stops accepting the old one, just as rekeying a lock makes the old key useless. If a secret method is exposed, every device that uses it needs a software or firmware update, or replacing if it can't be updated.

Access cards offer a well-known example. One widely used family of contactless cards, MIFARE Classic, relied on a secret method called Crypto-1. In 2007 and 2008, researchers studied the chip closely, reconstructed the hidden algorithm and found serious weaknesses. Because the method itself was flawed, choosing a different key couldn't fix it. The weaknesses made cloning practical in systems that relied on Crypto-1 for security.

What makes a good key

Once the design is sound, the protection depends on the secret key, so the key has to be good. A good key has three qualities:

  • Long. There must be so many possible keys that nobody could ever try them all.
  • Random. Every possible key must be equally likely. A key built from a birthday, a name or a pattern is far easier to guess than its length suggests.
  • Secret. The key must be stored where it can't be read or copied. A strong key written on a sticky note is a weak key.

Secure smart cards take the last point seriously. A smart card can generate a signing key pair on its own chip. The private key stays on the card, and the public key can be shared so others can verify the card's signatures. You'll see why that matters later in the course.

How long is long enough?

The simplest attack on any key is to try every possibility until one works. This is called a brute-force attack. Each extra bit in a key doubles the number of possibilities, so the numbers grow astonishingly fast.

The table below assumes an attacker who can check guesses offline, on their own equipment, with nothing to slow them down or count their attempts, at a hypothetical rate of one trillion guesses every second. That's far faster than any ordinary computer.

SecretPossible valuesTime to try them all offline
4-digit PIN10,000A tiny fraction of a second
8 lowercase lettersAbout 209 billionAbout a fifth of a second
64-bit keyAbout 18 quintillionAbout 7 months
128-bit keyAbout 3.4 × 1038About 1019 years, hundreds of millions of times the age of the universe

If a four-digit PIN can be guessed so quickly, why do cards use them? What matters is where the guesses are checked. When the card checks the PIN, it can enforce a retry limit: after a few wrong attempts it blocks that PIN, so an attacker gets only a handful of tries. Blocking the PIN doesn't necessarily disable the whole card; other functions may still work. The table's numbers apply when guesses can be checked offline instead, where no card is counting. A cryptographic key can face exactly that kind of attack, so it has to be long enough to survive any number of tries.

Try it: make a random key

Your browser uses its cryptographic random-number generator to produce the selected number of bits. Nothing is sent or stored.

Key length

3f9a 61c0 d42e 7b18 95a3 0ce7 f16b 28d4

Possible keys
about 3.4 × 1038
Time to try them all offline, at one trillion guesses a second
About 1.1 × 1019 years
Each click generates a fresh sample. Repeats are possible, especially at 32 bits; at 128 bits they’re vanishingly unlikely.
Go deeperOptional technical detail for readers who want more

Kerckhoffs and Shannon

Auguste Kerckhoffs set out his principles for military ciphers in 1883, in La cryptographie militaire. The second of them says a system must not depend on secrecy, and that it should be no problem if it falls into enemy hands. Claude Shannon, the founder of information theory, later put it more bluntly: assume "the enemy knows the system". Modern standards bodies such as NIST publish their algorithms in full for exactly this reason.

Counting keys

A key of n bits has 2n possible values. A 128-bit key has 2128, about 3.4 × 1038. A 256-bit key has 2256, about 1.2 × 1077: a 78-digit number, not far off the estimated 1080 atoms in the observable universe. On average, a brute-force attacker finds the key after trying half of the possibilities, which only saves one bit's worth of effort.

Bits of security aren't the same as key length

For symmetric algorithms such as AES, a 128-bit key gives roughly 128 bits of security, because the best known attack is close to brute force. Public-key algorithms are different: their keys have mathematical structure that attackers can exploit, so they need much longer keys for the same strength. NIST rates an RSA-2048 key at about 112 bits of security and an elliptic-curve P-256 key at about 128 bits. You'll meet RSA and elliptic curves in lesson 1.7.

Where random keys come from

Computers are designed to be predictable, so generating true randomness takes care. Secure devices use hardware random number generators that measure tiny physical effects, such as electrical noise, and then process the result into uniform random bits. NIST's SP 800-90 series describes how this should be done and tested. The demo on this page uses your browser's crypto.getRandomValues(), which draws on the operating system's cryptographic random-number generator.

Try it yourself

On a computer with OpenSSL installed, this command prints a random 128-bit key in hexadecimal:

$ openssl rand -hex 16

The 16 means 16 bytes, and each byte is 8 bits.

Sources

Draft lesson, awaiting technical review · Last updated 18 November 2026

Search

Press / to search from any page