How Does Base64 Really Work? A Deep Dive into Encoding Basics
This article explains the origin, alphabet, step‑by‑step conversion process, padding rules, and provides a Java example to illustrate how Base64 encodes binary data into printable characters, highlighting why the output is about one‑third larger than the input.
In every moment online you rely on Base64, but how does it actually work?
Origin of Base64
Base64 has become a common way to encode 8‑bit data for transmission. It is used in payment systems, signatures, encryption, and when non‑ASCII characters cause garbled text. It solves the problem of transmitting binary data over channels that only accept printable characters, such as email, URLs, cookies, and small binary files.
Encoding Principle
Base64 uses a 64‑character alphabet (A‑Z, a‑z, 0‑9, +, /). The index table is defined as an array. The padding character “=” is added to complete the output.
Conversion Steps
Split the input bytes into groups of three (24 bits).
Divide each 24‑bit group into four 6‑bit units.
Pad each 6‑bit unit with two leading zeros to form an 8‑bit byte.
Map each resulting value to the Base64 alphabet.
The process expands the data size by roughly one‑third because each 6‑bit unit becomes an 8‑bit character.
Example: Encoding “Man”
ASCII codes: M=77, a=97, n=110 → binary 01001101 01100001 01101110 (24 bits).
Group into 6‑bit chunks: 010011 010110 000101 101110.
Pad each chunk with two leading zeros → 00010011, 00010110, 00000101, 00101110.
Indices: 19, 22, 5, 46 → characters T, W, F, u.
Result: “Man” → “TWFu”.
Handling Incomplete Byte Groups
Two bytes (“BC”): produce three Base64 characters and one “=” padding → “QKM=”.
One byte (“A”): produce two Base64 characters and two “=” paddings → “QQ==”.
Important Notes
Base64 converts binary to printable characters; it is not encryption.
It is used for transmission, storage, and representation of binary data.
Different source encodings (UTF‑8, GB2312, etc.) yield different Base64 results for the same visual text.
Java Verification
The following Java program uses sun.misc.BASE64Encoder to encode sample strings and prints the results, confirming the manual calculations.
package com.secbro2.blog.utils;
import sun.misc.BASE64Encoder;
/**
* @author zzs
*/
public class Base64Utils {
public static void main(String[] args) {
String man = "Man";
String a = "A";
String bc = "BC";
BASE64Encoder encoder = new BASE64Encoder();
System.out.println("Man base64结果为:" + encoder.encode(man.getBytes()));
System.out.println("BC base64结果为:" + encoder.encode(bc.getBytes()));
System.out.println("A base64结果为:" + encoder.encode(a.getBytes()));
}
}Output:
Man base64结果为:TWFu
BC base64结果为:QkM=
A base64结果为:QQ==Summary
The article walks through the history, alphabet, conversion algorithm, padding rules, and provides a concrete Java example, giving readers a clear understanding of how Base64 encoding works and why it expands data size.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Senior Brother's Insights
A public account focused on workplace, career growth, team management, and self-improvement. The author is the writer of books including 'SpringBoot Technology Insider' and 'Drools 8 Rule Engine: Core Technology and Practice'.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
