<aside> 🧭
Module 02 · Keys, Encodings & File Formats
In Module 01 you made a key and noticed it came out as strange Base64 text. This module opens that file up. By the end you will know what is inside a key, why the same key can be saved in five different file formats, and how to convert between them without breaking anything.
🧠 concept → 🧪 exercise → ✅ expected result (hidden) → 🎯 interview questions (answers hidden)
Prerequisite: Module 01. You already generated a keypair (Exercise B2.1), saw that the private file was much bigger than the public one, and used openssl pkey -pubout. This module explains why all of that is true.
</aside>
<aside> 🏠
The picture to hold in your head for this whole module.
Think of a padlock and its key.
Someone can lock a box with your padlock and send it to you. Only you can open it. That is Module 01's Exercise B2.1, in physical form.
This module is about the padlock and the key as files on disk — what they contain, how they are written down, and what happens when two programs disagree about how to write them down.
</aside>
<aside> 📖
Official docs: openssl genpkey manual · RFC 8017 — RSA specification · NIST SP 800-57 Part 1 — key management and key sizes
</aside>
There are two families of key in everyday use. They do the same job. They just use different maths to do it.
<aside> 🔒
The analogy. Both are padlocks. They are simply different brands.
RSA is the big, old, heavy padlock. It has been around since 1977, it works everywhere, and everybody knows how to use it. To be strong it has to be physically large — that is why RSA keys are 2048 or 4096 bits.
Elliptic Curve (EC) is the modern compact padlock. It is a fraction of the size and just as hard to break, because it is built on cleverer engineering. A 256-bit EC key is about as strong as a 3072-bit RSA key.
Smaller padlock, same strength, lighter to carry. That is the whole trade.
</aside>
| Key type | Common size | When you will see it |
|---|---|---|
| RSA | 2048-bit | The safe default. Works with absolutely everything, including very old clients |
| RSA | 4096-bit | Used for root CAs and long-lived keys. Slower, and rarely worth it for a web server |
| EC (P-256) | 256-bit | The modern choice for web servers. Small, fast, supported by every current browser |
| EC (P-384) | 384-bit | Higher-security setups, some government and banking requirements |
| Ed25519 | 256-bit | Excellent, very small, very fast — but not allowed in public web certificates yet. Common for SSH |
<aside> ⚠️
A trap worth knowing before you meet it. "Bigger number means stronger" is true inside one family and completely false between families.
A 256-bit EC key is not weaker than a 2048-bit RSA key. It is roughly stronger. The numbers count different things, so comparing them directly is like comparing a shoe size to a hat size.
If someone says "we require 4096-bit keys" as a blanket rule, they have usually copied it from a checklist without knowing this.
</aside>
🧪 Exercise A1.1 — Make one of each and compare them
mkdir -p ~/tls-lab/m02 && cd ~/tls-lab/m02
umask 077
openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:2048 -out rsa2048.key
openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:4096 -out rsa4096.key
openssl genpkey -algorithm EC -pkeyopt ec_paramgen_curve:P-256 -out ec256.key
openssl genpkey -algorithm ED25519 -out ed25519.key
ls -l *.key
wc -l *.key
🧪 Exercise A1.2 — Feel the cost of a big RSA key
time openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:2048 -out /tmp/t2048.key
time openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:4096 -out /tmp/t4096.key
time openssl genpkey -algorithm EC -pkeyopt ec_paramgen_curve:P-256 -out /tmp/tec.key
rm -f /tmp/t2048.key /tmp/t4096.key /tmp/tec.key