Base64 does not encrypt or compress anything. It takes exactly the same bits and writes
them down using a smaller alphabet, made only of characters that survive being pasted
into an email, a web address or a JSON file. Here is the whole idea, worked through with
the word abc.
1Every letter is eight bits
A computer stores each of these letters as one byte, which is a row of eight ones and
zeros. Which row belongs to which letter is fixed by a standard table called ASCII.
a01100001
b01100010
c01100011
2Cut the same bits into sixes
Base64 has 64 characters, and 64 is exactly how many different rows six bits can make.
So the same 24 bits are kept in the same order and cut into groups of six instead of
eight. Nothing is added and nothing is thrown away. The colours and underlines show how
each letter's bits end up spread across the new groups.
3Look each group up
Each group of six bits is a number between 0 and 63. That number is a position in the
base64 alphabet, and the character at that position is what gets written down.
01100024Y
01011022W
0010019J
10001135j
abc becomes YWJj. Three bytes became four characters.
The whole base64 alphabet, with each character's position. The four used for
abc are highlighted.
- A0
- B1
- C2
- D3
- E4
- F5
- G6
- H7
- I8
- J9 (used for abc)
- K10
- L11
- M12
- N13
- O14
- P15
- Q16
- R17
- S18
- T19
- U20
- V21
- W22 (used for abc)
- X23
- Y24 (used for abc)
- Z25
- a26
- b27
- c28
- d29
- e30
- f31
- g32
- h33
- i34
- j35 (used for abc)
- k36
- l37
- m38
- n39
- o40
- p41
- q42
- r43
- s44
- t45
- u46
- v47
- w48
- x49
- y50
- z51
- 052
- 153
- 254
- 355
- 456
- 557
- 658
- 759
- 860
- 961
- +62
- /63
4When the bytes run out
Base64 works three bytes at a time, because three bytes are 24 bits and 24 bits cut into
exactly four groups of six. Text does not always come in threes, though. The word
ab is only 16 bits: two full groups, and four bits left over.
Zeros are added to finish the last group, which gives a third character. The fourth
character has nothing left to describe, so it is written as =. The equals
sign carries no data. It only says that the last group came up short.
01100024Y
01011022W
0010008I
no bits leftpadding=
The two shaded zeros were added to finish the third group. They are not part of
ab.
| Text |
Bytes |
Base64 |
Padding |
abc | 3 | YWJj | None needed |
ab | 2 | YWI= | One = |
a | 1 | YQ== | Two = |
One = means the last group held two bytes, and two mean it held only one.
Because padding carries no data, plenty of formats leave it off, including the tokens
many websites use to keep you signed in. This tool notices when it is missing and puts it
back.
A few more things worth knowing
-
Base64 is about a third larger than what it encodes, because every three bytes become
four characters.
-
Letters beyond plain English, such as
é or an emoji, take between two and
four bytes each. They are cut into sixes in exactly the same way.
-
The URL-safe alphabet swaps
+ and / for - and
_, because those two characters mean something in a web address.
Everything else is identical.
-
Base64 hides nothing. Anyone can decode it, so it is not a way to keep a secret.