Definition
- Base64 converts binary data into ASCII text using 64 printable characters, making binary content safe for text-based transport like JSON and HTTP headers.
- Basic encoder uses the standard alphabet (A-Z, a-z, 0-9, +, /) and is suitable for general encoding.
- URL-safe encoder replaces
+and/with-and_so the string survives URL parsing without percent-encoding. - MIME encoder inserts line breaks every 76 characters per the MIME standard, suitable for email attachments.
- Encoding is NOT encryption: anyone can decode Base64 without any key, so it provides zero confidentiality.
- Size overhead: Base64 increases data size by approximately 33 percent because every 3 input bytes produce 4 output characters.
Base64 is like writing a cheque amount in words (Five Thousand Rupees Only) instead of numbers. Anyone who reads the cheque can understand the amount: it is NOT a secret code. It is just a different representation of the same data that is safe for text-based systems. Encryption would be putting the cheque in a locked vault (AES): only the key holder can read it.
Understanding the Concepts
What is the Java 8 Base64 API?
Java 8 introduced java.util.Base64 as a standard API for encoding and decoding binary data to and from ASCII text. Before Java 8, developers used the unofficial sun.misc.BASE64Encoder which was not part of the public API and could break between JDK versions.
How do you use Base64 encoding and decoding?
package com.bank.service;
import java.util.Base64;
import java.nio.charset.StandardCharsets;
public class Base64Demo {
public static void main(String[] args) {
String accountData = "SAV2001:Rajesh:125000.00";
byte[] originalBytes = accountData.getBytes(StandardCharsets.UTF_8);
// Encode
String encoded = Base64.getEncoder().encodeToString(originalBytes);
System.out.println("Encoded: " + encoded);
// Decode
byte[] decodedBytes = Base64.getDecoder().decode(encoded);
String decoded = new String(decodedBytes, StandardCharsets.UTF_8);
System.out.println("Decoded: " + decoded);
}
}
Encoded: U0FWMjAwMTpSYWplc2g6MTI1MDAwLjAw
Decoded: SAV2001:Rajesh:125000.00
How is URL-safe encoding different from Basic?
| Aspect | Basic | URL-safe |
|--------|-------|----------|
| Characters | A-Z, a-z, 0-9, +, / | A-Z, a-z, 0-9, -, _ |
| Padding | = padding by default | Optional (withoutPadding) |
| Safe in URLs | No (+ and / need escaping) | Yes (- and _ are URL-safe) |
| Use case | General encoding | JWT tokens, URL parameters |
package com.bank.service;
import java.util.Base64;
import java.nio.charset.StandardCharsets;
public class UrlSafeBase64Demo {
public static void main(String[] args) {
String token = "txn-id:TXN20240115:amount:50000";
byte[] bytes = token.getBytes(StandardCharsets.UTF_8);
String basic = Base64.getEncoder().encodeToString(bytes);
String urlSafe = Base64.getUrlEncoder().withoutPadding().encodeToString(bytes);
System.out.println("Basic: " + basic);
System.out.println("URL-safe: " + urlSafe);
}
}
Why is Base64 encoding NOT encryption?
package com.bank.core;
import java.util.Base64;
import java.nio.charset.StandardCharsets;
public class EncodingVsEncryption {
public static void main(String[] args) {
String secret = "AccountNumber:1234567890";
// Base64 encoding: ANYONE can decode this!
String encoded = Base64.getEncoder().encodeToString(
secret.getBytes(StandardCharsets.UTF_8));
System.out.println("Encoded: " + encoded);
// Anyone can decode without any key:
String decoded = new String(
Base64.getDecoder().decode(encoded), StandardCharsets.UTF_8);
System.out.println("Decoded: " + decoded);
// Zero security! Never Base64-encode sensitive data and call it protected.
}
}
Encoded: QWNjb3VudE51bWJlcjoxMjM0NTY3ODkw
Decoded: AccountNumber:1234567890
What are the common banking use cases for Base64?
Base64 is used in banking systems for embedding HMAC signatures in JSON API payloads, encoding KYC documents (images, PDFs) for transmission in REST API responses, encoding JWT header and payload sections, and encoding binary content in XML SOAP messages. In each case, Base64 makes binary data text-safe for transport but provides no security.
What are the limitations?
Base64 inflates data size by approximately 33 percent. Three bytes of binary data become four Base64 characters. For large files like KYC document scans, this overhead matters. Avoid Base64-encoding large blobs in performance-critical paths. Use multipart form data or presigned URLs instead.
What should a senior engineer know?
JWT tokens use Base64URL encoding for the header and payload, which means anyone can decode and read them. Sensitive data like account numbers should never be stored in JWT payloads. Only the JWT signature provides integrity through HMAC with a secret key. For confidentiality, use JWE (JSON Web Encryption) rather than plain JWT.
What about MIME encoding?
MIME encoding inserts line breaks every 76 characters per the RFC 2045 standard. Use it when embedding encoded content in email bodies or multipart MIME messages. The bank's statement delivery service uses MIME encoding when attaching PDF statements to email notifications.
When should you NOT use Base64?
Do not Base64-encode data and claim it is protected. Do not use Base64 for large binary payloads in hot API paths because the 33 percent overhead adds latency and memory pressure. Do not use the unofficial sun.misc.BASE64Encoder: use java.util.Base64 from Java 8 onward.
Visual Overview
Binary Data (bytes) ASCII Text (string)
─────────────────── ────────────────────
[0x53, 0x41, 0x56] ──encode──→ "U0FW"
3 bytes input 4 characters output
(33% larger)
Three Base64 variants:
┌──────────────────────────────────────────────────────┐
│ Basic : A-Z a-z 0-9 + / (general use) │
│ URL-safe : A-Z a-z 0-9 - _ (URLs, JWT) │
│ MIME : Basic + line breaks every 76 chars │
└──────────────────────────────────────────────────────┘
Encoding vs Encryption:
┌───────────┐ ┌───────────┐
│ Base64 │ No key needed │ AES │ Key required
│ (encoding)│ Anyone decodes │(encryption)│ Only key holder decodes
│ 0 safety │ │ Real │
└───────────┘ │ security │
└───────────┘
Key Takeaway
The Java 8 Base64 API provides three variants for converting binary data to text-safe ASCII. The Basic encoder handles general encoding. The URL-safe encoder replaces plus and slash with hyphen and underscore, making the output safe for URLs, query parameters, and JWT tokens. The MIME encoder wraps lines at 76 characters for email attachments.
The critical distinction is that Base64 is encoding, NOT encryption. Anyone can decode a Base64 string without any key. It provides zero security. It merely converts binary data into a text-safe format for transport. In National Bank of India's document service, KYC documents are Base64-encoded for transmission in JSON API responses, but they are not secured by this encoding. For sensitive data like account numbers, the correct approach is AES encryption first, then Base64 encoding of the ciphertext for transport.
Base64 increases data size by approximately 33 percent because every 3 bytes of binary data become 4 ASCII characters. Avoid encoding large blobs in performance-critical paths. Use multipart form data or presigned URLs for large file transfers.
Remember one sentence: Base64 is encoding for transport, not encryption for security: anyone can decode it without a key.
---