AES cipher (Advanced Encryption Standard)
AES (Advanced Encryption Standard) is the cipher on which almost all encryption today rests: the Wi-Fi at home, the disk in your laptop and your phone, the connection to your bank over HTTPS and the messages in WhatsApp. If you mainly want to know whether it is secure: it is. Nobody has broken AES-128 or AES-256 (the number is the key length in bits), and nothing suggests that this will change any time soon.
AES is a symmetric cipher: the same key encrypts and decrypts the data. The key is a row of 128, 192 or 256 zeros and ones. How two people agree on a key without sending it over the network is dealt with by other methods, such as Diffie–Hellman key exchange or the RSA cipher. AES then uses that key to encrypt the data itself: it chops the data into blocks of 16 bytes and puts each block many times through swapping bytes, rearranging, mixing and adding the key, until no trace of the original data is left.
Why not use RSA directly? It is too slow for large amounts of data. AES, by contrast, is nimble: the processors in computers and phones have dedicated circuits for it, and an ordinary laptop encrypts a gigabyte of data in about a second.
Bit, byte and hex
A bit is the smallest piece of information: either 0 or 1. Eight bits make a byte, for example 01011111. A byte has 28 = 256 possible values. A letter of text usually takes up a single byte, a photo a million bytes or much more.
Eight zeros and ones in a row are hard to read, so we split the byte into two groups of four bits. A group of four has 16 possible values, exactly one digit of the hexadecimal system, hex: 0 to 9 and the letters a to f for 10 to 15. The bits have weights 1, 2, 4 and 8 from the right, so 0101 is 4 + 1 = 5 and 1111 is 8 + 4 + 2 + 1 = 15, that is, f. The byte 01011111 is written 5f: two hex digits are one byte. A more detailed explanation can be found in the article on hash functions.
AES is a block cipher: it encrypts data in blocks of 16 bytes, that is, 128 bits. The sixteen bytes are arranged into a 4 × 4 square, like the letters in a Polybius square or in the Playfair cipher. It is filled column by column, so in the picture above the block 00, 11, 22, … ff runs from top to bottom and from left to right.
XOR: addition without carrying
The main operation of the whole cipher is called XOR and is written ⊕. It is used to “mix” the key into the data and to unmix it again. Without the key, nobody can read anything out of the result. XOR itself compares two bits: when they are the same, the result is 0, when they differ, it is 1. There are no other possibilities:
| 0 | 1 | |
|---|---|---|
| 0 | 0 | 1 |
| 1 | 1 | 0 |
It is like addition in which 1 + 1 gives zero and nothing is carried. Two bytes are XORed bit by bit. Let’s try the bytes 5f and d6:
The result is 10001001, hex 89. Now comes the nice part: if we XOR the result 89 with d6 once more, we get 5f back. A bit added twice is the same as none. That is why a key can use XOR to encrypt the data, and the same key can use XOR to decrypt it again. The Vernam cipher stands on this too, except that it needs a key as long as the message. AES manages with a short key, and the rounds do the rest of the work.
How AES encryption works: one round
Encryption begins by XORing the block with the key. Then come ten rounds, and each round has four steps. We will go through them on a real example from the standard FIPS 197, where the key is 00 01 02 … 0f (16 bytes, that is, 128 bits) and the data is 00 11 22 … ff. After the first XOR with the key, we get the square 00, 10, 20, … f0, and the first round starts with it.
1. Swapping bytes (SubBytes). Each byte is replaced by another according to a fixed table of 256 rows, called the S-box. The principle is the same as in the substitution cipher, except that bytes are swapped instead of letters. 50 always becomes 53, and 00 always becomes 63.
There is more complicated mathematics behind the table, but you do not need to know it. One property is enough: the table has no regularity, and the neighbouring values 50 and 51 end up completely differently (53 and d1). The other steps, XOR and rearranging, behave predictably, like addition: change the input and you know exactly how the output changes. If the whole cipher were like that, it could be written as a system of equations like the ones you know from school, only bigger, and the key could be calculated from it. The swap is the only step that breaks this predictability, and that is why the whole cipher stands on it.
2. Shifting rows (ShiftRows). The first row stays, the second moves one place to the left, the third two places and the fourth three. What drops off on the left appears on the right. The bytes are merely rearranged, as in the rail fence cipher, and nothing is calculated.
Why this is done is shown by the colours: before the shift each column is a single colour, and after the shift each has one byte from all four original columns.
3. Mixing columns (MixColumns). All four columns are mixed, each separately: from four bytes, four new ones are calculated, and every new byte depends on all four old ones. The first column 63, 53, e0, 8c thus changes into 5f, 72, 64, 15.
Behind the mixing there is again a multiplication of bytes that you do not need to know. The property is enough: if you change a single byte in a column, all four change.
4. Adding the key (AddRoundKey). Finally the square is XORed with the round key of that round. From the original key, a different round key, again of 16 bytes, is derived in advance for each round. The top left byte gives
That completes the first round. The remaining nine run the same way, only with a different round key, and the last one also leaves out the column mixing. After the tenth round, the block 69 c4 e0 d8 … c5 5a from the first picture comes out, exactly as the standard gives it.
Everything can be decrypted, because every step has its counterpart. Whoever has the key goes through the steps backwards: XOR with the same round key unmixes it again, the inverse mixing restores the columns, shifting to the right restores the rows and the inverse table restores the bytes. Whoever lacks the key does not know the round keys either and gets stuck at the very last step.
Avalanche: why ten rounds
None of the steps on its own hides anything; the strength lies in repeating them.
Let’s try encrypting with the same key two blocks that differ in a single byte: the first begins with 00, the second with 01. In the picture, the bytes in which the two blocks differ at that moment are in red.
Before the first round, a single byte differs. The swap does change it, but the change stays confined to that byte. Shifting rows does not move it, because it lies in the first row. Only the column mixing spreads the change over the whole first column, so after the first round the blocks differ in four bytes. In the second round, the row shift splits the changed bytes among all four columns, and the mixing then scrambles each of them completely. After the second round all 16 bytes differ. The finished ciphertexts end up differing in 62 bits out of 128, roughly half, as they should. From one ciphertext you therefore cannot read anything at all about the other, even if the data differ by a single bit.
So why ten rounds, if the avalanche is complete after two? Scattering the bytes is not enough: they must be scattered so thoroughly that no trace of the data or the key remains in them. Cryptographers can attack shortened versions of AES with a few rounds, and ten rounds were chosen so that a large safety margin remains. That is also why AES-256, with its longer key, has 14 rounds and AES-192 has 12: a larger key needs more mixing.
Beware of repeating blocks
If a long file were split into blocks and each encrypted separately with the same key, the same block would always give the same ciphertext. Encrypt a picture with large areas of a single colour this way: each area turns into a different colour, but again a uniform one, and the outline of the picture remains visible. The famous example in cryptography is the penguin, which you can still recognise after encryption.
That is why AES is not used bare like this. Each block is first combined with its sequence number and a random number for the message, or with the previous block, so that two identical blocks give different ciphertexts. Such methods are called modes of operation, and the most widespread today is called GCM. It also detects when someone alters the ciphertext on the way.
Where AES came from
The predecessor of AES was called DES, and it was the American standard from 1977. Its key was only 56 bits, so 256, about 72 quadrillion possibilities. By the end of the 1990s that was too little. For less than 250,000 US dollars, the Electronic Frontier Foundation built a machine called Deep Crack, which in July 1998 found a DES key in 56 hours.
So the US standards body NIST announced a competition for a successor as early as January 1997. Unlike DES, it was held in public: 15 ciphers entered, and cryptographers from all over the world studied them for over two years. On 2 October 2000 the winner was announced: Rijndael, by two Belgian cryptographers, Joan Daemen and Vincent Rijmen. On 26 November 2001 it was published as the standard FIPS 197 under the name AES.
AES-128 and AES-256: how strong is the key
An AES-128 key has 128 bits, so there are 2128 possible keys, a number with 39 digits:
That is 272, about 4.7 sextillion times more than for DES. Imagine a billion computers setting about the search, each of them managing a billion keys per second. Going through all the keys would take them roughly 1013 years, about eight hundred times longer than the universe has existed. The best known attack on full AES-128 shortens the work only about fourfold, so it helps nobody.
And what about quantum computers? A quantum computer would break RSA and Diffie–Hellman completely, which is why post-quantum cryptography is being developed. It would only speed up the search for an AES key (with what is called Grover’s algorithm): for AES-128, from 2128 attempts to
Where you meet AES
- Wi-Fi. A home network with WPA2 encrypts with AES (in CCMP mode), and so does the newer WPA3. The weak point tends to be the password: whoever captures a login to a WPA2 network can try passwords at home as often as they like. WPA3 makes this blind guessing harder.
- Disk. BitLocker in Windows encrypts with AES with a 128-bit or 256-bit key, and FileVault on a Mac uses AES too. The iPhone encrypts files with AES-256 (older models AES-128), and on Android phones launched with version 10 or later, file encryption is mandatory, usually AES-256.
- HTTPS. The TLS 1.3 protocol, on which HTTPS rests, requires every implementation to be able to do AES-128 in GCM mode. The padlock in the browser therefore almost always means that the data is flowing through AES.
- WhatsApp. Messages are protected by the Signal protocol: keys are agreed with public-key methods, and the messages themselves and attachments are encrypted with AES-256.
What should you take home from this? AES-128 and AES-256 are both secure, and you usually choose neither the mode nor the key length yourself, because the device decides for you. For Wi-Fi, WPA2 with a long password is enough, and WPA3 is better. Data leaks because of a weak password, a stolen phone with no lock or a bug in a program, not because of AES. How long a password needs to be is shown in the article on password security.