Symmetric encryption: the maths
XOR and its properties, the size of the key space, brute force and the problem of the number of keys among many users.
XOR
The most used operation in symmetric encryption is XOR (exclusive or): it compares two bits and returns 1 if they are different, 0 if they are equal.
| A | B | A XOR B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
It has a valuable property: applying the same XOR twice returns the original value. If C = P XOR K, then C XOR K = P. Encrypting and decrypting are the same operation.
How many keys are needed
- 01
Key space
An n-bit key has 2^n possible values: 2^56 for DES, 2^128 for AES-128.
- 02
Brute force
Trying a trillion keys per second, 2^56 keys take about 20 hours; 2^128 take about 10^19 years, almost a billion times the age of the universe.
- 03
Keys between users
For n people to communicate in pairs with different keys, n(n − 1)/2 keys are needed: 45 for 10 people, 4,950 for 100, almost 500,000 for 1,000.
Symmetric encryption: worked example
An example of XOR encryption on a single character and on a word, to see how the same key encrypts and decrypts.
The pages of this topic
Back to the overviewSymmetric encryption: how it works
How block and stream ciphers work, modes of operation and authenticated encryption.
Symmetric encryption: history
From the scytale and the Caesar cipher to polyalphabetic ciphers, from rotor machines to DES and AES.
Symmetric encryption: weaknesses and attacks
Brute force, reuse of keys and nonces, wrong modes, side channels and poor key management: how symmetric encryption is attacked.
Symmetric encryption today
Where symmetric encryption is used every day and which algorithms are recommended: AES-GCM, ChaCha20-Poly1305, disk encryption.
Symmetric encryption: try it yourself
Exercises on XOR, counting keys and brute force, with solutions.
Symmetric encryption: frequently asked questions
Short answers to the most common doubts about symmetric encryption.