Base64 Encode & Decode

Standard Base64 (RFC 4648, +/ alphabet with = padding): encode any UTF-8 text to ASCII, or decode a payload back to text—useful for Basic auth strings, JWT-shaped blobs, PEM fragments, and JSON fields. Strip data:image/…;base64, style prefixes before decode; for whole image files or Data URLs use Image to Base64 / Base64 to image in the Encode section.

What Base64 represents in bytes, text, and transport

Base64 is a reversible byte-to-text encoding: every three raw bytes become four printable ASCII characters from a 64-symbol alphabet. It is specified in RFC 4648 and shows up in HTTP Basic Authorization, PEM certificates, many JSON fields, and data: URLs—not because it hides data, but because it moves binary or non-ASCII bytes through channels that only tolerate ASCII text safely.

This page implements the standard alphabet A–Z, a–z, 0–9, +, / with = padding for the common “encode UTF-8 text” and “decode Base64 back to UTF-8 string” path your browser and Node use for string payloads. It is not a substitute for encrypting secrets; pair TLS in transit with real cryptography when confidentiality matters. For turning image or file bytes into Base64 or Data URLs, use image to Base64 and to reverse a visual payload use Base64 to image.

Confusion often comes from mixing roles: hex shows each byte as two nibbles; Base64 packs bits more densely; URL percent-encoding escapes reserved characters in URLs and is unrelated to the Base64 alphabet. JWT header and payload segments are Base64url (- / _, often no padding), which differs from the +/ form you paste here unless you normalize.

How Base64 relates to bits, padding, and UTF-8

Why three bytes become four characters and how the 6-bit groups line up

  1. Each input byte is eight bits; four Base64 output characters carry six bits each, so 4×6 = 24 bits holds exactly three bytes.
  2. The encoder concatenates those bits, reads successive 6-bit indexes, and picks the symbol from the 64-character table.
  3. Decoding inverts the process: four symbols yield 24 bits, split back into three bytes in order.

What the trailing equals signs do when the byte count is not a multiple of three

  1. If one byte remains after full triplets, the second quartet is padded with zeros and two = characters finish the block.
  2. If two bytes remain, one = is emitted so the total output length stays a multiple of four characters.
  3. Strict decoders reject wrong padding length; some runtimes tolerate missing padding—this page expects valid RFC-style padding for decode.

Why text encoding must be UTF-8 before Base64 and what happens on decode

  1. JavaScript string Base64 uses UTF-8 bytes as the input octet stream, the same as btoa with binary strings built from those bytes.
  2. If upstream produced Base64 from UTF-16 or Latin-1 without agreement, decoding here can show mojibake even when the Base64 string is valid.
  3. Emoji and scripts outside the BMP still encode as multi-byte UTF-8 sequences before Base64; the character count of the string is not the byte count.

How Data URLs differ from raw Base64 payloads in HTML and APIs

  1. A Data URL has the shape data:[mediatype][;base64], followed by the payload—browsers need the prefix to interpret inline images or downloads.
  2. REST or GraphQL fields often store only the payload and send the MIME type in another property; do not paste the entire Data URL into this decoder unless you strip the prefix first.
  3. The ;base64 marker distinguishes the segment from quoted-printable or percent-encoded forms in the same family of “ASCII armor”.

Where URL-safe Base64 and the standard +/ alphabet disagree

  1. URL-safe variants swap +→- and /→_ and may omit = so query strings stay unescaped.
  2. JWT uses Base64url for header and payload pieces; transform characters before comparing with tools that only accept +/.
  3. This codec follows standard Base64; translate symbols manually when an API contract specifies URL-safe form.

Where teams bump into Base64 beside “encode this string”

Authorization headers and opaque tokens in logs

Basic Auth concatenates user:password, Base64-encodes the UTF-8 bytes, and prefixes Basic —the string is still reversible by anyone who can read the header. Opaque bearer tokens are often random bytes Base64-encoded for display; treat them like secrets in logs regardless of encoding.

JWT segments versus the HMAC or RSA signature

The header and payload are JSON objects Base64url-encoded for transport; they are not encrypted. Integrity comes from the third segment and the shared or public key—verify signatures in your stack, not by eyeballing Base64 text.

Embedding small assets in HTML or CSS

Data URLs increase markup size roughly four-thirds over raw bytes but avoid an extra HTTP request for tiny icons. Large media still belongs in binary files or CDNs; compare byte cost with image to Base64 when sizing bundles.

Frequently asked questions

What is Base64 encoding used for?
It represents arbitrary bytes as ASCII text so you can put binary data in JSON, XML, email bodies, certificates, or data: URLs without corrupting line-oriented transports. It does not add secrecy—anyone can decode the same string.
How do I decode Base64 to plain text?
Paste the payload only—strip a data:…;base64, prefix if present—then decode to UTF-8. If the result is garbled, the upstream may have used another charset or the blob may not be text (for example a gzip or image); confirm the format first.
Base64 vs hex — what is the difference?
Hex prints each byte as two characters (0–9A–F); Base64 uses four characters per three bytes, so it is more compact on the wire. Neither is encryption; choose hex when humans read individual bytes, Base64 when you need compact ASCII armor.
Why does valid Base64 still decode to garbage characters?
The bytes may be compressed, encrypted, UTF-16 text, or a binary format mistaken for Base64 text. Validate the intended layer—maybe it is a gzip blob—or open images with Base64 to image instead of UTF-8 decode.
Is Base64 the same as encryption?
No—it is a reversible encoding with a published alphabet. Use AES, TLS, or your platform’s crypto when you need confidentiality or integrity, not Base64 alone.
What is URL-safe Base64 and do I need it?
Some specs replace + / / with - / _ and drop padding so values sit cleanly in query strings. JWT and many OAuth parameters use this variant. Convert characters or use a Base64url-aware library when the spec demands it.
How does Base64 relate to JWT?
The first two JWT segments are Base64url-encoded JSON; decoding them explains claims and algorithms but does not verify trust—that requires signature validation with the issuer’s key.
Why do some Base64 strings end with one or two equals signs?
Padding aligns the character stream to a multiple of four; it encodes how many bytes were implied in the last incomplete block. Wrong padding often makes decoders throw before producing output.