Data & APIs

Base64 Encode: Convert Text and Binary Data Safely

base64 encode: Understand Base64 encoding, its size overhead, URL-safe variant, and why encoding does not encrypt private data. Read the steps and example.

Photorealistic editorial photo for What Is Base64 Encoding
Data & APIs · Practical ideas for your next build

Base64 encodes binary data as printable ASCII characters so it can pass through text-oriented formats. It is commonly used in email, data URLs, and some API payloads. Encoding is reversible and provides no confidentiality: anyone who has the text can decode it. Base64 also increases data size by roughly one third before other formatting overhead. Choose it only when a protocol or interface requires a text representation, and use encryption separately for sensitive content.

Understand the alphabet and padding

Standard Base64 uses letters, digits, plus, slash, and optional equals padding. URL-safe Base64 replaces plus and slash with hyphen and underscore; some versions omit padding. A decoder must know which variant it receives. Treat whitespace and line breaks according to the format’s rules. A value that looks like Base64 is not necessarily valid text after decoding because the bytes may represent an image, compressed file, or encrypted data.

Plan for size and character encoding

Base64 maps each group of three input bytes to four output characters, producing approximately 4/3 the original size. JSON and URL escaping can add further bytes. For text, first encode characters to bytes using a defined character encoding such as UTF-8, then apply Base64. Do not decode arbitrary bytes as text without checking the expected encoding. For large files, a binary upload or object storage link is usually more efficient.

Keep secrets out of public strings

Base64 is not a security boundary. It can hide a value from casual reading, but it does not prevent anyone from recovering the original bytes. Do not use it to protect passwords, API keys, or personal data. Use authenticated encryption or a secure transport protocol for confidentiality, and manage keys separately. A data URL is also visible to scripts and logs that can access the page, so avoid embedding private files in public content.

Worked example

EXAMPLE
Text: Hello
UTF-8 bytes: 48 65 6c 6c 6f
Base64: SGVsbG8=
Decode: Hello

Is Base64 encryption?

No. Base64 is an encoding that anyone can reverse. Use encryption when confidentiality is required.

Reference and further reading

Read the official documentation ↗