What is a brute force attack?
A brute force attack tries every possible key until one works. It needs no insight into the cipher, only time. Attackers use the same idea to guess passwords, one attempt after another.
The Caesar cipher is an easy target because it has so few keys. The tool above simply runs through all of them and puts the most language-like result first.
Why the Caesar cipher has only 25 keys
The English alphabet has 26 letters, so there are 26 possible shifts. A shift of 0 or 26 changes nothing, which leaves 25 useful keys. That is the whole search, however long the message is.
A longer alphabet adds a few more keys, but the search stays tiny. The tool above works on A to Z. For other alphabets, use the solver on the home page.
How to do it by hand
You only need a pencil. Take the first word, move each letter back by one, and see whether it reads as a word. If not, try two, then three, and carry on. Here is what that looks like for a message encrypted with a shift of 3:
KHOOR ZRUOG | Shift back | Result |
|---|---|
| 1 | JGNNQ YQTNF |
| 2 | IFMMP XPSME |
| 3 | HELLO WORLD |
| 4 | GDKKN VNQKC |
You can stop as soon as you reach real words, and you can usually reject a wrong shift after the first word. At worst you make 25 attempts.
How to spot the right result
Read the list and look for real words. A computer does roughly the same thing by scoring each line on how closely its letters match a language, which is why the top result is usually right on longer text. The score is only a guide, and it can fail on names, slang or a few words.
A very short text can even have two honest answers. CHEER shifted by 7 becomes JOLLY, so a one-word message could decode to either word, and you need context to choose.
A few lines of code
The search is a single loop over the 25 shifts. This Python version handles capital letters and prints every line for you to read:
text = "KHOOR ZRUOG" for shift in range(1, 26): plain = "" for c in text: if c.isalpha(): c = chr((ord(c) - 65 - shift) % 26 + 65) plain += c print(shift, plain) # 3 HELLO WORLDFor the full program, follow the Caesar cipher tutorial in Python.
Why the same attack fails against AES
Brute force depends on the number of keys, and modern ciphers have far too many. Suppose you could test a billion keys per second. This table shows how long a full search would take:
| Cipher | Possible keys | At a billion keys per second |
|---|---|---|
| Caesar | 25 | Instantly |
| DES | 2⁵⁶, about 7.2 × 10¹⁶ | About 2.3 years |
| AES-128 | 2¹²⁸, about 3.4 × 10³⁸ | About 10²² years |
| AES-256 | 2²⁵⁶, about 1.2 × 10⁷⁷ | About 3.7 × 10⁶⁰ years |
DES shows that the speed matters too: in 1998 a machine built by the Electronic Frontier Foundation found a DES key in 56 hours. AES has far longer keys, and the path from the Caesar cipher to AES explains what else changed.
When brute force is the wrong tool
Brute force works here because the Caesar cipher has so few keys. A substitution cipher has about 4 × 10²⁶ keys, so people attack it with frequency analysis and word patterns instead. A Vigenère cipher with a long keyword is out of reach by hand as well.
If no line reads as words, the text probably is not a Caesar cipher at all. In that case, try the methods in breaking a cipher without the key.
Frequently asked questions
How many keys does the Caesar cipher have?
With the 26-letter alphabet it has 25 useful keys, because a shift of 0 or 26 leaves the text unchanged.
Does brute force always break a Caesar cipher?
It always produces the right text somewhere in the list. A very short message may also give another readable result, so you may need context to choose.
Can brute force break AES?
Not in practice. AES-128 has 2¹²⁸ possible keys, so a full search would take far too long even at a billion keys per second.
How long does it take by hand?
It depends on the text, but there are never more than 25 shifts to try, and the first word is usually enough to reject a wrong one.