* { margin: 0; padding: 0; box-sizing: border-box; } body { background: #0f1117; color: #e5e7eb; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; line-height: 1.8; } .container { max-width: 800px; margin: 0 auto; padding: 40px 20px; } h1 { font-size: 2.2rem; color: #fff; margin-bottom: 12px; line-height: 1.3; } h2 { font-size: 1.6rem; color: #8b5cf6; margin: 48px 0 16px; padding-bottom: 8px; border-bottom: 2px solid #8b5cf633; } h3 { font-size: 1.25rem; color: #c4b5fd; margin: 32px 0 12px; } p { margin-bottom: 16px; color: #d1d5db; } .meta { color: #9ca3af; font-size: 0.9rem; margin-bottom: 32px; } a { color: #8b5cf6; text-decoration: none; } a:hover { text-decoration: underline; } code { background: #1f2937; padding: 2px 8px; border-radius: 4px; font-size: 0.9rem; color: #c4b5fd; } pre { background: #1f2937; padding: 20px; border-radius: 8px; overflow-x: auto; margin: 16px 0; border-left: 4px solid #8b5cf6; } pre code { background: none; padding: 0; color: #e5e7eb; } table { width: 100%; border-collapse: collapse; margin: 20px 0; } th, td { padding: 12px 16px; text-align: left; border-bottom: 1px solid #374151; } th { background: #1f2937; color: #8b5cf6; font-weight: 600; } td { color: #d1d5db; } .tip { background: #8b5cf615; border-left: 4px solid #8b5cf6; padding: 16px 20px; margin: 20px 0; border-radius: 0 8px 8px 0; } .tip strong { color: #8b5cf6; } .warning { background: #f59e0b15; border-left: 4px solid #f59e0b; padding: 16px 20px; margin: 20px 0; border-radius: 0 8px 8px 0; } .warning strong { color: #f59e0b; } .formula-box { background: #1f2937; border: 2px solid #8b5cf644; padding: 24px; border-radius: 12px; margin: 20px 0; text-align: center; } .formula-box .formula { font-size: 1.3rem; color: #c4b5fd; font-family: 'Courier New', monospace; } .formula-box .desc { color: #9ca3af; font-size: 0.85rem; margin-top: 8px; } .cta { background: linear-gradient(135deg, #8b5cf622, #8b5cf611); border: 1px solid #8b5cf644; padding: 32px; border-radius: 12px; text-align: center; margin: 40px 0; } .cta h3 { color: #fff; margin-bottom: 12px; } .cta-btn { display: inline-block; background: #8b5cf6; color: #fff; padding: 12px 32px; border-radius: 8px; font-size: 1.1rem; font-weight: 600; margin-top: 12px; } .cta-btn:hover { background: #7c3aed; text-decoration: none; } .faq-item { margin: 24px 0; } .faq-q { font-weight: 600; color: #c4b5fd; margin-bottom: 8px; } .toc { background: #1f2937; padding: 20px 24px; border-radius: 8px; margin: 24px 0; } .toc h3 { color: #8b5cf6; margin: 0 0 12px; } .toc ol { padding-left: 20px; } .toc li { margin-bottom: 6px; } .toc a { color: #d1d5db; } window.dataLayer=window.dataLayer||[]; function gtag(){dataLayer.push(arguments);} gtag("js",new Date()); gtag("config","G-9J54FYQT11");

Complete Guide to Base64 Encoding and Decoding

April 15, 2026 · About 8 minutes of reading · By Moran @ Dumb Tech

Whether you're a front-end developer working with API data, a back-end engineer transferring binaries, or an average user needing to convert a piece of text format, Base64 encoding and decoding is a fundamental skill to master. This guide takes a holistic look at Base64 from principle to practice.

Content

  1. What is Base64 encoding
  2. Detailed explanation of the coding principle
  3. Common variants of Base64
  4. Practical scenarios:
  5. Code examples by language
  6. Performance and safety considerations
  7. FAQFAQ

What is Base64 encoding

Base64 is an encoding method for representing binary data based on 64 printable characters. It converts arbitrary binary data into ASCII strings consisting of letters (A-Z, a-z), numbers (0-9), and "+" and "/". The core purpose of this encoding is to ensure that data is not garbled or lost when passing through text-only transport protocols.

Base64 was originally defined in the MIME (Multipurpose Internet Mail Extensions) protocol (RFC 2045) for secure transmission of binary attachments in email. It is now widely used in embedded data in HTML/CSS, JWT tokens, API authentication, database storage, and many other fields.

Why Base64

Data in computers is essentially binary (0 and 1), but many communication protocols and storage systems only support text characters. If binary data is transmitted directly, some special bytes, such as control characters, may be interpreted as commands by the protocol, resulting in data corruption. Base64 solves this problem once and for all by mapping binary data to a plain text character set.

Tip:Base64 is not an encryption algorithm! It's just an encoding that anyone can easily decode. Do not use Base64 to protect sensitive data.

Detailed explanation of the coding principle

Base64's encoding process follows a clear three-step conversion rule:

3 bytes (24bit) → 4 Base64 characters (6bit x 4)
Data volume swells by about 33%

Encoding Steps

Step 1: Group Read— Break the raw data into groups every 3 bytes (24 bits).

Step 2: Split into 6-bit units— Split the 24 bits of each group into 4 6-bit units.

Step 3: Lookup Table Mapping— Each 6-bit unit has a value range of 0-63, corresponding to one character in the Base64 character table.

Let's look at a concrete example of encoding the string "Man" as Base64:

characters. ASCII valuebinary
M7701001101
a9701100001
n11001101110

Combined 24 bits: 01001101 01100001 01101110

Split into 4 groups of 6: 010011 | 01 0110 | 0001 01 | 101110

Check the table: 19→ T, 22→ W, 5→ F, 46→ u, the result isTWFu

Filling mechanism

Base64 populates with the "=" character when the number of bytes in the original data is not a multiple of 3:

Remaining Numeric Sectionsprocessing modeEXAMPLE
0No need to fill"Man" → TWFu
1Make up 2 "=""M" → TQ==
2Make up 1 "=""Ma" → TWE=

Common variants of Base64

Standard Base64 needs to be adjusted in specific scenarios, so a variety of variants are derived:

variantsubstitution character:Padding Application Scenarios
Standard Base64+ /YesMIME, email, generic scenarios
URL Security Base64- _OptionalURL parameters, JWT token
No Fill Base64+ /NoPartial API, data URL
Base64url (RFC 4648)- _NoModern Web Standards
⚠️ Note:When using standard Base64 in a URL, "+" and "/" are encoded by the URL as "%2B" and "% 2F", resulting in data bloat. URL scenarios must use URL-safe variants.

Practical scenarios:

1. API Authentication (Basic Auth)

HTTP Basic Authentication Base64 encodes the username and password in the request header after connecting with a colon:

Authorization: Basic dXNlcjpwYXNzd29yZA==

"user: password" after decoding.

2. HTML/CSS embedded resources

Small images and icons can be directly embedded in HTML or CSS after Base64 encoding, reducing the number of HTTP requests:

<img src="data:image/png;base64,iVBORw0KGgo..." />

3. JWT Token

The Header and Payload sections of the JSON Web Token use URL-safe Base64 encoding. The JWT is made up of three Base64 strings connected by dots.

4. Database storage

When some databases or systems do not support storing binary data directly, you can use Base64 encoding to store it as text, such as storing encryption keys, certificates, etc.

Code examples by language

JavaScript


const encoded = btoa('Hello, World!');
console.log(encoded); // SGVsbG8sIFdvcmxkIQ==


const decoded = atob('SGVsbG8sIFdvcmxkIQ==');
console.log(decoded); // Hello, World!

Python

import base64 # encoded = base64.b64encode (b 'Hello, World! ') print (encoded) # b 'SGVsbG8sIFdvcmxkIQ = =' # decoded = base64.b64decode (b 'SGVsbG8sIFdvcmxkIQ = = ') print (decoded) # b 'Hello, World!'

Java

import java.util.Base64;


String encoded = Base64.getEncoder().encodeToString("Hello".getBytes());

byte[] decoded = Base64.getDecoder().decode(encoded);
Tip:JavaScript's btoa () can only handle Latin1 characters. When processing Chinese, you need to first go to UTF-8: `btoa (new TextEncoder () .encode (' Chinese '))` or use a third-party library.

Performance and safety considerations

Volume expansion

The Base64 encoded data volume is about 133% of the original data (an increase of about 33%). For large file transfers, this is an overhead to consider.

Original sizeAfter Base64 encodingincreased by
1 KB1.33 KB+333 B
100 KB133 KB+33 KB
1 MB1.33 MB+333 KB
10 MB13.3 MB+3.3 MB

SAFETY INSTRUCTIONS.

Base64 encoding does not provide any security. The encoded data can be restored instantaneously. Base64 does not apply in the following scenarios:

FAQFAQ

Q1: What is the difference between Base64 and hex encoding?

Base64 uses 64 characters, 4 characters per 3 bytes, 33% more volume; hexadecimal uses 16 characters, 2 characters per 1 byte, 100% more volume. Base64 is more efficient, but hexadecimal is more intuitive and easy to read, and is often used to display hashes and debug.

Q2: Can Base64 encode Chinese?

Yes, but you need to ensure that Chinese is correctly converted to UTF-8 byte sequences before Base64 encoding. In JavaScript, btoa () does not support direct encoding of Chinese, and you need to convert the string to a UTF-8 byte array first with the help of TextEncoder.

Q3: Why does JWT use Base64url instead of standard Base64?

JWTs are often transmitted in the URL (such as query parameters), and the "+" and "/" in standard Base64 have a special meaning in the URL, and "=" is encoded in the URL. These problems are avoided with Base64url (substitute "-" for "+", "_" for "/", remove padding "=").

Q4: Can Base64 encoded data be compressed?

Base64 encoding itself has distributed the data evenly over 64 characters, which actually reduces the entropy of the data, making the compression effect of traditional compression algorithms (such as gzip) on Base64 data poor. If both encoding and compression are required, the original data should be compressed before Base64 encoding.

Q5: Will Base64 encoding be slow for large files (e.g. 100MB)?

For the built-in Base64 implementation of modern programming languages, the encoding of 100MB files is usually completed in 1-3 seconds, and performance is not an issue. However, the encoded 133MB volume may take up more memory. It is recommended to use streaming for large files to avoid one-time loading into memory.

🚀 Online Base64 encoding and decoding tool

You don't need to install any software to use our Base64 codec directly in your browser. Supports text, URL Safe Mode, Live Preview.

Home

© 2026 Risetop Risetop.top — Making Tools Simpler