Base64 does not encrypt or compress anything. It takes the original bits and writes them
down again using a smaller alphabet, made only of characters that are safe to paste into
an email, a web address or a JSON file. Here is how that works for the word
abc.
1 Every letter is eight bits
A computer stores each of these letters as one byte, which is a row of eight ones and
zeros. Which pattern of ones and zeros goes with which letter is set by a standard table
called ASCII.
a 01100001
b 01100010
c 01100011
2 Cut the original bits into sixes
Base64 has 64 characters, and six bits can be arranged in exactly 64 different
combinations of ones and zeros. So the same 24 bits (remember, we had three letters of
eight bits each) 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.
3 Look up each group in the alphabet
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.
011000 24 Y
01 0110 22 W
0010 01 9 J
100011 35 j
So abc becomes YWJj, and the three bytes we started with have
become four characters.
Here's the whole base64 alphabet, with each character's position. The four used for
abc are highlighted.
A 0
B 1
C 2
D 3
E 4
F 5
G 6
H 7
I 8
J 9 (used for abc)
K 10
L 11
M 12
N 13
O 14
P 15
Q 16
R 17
S 18
T 19
U 20
V 21
W 22 (used for abc)
X 23
Y 24 (used for abc)
Z 25
a 26
b 27
c 28
d 29
e 30
f 31
g 32
h 33
i 34
j 35 (used for abc)
k 36
l 37
m 38
n 39
o 40
p 41
q 42
r 43
s 44
t 45
u 46
v 47
w 48
x 49
y 50
z 51
0 52
1 53
2 54
3 55
4 56
5 57
6 58
7 59
8 60
9 61
+ 62
/ 63
4 When 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, which is enough for two full groups of six with 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.
011000 24 Y
01 0110 22 W
0010 00 8 I
no bits left padding =
The two shaded zeros were added to finish the third group. They are not part of
ab.
Text
Bytes
Base64
Padding
abc3 YWJjNone needed
ab2 YWI=One =
a1 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. When it is missing, this tool adds it back before
decoding, and the status line under the boxes mentions that it did.
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, and those bytes 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
something secret.
Try abc in the tool