Base64 does not encrypt or compress anything. It takes the original bits and writes
them down using a smaller alphabet, made only of characters that are valid to paste
into an email, a web address or a JSON file. Here an example, using 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. The set of ones and zeros that maps to each letter is described by a standard
table called ASCII.
a01100001
b01100010
c01100011
2Cut the original bits into sixes
Base64 has 64 characters, and with six bits, we have exactly 64 different combinations of ones and zeros.
So the same 24 bits (remember, we had three letters, each corresponding to 8 bits) are kept in the same
order and cut into groups of six instead of eight. The colors 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, so we write down the character at that position.
01100024Y
01011022W
0010019J
10001135j
abc becomes YWJj. The three original bytes became four characters.
Here's 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 six-bit groups, and four bits left over.
We fill out the last group with zeros, which finishes the third character. The fourth
character has nothing left to describe, so it is written as =. The equals
sign does not correspond to any data; it's just a placeholder to indicate that we couldn't
make a nice 24-bit group.
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 does not hide anything. Anyone can decode it, so it is not a way to keep a secret.