Cipher Utility

Base64 Encoding: Not Encryption, but Still Useful

How 3 bytes become 4 characters, and why anyone can reverse it.

What Base64 is

Base64 turns any data, including binary data such as images, into text made of 64 safe characters. It exists so that binary data can travel through systems built for text, such as email, JSON, HTML and HTTP headers. It is an encoding, which means anyone can reverse it, and it gives no security at all.

The standard alphabet is defined in RFC 4648:

ValuesCharacters
0 to 25A to Z
26 to 51a to z
52 to 610 to 9
62, 63+ and /
Padding= (not one of the 64)

Why 64

64 is 2 to the power of 6, so each Base64 character carries exactly 6 bits. Three bytes (24 bits) fit exactly into four characters, which keeps the encoding simple. The characters are all printable ASCII, and they avoid control characters and null bytes that older text systems could drop or change.

How Base64 encoding works

Take the input three bytes at a time, write the 24 bits out, cut them into four groups of 6 bits, and look each group up in the alphabet. Here is "Hi!":

1. Bytes:        H = 72       i = 105      ! = 33
2. Bits:         01001000     01101001     00100001
3. Joined:       010010000110100100100001
4. Groups of 6:  010010  000110  100100  100001
5. Values:       18      6       36      33
6. Characters:   S       G       k       h

"Hi!"  →  SGkh

Decoding runs the steps backwards: look up each character's value, join the 6-bit groups and cut them into bytes again. You can watch both directions with the Base64 encoder and decoder.

Padding

When the input length is not a multiple of 3, the last group is short. The encoder fills the missing bits with zeros and adds = signs so the output length is always a multiple of 4:

Bytes left overPaddingExample
NoneNone123 → MTIz
2=12 → MTI=
1==1 → MQ==

Size

Every 3 bytes become 4 characters, so Base64 output is about 33% larger than the input. A 1 MB photo becomes roughly 1.33 MB of text. Email adds a line break every 76 characters, which costs a few percent more.

Base64 is not encryption

People often mistake Base64 for encryption, because the output looks scrambled. It isn't encrypted. There is no key, the method is public, and any programming language can decode it in one line:

password123  →  cGFzc3dvcmQxMjM=  →  password123

Base64 is also not hashing, because it is fully reversible, and not compression, because it makes data bigger. It is easy to recognize, too: a string of letters, digits, + and / that often ends in = or == is very likely Base64.

Two mistakes come up again and again. Storing API keys or passwords "encoded in Base64" protects nothing; keep secrets in environment variables or a secrets manager, and hash passwords with bcrypt or Argon2. Base64 also does nothing against tampering or eavesdropping. For those you need encryption, a MAC or a digital signature.

Where Base64 is used

Email attachments

Email was designed for 7-bit text. MIME encodes attachments as Base64 and marks them with a header:

Content-Transfer-Encoding: base64

iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk...

Data URLs

Small images and fonts can be embedded directly in HTML or CSS. This is a complete 1 × 1 pixel transparent GIF:

<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" alt="">

JSON has no binary type, so APIs often send files as Base64 strings as well.

HTTP Basic authentication

The browser joins the user name and password with a colon and Base64-encodes the result:

username:password  →  Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=

Anyone who sees that header can decode the password, so Basic authentication is only acceptable over HTTPS.

JSON Web Tokens

A JWT has three parts separated by dots: header, payload and signature, each encoded with the URL-safe Base64 variant without padding.

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9
.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIn0
.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c

Decode the first part and you get {"alg":"HS256","typ":"JWT"}. Decode the second and you get {"sub":"1234567890","name":"John Doe"}. The payload of a JWT is readable by anyone; only the signature protects it from being changed.

Keys and certificates

PEM files, the text files between -----BEGIN CERTIFICATE----- lines, hold Base64-encoded binary keys and certificates. Encrypted data is also often Base64-encoded so it can be stored or sent as text, but the encryption is what protects it.

Base64 variants and other encodings

The + and / characters have special meanings in URLs and file paths, so there is a URL-safe variant:

VariantValues 62 and 63PaddingUsed for
Standard+ /=Email, data URLs, most APIs
URL-safe- _Often left outURLs, file names, JWTs

Other text encodings make different trade-offs:

  • Base32 uses only capital letters and the digits 2 to 7, so it survives being read aloud or typed in by hand. The output is 60% larger than the input. Authenticator app secrets (TOTP) are usually Base32.
  • Hexadecimal uses 0 to 9 and A to F, two characters per byte, so it doubles the size. It is easy to read byte by byte, which is why hash outputs and memory dumps are shown in hex.

When to use Base64

Use Base64 when you need to move or store binary data through something that only handles text: email, JSON, XML, HTML attributes, HTTP headers or text-only configuration. For large data, compress it first (for example with gzip) and then encode it, because Base64 itself only adds size.

Don't use it to hide anything. For confidentiality use real encryption such as AES, or TLS for data in transit. To detect changes use a SHA-256 hash, an HMAC or a digital signature. For passwords use bcrypt or Argon2. Choosing the right encryption goes through these options.

Try it on Cipher Utility

Further reading