Why use Base64? Use cases, benefits & trade-offs
Base64 turns arbitrary bytes into portable text. Use it when binary data needs to pass through a text field, then be restored to its original bytes.
An image, PDF, audio file or encrypted message can contain bytes that are not valid text. A JSON string, text editor or older messaging system cannot always carry those bytes directly. Base64 gives them a predictable text representation. It helps with format compatibility; it does not provide confidentiality, authentication or protection against tampering.
File or arbitrary bytes
↓ Base64 encode
Text that fits a string field
↓ Base64 decode
The original bytes
For example, the UTF-8 bytes of Hello encode as SGVsbG8=. Decoding restores the same five bytes. The same process works for binary files, without needing to interpret their contents. See the Base64 standard, RFC 4648.
Working with media? Use the image to Base64 converter for local image previews or the audio to Base64 converter for voice recordings and local playback. Both support reverse decoding and Data URLs without uploads.
Where Base64 helps
| Use case | Benefit | Trade-off |
|---|---|---|
| JSON APIs | Send file bytes alongside metadata in one JSON object. | Larger requests and encoding work; direct uploads suit large files better. |
| Email attachments | Carry arbitrary files through MIME-compatible mail systems. | Additional size and protocol-specific line wrapping. |
| Inline web resources | Embed a small resource in a self-contained document. | Larger documents; no separate resource cache entry. |
| JWTs and signatures | Represent binary signatures in a compact text format. | Decoding alone does not validate authenticity or claims. |
| PEM certificates and keys | Exchange binary structures through text files and deployment tools. | The text encoding does not itself encrypt a private key. |
| Kubernetes Secrets | Represent arbitrary bytes in JSON or YAML data fields. | Access controls and encryption are separate requirements. |
| HTTP Basic authentication | Follow the protocol's format for credentials in a header. | Credentials are reversible; use HTTPS. |
| Shell scripts | Pass bytes through text-only or single-line interfaces. | Watch wrapping, shell history and platform-specific flags. |
1. Put file bytes into a JSON API
JSON has strings, numbers, objects and arrays, but no native binary byte type. An API can define a Base64 string field so callers can send a small file with its business metadata:
{
"filename": "invoice.pdf",
"contentType": "application/pdf",
"contentBase64": "JVBERi0xLjcK..."
}
The encoded value above is abbreviated, not a complete PDF. The receiving application decodes the string to obtain the file bytes. This is convenient when an existing API accepts only JSON. HTTP itself supports binary bodies: for large uploads, consider a raw request body, multipart upload or an object-storage upload followed by a file reference.
2. Send email attachments
MIME Base64 allows images, PDFs and archives to travel through mail infrastructure designed around text. Encoding provides interoperability with those systems. MIME has its own wrapping rules, including lines of no more than 76 characters. Use our MIME / Email preset when that is what the receiving system requires, rather than assuming every Base64 string should wrap. See MIME's Base64 content-transfer encoding.
3. Embed a small resource in a web page
A data: URL can carry a resource inline. A Base64 image URL has the form data:image/png;base64,…. This can make a small document self-contained and avoid a separate request for that resource.
The trade-off is a larger HTML or CSS file, and the embedded image cannot be cached independently of its container. Large images usually belong in separate files. Not every data URL uses Base64; text can also use percent encoding. Read MDN's data URL documentation.
4. Represent JWT segments and signatures
A common signed JWT uses three dot-separated segments:
Base64URL(header).Base64URL(payload).Base64URL(signature)
Base64URL represents signature bytes with an alphabet suited to URLs and other text contexts. JWS compact serialization omits padding. Use the Base64URL converter with its JWT segment preset to inspect one segment.
Readable does not mean verified. A signed token's payload is not confidential simply because it is encoded. Trust requires signature verification and checks appropriate to the application, such as expiration, issuer and audience. Our converter does not perform those checks. See the JWS specification.
5. Exchange PEM certificates and keys
PEM-style files wrap Base64 data in labels such as -----BEGIN CERTIFICATE----- and -----END CERTIFICATE-----. The labels identify the type of structure, while the text representation makes it convenient to exchange through files, editors and configuration tools.
Whether a private key is encrypted depends on its actual format and encryption mechanism. Adding a Base64 wrapper does not protect it or turn arbitrary bytes into a valid certificate. Our PEM preset handles the envelope, not certificate validation. See RFC 7468: textual encodings of certificates and keys.
6. Represent Kubernetes Secret values
Kubernetes Secret data values use Base64 to represent bytes in JSON or YAML. The stringData field is an alternative for supplying plain strings, which Kubernetes converts. This supports a consistent representation of text and binary values.
For example, password becomes cGFzc3dvcmQ= and can immediately be decoded again. The protection comes from access control, encryption configuration and secret-handling practices. Base64 does not make a Secret safe to commit to a public repository. See the Kubernetes Secret documentation.
7. Follow the HTTP Basic authentication format
Basic authentication encodes the combined username:password value for an HTTP header. For the dummy credentials user:pass, the result is:
Authorization: Basic dXNlcjpwYXNz
This meets a protocol requirement; it does not hide the credentials from anyone who can read the header. Use HTTPS and avoid recording authentication headers in logs. See RFC 7617: HTTP Basic authentication.
8. Work with Linux commands and text pipelines
When a script step accepts text, Base64 can provide a reversible representation of file bytes. If the whole pipeline accepts binary input, direct byte transfer may be simpler. GNU/Linux can encode a file without line wrapping like this:
base64 --wrap=0 sample.bin > sample.b64
The output redirection replaces an existing output file. Keep input and output paths different. For copyable text, file, UTF-8 and Base64URL examples, see our Linux Base64 command guide, including newline and invalid-input troubleshooting.
What does Base64 cost?
Every three input bytes become four encoded characters. With standard padding and no line wrapping:
Encoded length = 4 × ceil(input bytes / 3)
A 3 MiB file becomes 4 MiB of Base64 text before any transport compression. For larger inputs, the overhead approaches 33%; tiny inputs can have a higher percentage because of padding. Encoding and decoding also require processing, and implementations that keep both representations in memory use additional space.
Base64 is not compression. It also does not increase the randomness or security of a token. Choose it for a representation requirement, not as a performance or security feature.
Should I use it?
- The protocol requires Base64: follow its alphabet, padding and wrapping rules.
- Binary data must fit into a text field: Base64 is a practical option.
- The content is ordinary text: UTF-8 usually suffices; JSON and Markdown do not need another encoding layer by default.
- The channel accepts binary, especially large files: consider sending bytes directly.
- You need confidentiality, compression or authenticity: use the corresponding encryption, compression or authentication mechanism.