Base64 explained: what it is, how it works, when to use it

You have seen Base64 even if you never asked for it: the eyJ… at the start of a JWT, the data:image/png;base64,… in a stylesheet, the wall of letters inside a raw email attachment. It is one of the most common encodings in software and one of the most misunderstood. This guide explains what problem it solves, walks through exactly how three bytes become four characters, and covers the variants and traps you will meet in real code.

Why Base64 exists

Computers store everything as bytes, and a byte can have any of 256 values. Many channels, though, were built to carry text: email (originally 7-bit ASCII only), JSON and XML documents, URLs, HTTP headers, HTML attributes. Push raw binary through them — an image, a PDF, a cryptographic signature — and some byte values get altered, stripped or treated as control characters, and the file arrives corrupted.

Base64 solves this by describing any bytes using only 64 characters that survive practically everywhere: A–Z, a–z, 0–9, + and /, plus = for padding. It is a representation, not protection: anyone can decode it, and no key is involved.

How it works: 3 bytes become 4 characters

64 = 2⁶, so each Base64 character carries exactly 6 bits. Three bytes are 24 bits, which divide evenly into four 6-bit groups. The encoder therefore reads the input three bytes at a time and writes four characters for each group. Let’s encode the word Man by hand:

Text:      M          a          n
ASCII:     77         97         110
Bits:      01001101   01100001   01101110

Regroup the 24 bits into 6-bit chunks:
           010011  010110  000101  101110
Values:    19      22      5       46
Alphabet:  T       W       F       u

Result:    TWFu

The alphabet is just an index: 0–25 are A–Z, 26–51 are a–z, 52–61 are 0–9, 62 is + and 63 is /. So 19 is T and 46 is u. Decoding simply runs the steps backwards.

Padding: what the = signs mean

Input rarely comes in perfect multiples of three bytes. When one or two bytes are left over, the encoder fills the missing bits with zeros and adds = so that the output length is still a multiple of four:

InputBytesBase64Why
Man3TWFuFull group, no padding
Ma2TWE=16 bits → 3 characters + one =
M1TQ==8 bits → 2 characters + two ==

Padding carries no data. Because the original length can be worked out from the number of characters, many formats (JWTs among them) omit it, and good decoders accept both forms.

The size cost: about 33% bigger

Every 3 bytes turn into 4 characters, so encoded data is 4/3 the original size — roughly 33% more, plus up to two padding characters. A 1,000-byte file becomes 1,336 characters; a 3 MB photo becomes about 4 MB. Email encoders also insert a line break every 76 characters, adding a little more. That overhead is why Base64 suits small payloads, not bulk file transfer: if you can send binary directly (a normal HTTP upload with multipart/form-data, for example), do that.

URL-safe Base64

Two characters of the standard alphabet cause trouble in URLs and file names: + (often read as a space in query strings) and / (a path separator). The URL-safe variant from RFC 4648 swaps them for - and _, and usually drops the = padding as well, because = is also meaningful in query strings.

bytes FB FF BF   standard: +/+/    URL-safe: -_-_

This is the variant JWTs use for all three parts. If a Base64 string contains - or _, decode it as URL-safe.

Data URIs

A data URI puts a whole file inside a URL, so the browser needs no extra request:

data:text/plain;base64,SGVsbG8sIERoYWthIQ==
<img src="data:image/png;base64,iVBORw0KGgo…" alt="icon">

That first one decodes to Hello, Dhaka!. Data URIs are handy for tiny icons and for single-file HTML reports, but because the data is 33% larger and cannot be cached separately from the page, large images are better served as normal files.

The UTF-8 trap in JavaScript

The browser functions btoa() and atob() date from a time when strings were assumed to be Latin-1. They work on characters with codes 0–255 only, so anything outside that range fails:

btoa('বাংলা');   // InvalidCharacterError: Invalid character

Base64 encodes bytes, so first turn the text into UTF-8 bytes, then encode those bytes — and reverse both steps when decoding:

// Browser (and Node)
const bytes = new TextEncoder().encode('বাংলা');           // 15 UTF-8 bytes
const b64 = btoa(String.fromCharCode(...bytes));           // '4Kas4Ka+4KaC4Kay4Ka+'
const text = new TextDecoder().decode(
  Uint8Array.from(atob(b64), (c) => c.charCodeAt(0))
);                                                         // 'বাংলা'

// Node.js
Buffer.from('বাংলা', 'utf8').toString('base64');           // '4Kas4Ka+4KaC4Kay4Ka+'
Buffer.from('4Kas4Ka+4KaC4Kay4Ka+', 'base64').toString('utf8');
Buffer.from(bytes).toString('base64url');                  // URL-safe, no padding

If you skip TextDecoder and call atob() alone, you get 15 garbled Latin-1 characters instead of five Bangla letters — the classic “mojibake” bug. For very large inputs, encode in chunks, because spreading millions of bytes into String.fromCharCode can exceed the engine’s argument limit.

Python and the shell work on bytes explicitly:

import base64
base64.b64encode('বাংলা'.encode('utf-8'))            # b'4Kas4Ka+4KaC4Kay4Ka+'
base64.urlsafe_b64encode(bytes([0xfb, 0xff, 0xbf]))  # b'-_-_'

$ echo -n 'Hello, Dhaka!' | base64
SGVsbG8sIERoYWthIQ==

Watch the newline: without -n, echo adds a line break and you get SGVsbG8sIERoYWthIQo= — a different string that decodes with an extra newline at the end.

Where you will meet it

Base64 is not encryption. Encoding a password or API key in Base64 hides it from nobody. For the difference between encoding, encryption and hashing, read this guide.

Encode or decode in a second. The Base64 tool handles UTF-8 text, URL-safe output and whole files or images, all inside your browser. Try it now

FAQ

Why does my Base64 end with one or two = signs?

The input length was not a multiple of three bytes. One leftover byte produces ==, two leftover bytes produce =. They are padding only and carry no data.

Can I use Base64 to shrink data?

No — it always makes data about a third larger. If size matters, compress first (for example with gzip) and Base64-encode the compressed bytes only if the channel requires text.

Why do I get “Invalid character” when decoding?

Usually the string is URL-safe (contains - or _) and your decoder expects the standard alphabet, or it contains spaces, quotes or a data:…;base64, prefix that must be removed first.

Base64 সহজ ভাষায়: কী, কীভাবে কাজ করে, কখন ব্যবহার করবেন

না চাইলেও Base64 আপনি দেখেছেন: JWT-এর শুরুর eyJ…, স্টাইলশিটের data:image/png;base64,…, কিংবা কাঁচা ইমেইল অ্যাটাচমেন্টের ভেতরের অক্ষরের দেয়াল। সফটওয়্যারে এটা সবচেয়ে বেশি ব্যবহৃত এনকোডিংগুলোর একটি, আবার সবচেয়ে ভুল বোঝা এনকোডিংগুলোরও একটি। এই গাইডে দেখবেন এটা কোন সমস্যার সমাধান করে, তিন বাইট কীভাবে ঠিক চারটি অক্ষর হয়, আর বাস্তব কোডে কোন কোন রূপ ও ফাঁদের মুখোমুখি হবেন।

Base64 কেন দরকার

কম্পিউটার সবকিছু বাইট হিসেবে রাখে, আর একটা বাইটের মান ২৫৬ রকমের যেকোনো একটা হতে পারে। কিন্তু অনেক মাধ্যম তৈরি হয়েছে শুধু লেখা বহনের জন্য: ইমেইল (শুরুতে শুধু ৭-বিট ASCII), JSON ও XML ডকুমেন্ট, URL, HTTP হেডার, HTML অ্যাট্রিবিউট। এগুলোর ভেতর দিয়ে কাঁচা বাইনারি — ছবি, PDF, ক্রিপ্টোগ্রাফিক সিগনেচার — পাঠালে কিছু বাইট বদলে যায়, বাদ পড়ে বা কন্ট্রোল ক্যারেক্টার হিসেবে ধরা হয়, আর ফাইল নষ্ট হয়ে পৌঁছায়।

Base64 এর সমাধান করে যেকোনো বাইটকে মাত্র ৬৪টি অক্ষর দিয়ে প্রকাশ করে, যেগুলো প্রায় সব জায়গায় অক্ষত থাকে: A–Z, a–z, 0–9, + ও /, সাথে প্যাডিংয়ের জন্য =। এটা একটা প্রকাশের ধরন, সুরক্ষা নয়: যে কেউ ডিকোড করতে পারে, কোনো কী লাগে না।

কীভাবে কাজ করে: ৩ বাইট থেকে ৪ অক্ষর

৬৪ = 2⁶, তাই প্রতিটি Base64 অক্ষর ঠিক ৬ বিট বহন করে। তিন বাইট মানে ২৪ বিট, যা সমানভাবে চারটি ৬-বিটের ভাগে ভাঙে। তাই এনকোডার ইনপুট থেকে একবারে তিন বাইট পড়ে, আর প্রতিটি দলের জন্য চারটি অক্ষর লেখে। চলুন Man শব্দটা হাতে এনকোড করি:

Text:      M          a          n
ASCII:     77         97         110
Bits:      01001101   01100001   01101110

২৪ বিটকে ৬-বিটের টুকরোয় ভাগ করুন:
           010011  010110  000101  101110
Values:    19      22      5       46
Alphabet:  T       W       F       u

Result:    TWFu

বর্ণমালাটা আসলে একটা সূচি: ০–২৫ হলো A–Z, ২৬–৫১ হলো a–z, ৫২–৬১ হলো 0–9, ৬২ হলো + আর ৬৩ হলো /। তাই ১৯ মানে T, ৪৬ মানে u। ডিকোড করা মানে এই ধাপগুলো উল্টো দিকে চালানো।

প্যাডিং: = চিহ্নের মানে

ইনপুট খুব কমই ঠিক তিন বাইটের গুণিতকে আসে। এক বা দুই বাইট বাকি থাকলে এনকোডার ফাঁকা বিটগুলো শূন্য দিয়ে ভরে, আর = যোগ করে যাতে আউটপুটের দৈর্ঘ্য চারের গুণিতক থাকে:

ইনপুটবাইটBase64কেন
Man3TWFuপূর্ণ দল, প্যাডিং নেই
Ma2TWE=১৬ বিট → ৩টি অক্ষর + একটি =
M1TQ==৮ বিট → ২টি অক্ষর + দুটি ==

প্যাডিং কোনো ডেটা বহন করে না। অক্ষরের সংখ্যা থেকেই মূল দৈর্ঘ্য বের করা যায় বলে অনেক ফরম্যাট (JWT-সহ) এটা বাদ দেয়, আর ভালো ডিকোডার দুই রূপই গ্রহণ করে।

আকারের খরচ: প্রায় ৩৩% বড়

প্রতি ৩ বাইট ৪টি অক্ষর হয়, তাই এনকোড করা ডেটা মূলের ৪/৩ গুণ — মোটামুটি ৩৩% বেশি, সাথে সর্বোচ্চ দুটি প্যাডিং অক্ষর। ১,০০০ বাইটের ফাইল হয় ১,৩৩৬ অক্ষর; ৩ MB-এর ছবি হয় প্রায় ৪ MB। ইমেইল এনকোডাররা প্রতি ৭৬ অক্ষর পরপর লাইন ব্রেকও দেয়, তাতে আরেকটু বাড়ে। এই বাড়তি খরচের কারণেই Base64 ছোট ডেটার জন্য মানানসই, বড় ফাইল পাঠানোর জন্য নয়: সরাসরি বাইনারি পাঠানো গেলে (যেমন multipart/form-data দিয়ে সাধারণ HTTP আপলোড), সেটাই করুন।

URL-safe Base64

স্ট্যান্ডার্ড বর্ণমালার দুটি অক্ষর URL আর ফাইলের নামে ঝামেলা করে: + (query string-এ প্রায়ই স্পেস হিসেবে পড়া হয়) আর / (পাথ বিভাজক)। RFC 4648-এর URL-safe রূপ এগুলোর বদলে - ও _ ব্যবহার করে, আর সাধারণত = প্যাডিংও বাদ দেয়, কারণ query string-এ =-এরও আলাদা মানে আছে।

bytes FB FF BF   standard: +/+/    URL-safe: -_-_

JWT-এর তিনটি অংশই এই রূপে থাকে। কোনো Base64 স্ট্রিংয়ে - বা _ দেখলে URL-safe হিসেবে ডিকোড করুন।

Data URI

Data URI পুরো একটা ফাইলকে URL-এর ভেতরে বসিয়ে দেয়, ফলে ব্রাউজারকে আলাদা রিকোয়েস্ট করতে হয় না:

data:text/plain;base64,SGVsbG8sIERoYWthIQ==
<img src="data:image/png;base64,iVBORw0KGgo…" alt="icon">

প্রথমটা ডিকোড করলে পাওয়া যায় Hello, Dhaka!। ছোট আইকন বা এক ফাইলের HTML রিপোর্টের জন্য data URI কাজের, কিন্তু ডেটা ৩৩% বড় হয় আর পেজ থেকে আলাদা করে ক্যাশ করা যায় না — তাই বড় ছবি সাধারণ ফাইল হিসেবেই দেওয়া ভালো।

JavaScript-এ UTF-8-এর ফাঁদ

ব্রাউজারের btoa() আর atob() সেই যুগের, যখন ধরে নেওয়া হতো স্ট্রিং মানেই Latin-1। এগুলো শুধু ০–২৫৫ কোডের অক্ষর নিয়ে কাজ করে, তাই এর বাইরের কিছু দিলেই ব্যর্থ হয়:

btoa('বাংলা');   // InvalidCharacterError: Invalid character

Base64 এনকোড করে বাইট, তাই আগে লেখাকে UTF-8 বাইটে রূপান্তর করুন, তারপর সেই বাইট এনকোড করুন — আর ডিকোডের সময় দুটো ধাপই উল্টো করুন:

// ব্রাউজার (এবং Node)
const bytes = new TextEncoder().encode('বাংলা');           // 15 UTF-8 bytes
const b64 = btoa(String.fromCharCode(...bytes));           // '4Kas4Ka+4KaC4Kay4Ka+'
const text = new TextDecoder().decode(
  Uint8Array.from(atob(b64), (c) => c.charCodeAt(0))
);                                                         // 'বাংলা'

// Node.js
Buffer.from('বাংলা', 'utf8').toString('base64');           // '4Kas4Ka+4KaC4Kay4Ka+'
Buffer.from('4Kas4Ka+4KaC4Kay4Ka+', 'base64').toString('utf8');
Buffer.from(bytes).toString('base64url');                  // URL-safe, প্যাডিং ছাড়া

TextDecoder বাদ দিয়ে শুধু atob() চালালে পাঁচটা বাংলা অক্ষরের বদলে পাবেন ১৫টা এলোমেলো Latin-1 অক্ষর — পরিচিত “mojibake” বাগ। খুব বড় ইনপুট হলে টুকরো টুকরো করে এনকোড করুন, কারণ লাখ লাখ বাইট String.fromCharCode-এ spread করলে ইঞ্জিনের আর্গুমেন্ট-সীমা ছাড়িয়ে যেতে পারে।

Python আর শেল সরাসরি বাইট নিয়ে কাজ করে:

import base64
base64.b64encode('বাংলা'.encode('utf-8'))            # b'4Kas4Ka+4KaC4Kay4Ka+'
base64.urlsafe_b64encode(bytes([0xfb, 0xff, 0xbf]))  # b'-_-_'

$ echo -n 'Hello, Dhaka!' | base64
SGVsbG8sIERoYWthIQ==

নিউলাইনের দিকে খেয়াল রাখুন: -n ছাড়া echo একটা লাইন ব্রেক যোগ করে, ফলে পাবেন SGVsbG8sIERoYWthIQo= — আলাদা একটা স্ট্রিং, যা ডিকোড করলে শেষে বাড়তি একটা নিউলাইন আসে।

কোথায় কোথায় দেখা পাবেন

Base64 এনক্রিপশন নয়। পাসওয়ার্ড বা API key Base64-এ এনকোড করলে কারও কাছ থেকেই লুকানো যায় না। এনকোডিং, এনক্রিপশন ও হ্যাশিংয়ের পার্থক্য জানতে পড়ুন এই গাইড।

এক সেকেন্ডে এনকোড বা ডিকোড। Base64 টুল UTF-8 লেখা, URL-safe আউটপুট আর পুরো ফাইল বা ছবি সামলায় — সবকিছু আপনার ব্রাউজারের ভেতরেই। এখনই চেষ্টা করুন

প্রশ্নোত্তর

আমার Base64-এর শেষে এক বা দুটি = কেন?

ইনপুটের দৈর্ঘ্য তিন বাইটের গুণিতক ছিল না। এক বাইট বাকি থাকলে আসে ==, দুই বাইট বাকি থাকলে =। এগুলো শুধুই প্যাডিং, কোনো ডেটা নেই।

Base64 দিয়ে কি ডেটা ছোট করা যায়?

না — এটা সবসময় ডেটা প্রায় এক-তৃতীয়াংশ বড় করে। আকার জরুরি হলে আগে কমপ্রেস করুন (যেমন gzip দিয়ে), আর মাধ্যমটা শুধু লেখা নিলে তবেই কমপ্রেস করা বাইট Base64 করুন।

ডিকোড করার সময় “Invalid character” আসে কেন?

সাধারণত স্ট্রিংটা URL-safe (এতে - বা _ আছে) অথচ ডিকোডার স্ট্যান্ডার্ড বর্ণমালা আশা করছে, অথবা এতে স্পেস, কোট বা data:…;base64, প্রিফিক্স আছে যা আগে সরাতে হবে।

Related guides

More free tools