Encode Base64 Data in a QR Code
Generate a QR code containing Base64-encoded data for binary content transfer. Covers encoding strategies, size overhead, Data URI embedding, and use cases for certificate and key distribution.
Detailed Explanation
QR Codes for Base64 Data
Base64 encoding converts binary data into ASCII text, making it compatible with QR codes. This enables QR codes to carry binary payloads like small files, cryptographic keys, or compressed data.
Why Base64 in QR Codes?
QR codes store text data. To embed binary content, you need a text-safe encoding:
- Base64 — Standard encoding, 33% size overhead
- Base64url — URL-safe variant, replaces
+/with-_, no padding
Encoding a Small File
For a small configuration file or certificate:
data:application/json;base64,eyJrZXkiOiJ2YWx1ZSIsIm51bSI6NDJ9
The receiving application decodes the Base64 string back to binary:
const decoded = atob("eyJrZXkiOiJ2YWx1ZSIsIm51bSI6NDJ9");
// Result: {"key":"value","num":42}
Size Constraints
Base64 has a 33% overhead — 3 bytes of binary become 4 characters of Base64 text. Combined with QR code capacity limits:
| QR Version | Error Correction M | Binary Capacity | Base64 Capacity |
|---|---|---|---|
| 10 (57x57) | ~652 chars | ~489 bytes | ~652 chars |
| 20 (97x97) | ~1,460 chars | ~1,095 bytes | ~1,460 chars |
| 40 (177x177) | ~3,391 chars | ~2,543 bytes | ~3,391 chars |
Practical Limit
For reliable scanning with a phone camera, stay within QR Version 15-20 (about 1,000-1,500 Base64 characters). Larger codes require high-resolution printing and close scanning distance.
Data URI Format
Combine Base64 with Data URIs for self-describing payloads:
data:<mediatype>;base64,<data>
Common media types:
application/json— Configuration dataapplication/x-pem-file— Certificatesapplication/octet-stream— Generic binary
Use Cases for Base64 QR Codes
- Certificate distribution — Share small X.509 certificates or public keys
- Compressed data — Gzip-compress JSON, then Base64-encode the result
- Small images — Encode tiny icons or avatars (under 1KB)
- Encrypted payloads — AES-encrypted data encoded as Base64
- Configuration blobs — Device settings in a compact binary format
Use Case
Base64 QR codes are used for distributing small cryptographic certificates, sharing SSH public keys, transferring compressed configuration files between air-gapped systems, and encoding small binary payloads for IoT device provisioning where network connectivity is unavailable.