Module 3 · Cryptography and PKI · Lesson 21 of 42
Cryptography basics: ciphers, keys and Kerckhoffs's principle
What cryptography does, confusion and diffusion, substitution with Caesar and Vigenère ciphers, XOR on binary data, and why the key, not the algorithm, must stay secret.
In this lesson · about 25 minutes
- Define cryptography, plaintext, ciphertext, encryption and decryption
- Explain confusion and diffusion
- Encrypt and decrypt with Caesar (ROT-n) and Vigenère ciphers
- Explain why classical ciphers fall to cryptanalysis
- Use XOR to encrypt binary data with a key
- State Kerckhoffs's principle and why open algorithms are trusted
Cryptography is the practice of transforming information so that only the right people can read it, and then turning it back. Its main job is confidentiality, though later lessons show it also delivers integrity, authentication and non-repudiation.
The vocabulary
- Plaintext (or cleartext): the readable original.
- Ciphertext: the scrambled result.
- Encryption: plaintext → ciphertext. Decryption: ciphertext → plaintext.
- Cipher (algorithm): the method used to scramble.
- Key: the secret value that controls the cipher. Same algorithm + different key = different ciphertext.
- Cryptanalysis: the study of breaking ciphers without being given the key.
Confusion and diffusion
Picture a photo. Blur it a little and you can still tell who it is; cut it into pieces and shuffle them and you can't. Good ciphers aim for two properties (named by Claude Shannon):
- Confusion: the relationship between the key and the ciphertext is as complex as possible, so studying the ciphertext reveals nothing about the key.
- Diffusion: each bit of the plaintext affects many bits of the ciphertext, so changing one letter of the message changes about half the output. Patterns in the plaintext get spread out and disappear.
Modern ciphers like AES repeat rounds of substitution (confusion) and permutation (diffusion) many times.
Substitution: the Caesar cipher
The oldest cipher you'll meet is the Caesar cipher, a substitution cipher: every letter is replaced with the letter a fixed number of places further along the alphabet. Think of a cereal-box decoder ring: an outer ring of letters A–Z and an inner ring you can turn. Turn it 2 places and A becomes C, C becomes E, E becomes G, so ACE becomes CEG. A shift of n is called ROT n (ROT13 is a famous one).
To encrypt "We attack at dawn" with ROT 5, drop the spaces and shift each letter 5 places: WEATTACKATDAWN → BJFYYFHPFYIFBS.
The weakness: there are only 25 useful keys, and letter patterns survive (a double letter stays double, E is still the most common letter). Puzzle fans crack these for fun.
Try it: interactive cipher
Caesar cipher (ROT n)
Move the slider to change the shift, then press "Crack it" to see how fast a brute-force attack finds the message.
Ciphertext
BJFYYFHPFYIFBS
Adding a key: the Vigenère cipher
The Vigenère cipher is a Caesar cipher with a different shift for every letter, set by a keyword. Write the keyword repeatedly above the message; each key letter gives the shift for the letter under it (A = ROT 0, B = ROT 1 … Z = ROT 25).
With the key FACE: W + F (ROT 5) = B, E + A (ROT 0) = E, A + C (ROT 2) = C, T + E (ROT 4) = X… so "We attack at dawn" becomes BECXYAEOFTFEBN.
Vigenère shows the two parts every modern cipher has: an algorithm (shift letters by a table) and a key (which shift to use where). It held out for centuries but is breakable today, because the key repeats and leaves patterns. It also only works on letters, and computers need to encrypt any data: images, databases, voice calls.
Try it: interactive cipher
Vigenère cipher
Change the key and watch each letter's shift in the table. Try a longer key: the longer and more random it is, the harder the cipher is to crack.
| Plain | W | E | A | T | T | A | C | K | A | T | D | A | W | N |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Key | F | A | C | E | F | A | C | E | F | A | C | E | F | A |
| ROT | 5 | 0 | 2 | 4 | 5 | 0 | 2 | 4 | 5 | 0 | 2 | 4 | 5 | 0 |
| Cipher | B | E | C | X | Y | A | E | O | F | T | F | E | B | N |
Ciphertext
BECXYAEOFTFEBN
Each key letter sets the shift for one letter: A = ROT 0, B = ROT 1 … Z = ROT 25. The same plaintext letter can come out differently depending on where it sits, which hides the letter patterns a Caesar cipher leaves.
Encrypting binary: XOR
To a computer, everything is ones and zeros. Each character of text becomes 8 bits (so "Mike" is 32 bits), and modern ciphers work on bits, not letters. A building block in almost all of them is exclusive OR (XOR):
- If the plaintext bit and the key bit are the same, the result is 0.
- If they're different, the result is 1.
Line a short key up under the plaintext bits, repeat it as needed, and XOR each pair to get the ciphertext. The neat property: XOR the ciphertext with the same key again and you get the plaintext back. Real ciphers use far longer keys (128 or 256 bits) and many rounds, but the idea is the same.
Try it: interactive cipher
XOR encryption on binary
Type a short word and a key made of bits. The tool converts the text to binary, repeats the key under it, XORs each pair, then XORs again to decrypt.
XOR truth table
| A | B | A XOR B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
Same bits give 0; different bits give 1.
Decrypt: XOR the result with the same key
01001101 01101001 01101011 01100101
Back to text: Mike
Sorting drill
Name the term
Match each description to the right term.
- WEATTACKATDAWN
- BJFYYFHPFYIFBS
- The keyword FACE
- "Shift each letter by the key letter's value"
- AES, published for anyone to study
- The bits 11010 used with XOR
0 of 6 sorted
Check your understanding0 of 8 correct
Quiz score: 0 of 8
Noviq Academy is free and for learning. This course is independent and is not affiliated with or endorsed by CompTIA. CompTIA and Security+ are trademarks of CompTIA, Inc. Exam details such as price and policies change; confirm them on CompTIA's website before you book.