What Is Base64? How to Encode and Decode It (and Why It Is Not Encryption)
Quick answer: Base64 writes any data using only letters, numbers, + and /. To encode or decode text, choose Encode or Decode, paste your text and copy the result. It is not encryption: anyone can decode it. Why
Quick answer: Base64 writes any data using only letters, numbers, + and /. To encode or decode text, choose Encode or Decode, paste your text and copy the result. It is not encryption: anyone can decode it.
Why does Base64 exist?
Some systems (email, JSON, URLs) only handle plain text safely. Base64 turns bytes, such as an image, into plain text so they can travel through those systems. The cost is size: Base64 is about one third bigger than the original.
Example: hello becomes aGVsbG8=.
Encoding and decoding in JavaScript
btoa() and atob() only work on Latin-1 text, so Urdu, Hindi, Arabic and emoji break. Convert to UTF-8 bytes first:
function toBase64(text) {
const bytes = new TextEncoder().encode(text);
let bin = "";
for (const b of bytes) bin += String.fromCharCode(b);
return btoa(bin);
}
function fromBase64(b64) {
const bin = atob(b64);
const bytes = Uint8Array.from(bin, (c) => c.charCodeAt(0));
return new TextDecoder().decode(bytes);
}
fromBase64(toBase64("Salam دنیا 😀")); // "Salam دنیا 😀"
What is URL-safe Base64?
Normal Base64 uses +, / and =, which have special meanings in URLs. The URL-safe variant swaps them for - and _ and drops the = padding, so the value can sit inside a link (JWTs use this form).
const urlSafe = (b64) => b64.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
Base64 is not security
Never use Base64 to hide a password or key. It only changes how the data is written. For real protection you need encryption.
Try it without writing code
I keep a free browser-based Base64 encoder and decoder (it runs locally, nothing is uploaded) together with a JSON formatter and a URL encoder.
Originally published at saveanyreel.com.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.