Base64
An encoding scheme that converts binary data to ASCII strings using 64 characters: A-Z, a-z, 0-9, +, and /.
Base64 is an encoding scheme that converts binary data into ASCII strings. It uses 64 characters: A-Z (26), a-z (26), 0-9 (10), +, and /, representing 3 bytes of data as 4 characters. When the input data length is not a multiple of 3, padding characters (=) are appended.
Base64 is needed because text-based protocols like email and HTTP cannot transmit raw binary data. It is widely used in email attachments (MIME), data URI schemes (data:image/png;base64,...), JWT (JSON Web Tokens), and API request/response bodies. For example, embedding small icon images directly in HTML as data URIs using Base64 encoding reduces the number of HTTP requests. Web security fundamentals books cover Base64 use cases.
In JavaScript, btoa() encodes and atob() decodes Base64. However, these functions only handle Latin-1 characters, so multibyte characters like Japanese require combining with TextEncoder. In Node.js, use Buffer.from(data).toString('base64'). Python provides the base64 module, and Java offers the java.util.Base64 class as standard.
A common misconception is confusing Base64 with encryption. Base64 is purely encoding (a reversible transformation) and provides no security protection whatsoever. Anyone can easily decode it, so it cannot be used to protect passwords or sensitive information. Also note that data size increases by approximately 33%, since 3 bytes become 4 characters, resulting in 4/3 times the original size. Network protocol introduction books explain the technical background of Base64.
When including Base64 strings in URLs or filenames, the standard + and / characters cause issues as they have special meanings. Base64url (RFC 4648) addresses this by replacing + with - and / with _. From a character counting perspective, the length of a Base64-encoded string can be precisely calculated from the original byte count: ceil(n / 3) * 4 characters (including padding).