Base64 Encode & Decode
Free online Base64 Encode & Decode tool. 100% local processing — your data never leaves your device.
Result will be displayed here...
Input → Encode
Usage Guide
About Base64
Base64 is an encoding method that represents binary data using 64 printable characters, widely used in scenarios requiring binary data transmission over text protocols. Base64 uses A-Z, a-z, 0-9, +, / (64 characters total), plus = as a padding character. It encodes every 3 bytes (24 bits) of binary data into 4 Base64 characters, resulting in encoded data approximately 133% the size of the original. Base64 is not an encryption algorithm but an encoding method that anyone can decode.
Usage Steps
Base64 encoding and decoding is very simple:
Encoding Principle
Base64 encoding converts binary data to ASCII characters through the following process:
Base64 Variants
Base64 has several common variants for different scenarios:
Application Scenarios
Base64 is widely used in scenarios requiring binary data transmission over text protocols:
FAQ
Q: Does Base64 encoding increase data size?
A: Yes, Base64 encoding increases data size by approximately 33%. Reason: Base64 encodes every 3 bytes (24 bits) into 4 characters (32 bits), adding 8 bits. For example, a 100KB file becomes approximately 133KB after encoding. Impact: 1) Increases network transmission time. 2) Increases storage space. 3) Increases CPU encoding/decoding overhead. Optimization: 1) For large files, consider compressing (such as gzip) before encoding. 2) Using URL-safe Base64 and omitting padding can slightly reduce size. 3) For web resources, consider using binary transmission directly (such as Blob URL) instead of Data URL.
Q: What's the difference between Base64 and encryption?
A: Base64 is encoding, not encryption; they are fundamentally different. Base64 Encoding: 1) Purpose: Convert binary data to text format for transmission. 2) Reversibility: Anyone can decode without a key. 3) Security: No security, cannot protect data confidentiality. Encryption: 1) Purpose: Protect data confidentiality and prevent unauthorized access. 2) Reversibility: Requires a key to decrypt. 3) Security: Provides data protection. Correct Approach: If data protection is needed, first encrypt with AES or similar algorithms, then encode with Base64 for transmission.
Q: Why do Base64 strings end with = signs?
A: = is Base64's padding character, used to align encoding length. Reason: Base64 encodes every 3 bytes into 4 characters. If the original data length is not a multiple of 3, the last group will have less than 3 bytes and needs = padding. Rules: 1) 1 byte remaining: encodes to 2 characters + 2 =. 2) 2 bytes remaining: encodes to 3 characters + 1 =. 3) Exactly 3 bytes: no padding needed. Examples: “A” → “QQ==”, “AB” → “QUI=”, “ABC” → “QUJD”. URL-safe: In URLs, = may be omitted (URL-safe Base64), and will be automatically added back during decoding.
Q: What's the difference between Base64 and Hex encoding?
A: Base64 and Hex are both text representations of binary data, but have different characteristics. Base64: 1) Uses 64 characters (A-Z, a-z, 0-9, +, /). 2) Encoded size is approximately 133% of original data. 3) More compact, suitable for transmission. Hex (Hexadecimal): 1) Uses 16 characters (0-9, A-F). 2) Encoded size is 200% of original data. 3) More readable, suitable for debugging. Selection Advice: 1) Need compact transmission: use Base64. 2) Need readability and debugging: use Hex. 3) Encryption algorithm output: typically uses Hex (such as SHA-256).
Q: What is a Data URL? How to use it?
A: A Data URL is a way to embed data directly in a URL using Base64 encoding. Format: data:[media type];base64,[Base64 data]. Example: data:image/png;base64,iVBORw0KGgoAAAANS.... Advantages: 1) Reduces HTTP requests, improves page load speed. 2) No additional files needed, easy to share and embed. Disadvantages: 1) Increases HTML/CSS file size. 2) Cannot utilize browser cache. 3) Not suitable for large files. Use Cases: 1) Small icons, logos (< 10KB). 2) Inline SVG images. 3) Font files (small fonts). 4) CSS background images. Best Practice: Only use Data URLs for resources smaller than 10KB; large files should use separate files and utilize CDN and caching.
Q: How to use Base64 in JavaScript?
A: JavaScript provides built-in Base64 encoding and decoding methods. Encoding: btoa(string) - Encodes a string to Base64. Decoding: atob(base64String) - Decodes Base64 to a string. Note: btoa and atob only support ASCII characters. For UTF-8 strings, conversion is needed first:// Encode UTF-8
const base64 = btoa(unescape(encodeURIComponent('中文')));
// Decode UTF-8
const text = decodeURIComponent(escape(atob(base64)));
Modern Method: Use TextEncoder and TextDecoder APIs to handle UTF-8. File Encoding: Use FileReader.readAsDataURL() to encode files as Data URLs.
Use Cases
Recommended: Data URL for Small Images
Using Data URLs to embed small images (such as icons and logos) in HTML and CSS can reduce HTTP requests and improve page load speed. Recommended for images smaller than 10KB; large images should use separate files and utilize CDN and browser caching. Data URLs are particularly suitable for inline SVG images because SVG is a text format that remains readable after Base64 encoding.
- ✅ Small icons, logos (< 10KB)
- ✅ Inline SVG images
- ✅ CSS background images (small)
- ❌ Avoid large images (> 50KB)
- 💡 Use build tools (like Webpack) for automatic conversion
Recommended: JWT (JSON Web Token)
JWT uses Base64 to encode Header and Payload for securely transmitting information between client and server. JWT consists of three parts: Header, Payload, and Signature, separated by dots. Header and Payload use Base64 encoding, while Signature uses HMAC-SHA256 or RSA signing.
- ✅ User authentication and authorization
- ✅ Single Sign-On (SSO)
- ✅ API access tokens
- ❌ Don't store sensitive information in JWT (Base64 is decodable)
- 💡 Use HTTPS to transmit JWT
Recommended: HTTP Basic Authentication
HTTP Basic Authentication uses Base64 to encode username and password, formatted as Authorization: Basic [Base64(username:password)]. While Base64 encoding provides no security, using it with HTTPS can protect credentials during transmission. HTTP Basic Authentication is simple and easy to use, suitable for internal systems or scenarios requiring quick authentication implementation.
- ✅ Internal systems, admin panels
- ✅ API authentication (with HTTPS)
- ✅ Simple user authentication
- ❌ Must use HTTPS to avoid plaintext transmission
- 💡 Consider more secure OAuth 2.0 or JWT
Recommended: Email Attachments (MIME)
The MIME (Multipurpose Internet Mail Extensions) protocol uses Base64 to encode email attachments, converting binary files to text format for transmission in email systems. MIME Base64 inserts line breaks (CRLF) every 76 characters to comply with email protocol line length limits. Modern email clients automatically handle Base64 encoding and decoding without manual user intervention.
- ✅ Email attachment encoding
- ✅ Embedding images in email body
- ✅ Compliant with RFC 2045 standard
- 💡 Use email libraries (like Nodemailer) for automatic handling
Recommended: API Binary Data Transmission
When transmitting binary data (such as images and files) in text-format APIs like JSON and XML, Base64 encoding can be used. This avoids handling complex binary transmission formats like multipart/form-data. However, for large files, dedicated file upload APIs (such as multipart/form-data or direct upload to object storage) are recommended for better performance.
- ✅ Small file transmission (< 1MB)
- ✅ Embedding binary data in JSON APIs
- ✅ Storing certificates and keys in configuration files
- ❌ Avoid transmitting large files (> 10MB)
- 💡 Use multipart/form-data or object storage for large files
Recommended: Storing Binary Data in Configuration Files
When storing binary data (such as SSL certificates, keys, small images) in configuration files (like JSON, YAML, XML), Base64 encoding can be used. This avoids handling external file paths and file reading issues, making configuration files self-contained. Particularly suitable for scenarios requiring packaged configurations like Docker images and Kubernetes ConfigMaps.
- ✅ SSL certificates, private keys
- ✅ API keys, tokens
- ✅ Small images, icons
- ✅ Docker, Kubernetes configurations
- 💡 Protect configuration files containing sensitive information
Best Practice Recommendations
- Base64 is encoding, not encryption, and cannot protect data confidentiality. When data protection is needed, encrypt first then encode.
- Base64 encoding increases data size by approximately 33%; for large files, consider compressing before encoding.
- Data URLs are suitable for resources smaller than 10KB; large files should use separate files and utilize CDN and caching.
- HTTP Basic Authentication must be used with HTTPS to prevent credential theft during transmission.
- JWT Payload uses Base64 encoding, which anyone can decode; don't store sensitive information.
- For UTF-8 strings, use TextEncoder/TextDecoder or encodeURIComponent/decodeURIComponent for handling.