How to decode a JWT (and why decoding is not verifying)

A login request fails with 401 Unauthorized, and the only clue is a long string starting with eyJ in the Authorization header. That string is a JSON Web Token (JWT), and you can read most of it without any key at all. This guide shows how a JWT is built, how to decode it by hand and in code, how to read its timestamps — and why being able to read a token tells you nothing about whether you should trust it.

The structure: header.payload.signature

A JWT is three pieces of text joined by dots. The first two are JSON objects encoded with Base64url — the URL-safe flavour of Base64 that uses - and _ instead of + and / and drops the = padding. The third piece is the signature, also Base64url, but it is raw bytes, not JSON.

PartContainsReadable?
HeaderThe algorithm (alg), the type (typ), sometimes a key ID (kid)Yes — just decode it
PayloadThe claims: who the user is, who issued the token, when it expiresYes — just decode it
SignatureA MAC or digital signature over header.payloadNo — only checkable with the right key

Why does every JWT start with eyJ? Because the header always begins with {", and {" in Base64 is eyJ.

A worked example

Here is a demo token we generated for this guide. It is signed with the throw-away secret demo-secret-not-for-production and describes a fictional user, so it is safe to paste anywhere.

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJodHRwczovL2F1dGguZXhhbXBsZS5jb20iLCJzdWIiOiJ1c2VyXzEwMjQiLCJhdWQiOiJhcGkuZXhhbXBsZS5jb20iLCJuYW1lIjoiRGVtbyBVc2VyIiwicm9sZSI6ImVkaXRvciIsImlhdCI6MTc5MDQ2NzIwMCwiZXhwIjoxNzkwNDcwODAwfQ.wn-vHA442i0OIgh1K406fNWRgxPgNev3VDMf2NZyzxg

Split it at the dots and decode the first two parts:

// header
{ "alg": "HS256", "typ": "JWT" }

// payload
{
  "iss": "https://auth.example.com",
  "sub": "user_1024",
  "aud": "api.example.com",
  "name": "Demo User",
  "role": "editor",
  "iat": 1790467200,
  "exp": 1790470800
}

The third part, wn-vHA44…, decodes to 32 bytes: the output of HMAC-SHA256. It looks like noise because it is.

Registered claims and how to read them

RFC 7519 defines seven short claim names. None is mandatory, but most APIs use several of them:

ClaimMeaningIn our token
issIssuer — who created the tokenhttps://auth.example.com
subSubject — usually the user IDuser_1024
audAudience — which API should accept it (string or array)api.example.com
expExpiration time1790470800
nbfNot before — invalid until this timenot present
iatIssued at1790467200
jtiJWT ID — a unique ID, useful for blocklists and replay detectionnot present

Everything else (name, role) is a private claim that the issuer made up.

The time claims are Unix timestamps in seconds since 1 January 1970 UTC — not milliseconds. JavaScript’s Date works in milliseconds, so multiply by 1000:

new Date(1790470800 * 1000).toISOString()
// '2026-09-27T01:00:00.000Z'  → 7:00 AM in Dhaka (UTC+6)

So this token was issued at 00:00 UTC and was valid for exactly one hour (exp − iat = 3600). Validators normally allow a small clock skew of a minute or so.

Common bug: comparing exp with Date.now() directly. Date.now() is in milliseconds, so every token looks expired since 1970. Compare with Date.now() / 1000.

Decoding in code

JavaScript (browser or Node)

function decodePart(part) {
  const b64 = part.replace(/-/g, '+').replace(/_/g, '/');
  const bytes = Uint8Array.from(atob(b64), (c) => c.charCodeAt(0));
  return JSON.parse(new TextDecoder().decode(bytes)); // UTF-8 safe
}
const [header, payload] = token.split('.').map(decodePart);
console.log(payload.exp < Date.now() / 1000); // true = expired

The TextDecoder step matters: atob returns one character per byte, so names in Bangla or with accents would come out garbled without it. In Node you can simply write Buffer.from(part, 'base64url').

Python

import base64, json

def b64url_decode(part):
    part += "=" * (-len(part) % 4)   # restore the padding
    return base64.urlsafe_b64decode(part)

header_b64, payload_b64, signature_b64 = token.split(".")
print(json.loads(b64url_decode(payload_b64)))

Command line

node -e "console.log(JSON.parse(Buffer.from(process.argv[1].split('.')[1], 'base64url')))" "$TOKEN"

Plain base64 -d often fails on JWTs because of the missing padding and the -/_ characters; convert them first or use a one-liner like the one above.

Decoding is not verifying

Everything above needed no secret. That is the point: anyone can decode a JWT, and anyone can create one. An attacker can change "role": "editor" to "admin", re-encode the payload and send it. The only thing that exposes the forgery is the signature, because it no longer matches. A server must verify the signature — and then check exp, nbf, iss and aud — before trusting a single claim.

Never make authorization decisions from a decoded-but-unverified payload on the server. Decoding in the front end to show a name or an expiry countdown is fine; the back end must still verify.

How verification works: HS256 vs RS256/ES256

HS256 (HMAC with SHA-256) uses one shared secret. The issuer computes HMAC(secret, header + "." + payload); the verifier recomputes it with the same secret and compares. Simple and fast, but every service that can verify can also mint tokens, so the secret must stay on trusted servers.

import { createHmac, timingSafeEqual } from 'node:crypto';

function verifyHS256(token, secret) {
  const [h, p, s] = token.split('.');
  const header = JSON.parse(Buffer.from(h, 'base64url'));
  if (header.alg !== 'HS256') throw new Error('unexpected alg');
  const expected = createHmac('sha256', secret).update(`${h}.${p}`).digest();
  const given = Buffer.from(s, 'base64url');
  if (given.length !== expected.length || !timingSafeEqual(given, expected)) {
    throw new Error('bad signature');
  }
  return JSON.parse(Buffer.from(p, 'base64url'));
}

That sketch shows the mechanism only — it still skips the exp/aud/iss checks. In production use a maintained library such as jose (JavaScript) or PyJWT (Python) and pass the allowed algorithms explicitly.

RS256 (RSA) and ES256 (ECDSA on the P-256 curve) are asymmetric. The issuer signs with a private key it never shares; anyone verifies with the matching public key. Identity providers publish their public keys as a JWKS (JSON Web Key Set), usually at a URL such as /.well-known/jwks.json. The token’s kid header says which key in the set to use, which lets the provider rotate keys without breaking clients. Use asymmetric algorithms whenever the verifier is a different party from the issuer.

What about JWE? A signed JWT (JWS) is readable by anyone; a JSON Web Encryption token actually encrypts the payload. You can recognise one because it has five dot-separated parts instead of three, and the payload cannot be decoded without the key.

Decode and verify in one place. The JWT Decoder shows the header and claims, turns exp into a readable date and checks HS, RS, PS and ES signatures right in your browser — the token never leaves your device. Try it now

Next, read 8 JWT security mistakes developers still make to see what goes wrong when verification is done carelessly.

FAQ

Can I decode a JWT without the secret key?

Yes. The header and payload are only Base64url-encoded, not encrypted. The key is needed to verify the signature (or to create a new valid token), not to read the claims.

Why does my decoder say the token is expired when it just arrived?

Check the units first: exp is in seconds. Then check the server and client clocks — a machine that is a few minutes off will reject fresh tokens. Most libraries accept a small leeway setting for this.

Is it safe to paste a real token into an online decoder?

Only if decoding happens locally. A valid access token is a password for as long as it lives. JSON Bondhu decodes and verifies in your browser, but for production tokens the safest habit is to use a demo token or wait until it expires.

What is the difference between sub and jti?

sub identifies the user (the same for every token that user gets); jti identifies this one token, so a server can revoke it or detect it being replayed.

JWT কীভাবে ডিকোড করবেন (এবং কেন ডিকোড করা মানে যাচাই করা নয়)

লগইন রিকোয়েস্ট 401 Unauthorized দিয়ে ফেল করছে, আর হাতে একমাত্র সূত্র হলো Authorization হেডারে eyJ দিয়ে শুরু হওয়া একটা লম্বা স্ট্রিং। এটাই JSON Web Token (JWT), আর এর বেশিরভাগ অংশ কোনো কী ছাড়াই পড়া যায়। এই গাইডে দেখবেন JWT কীভাবে গঠিত, হাতে ও কোডে কীভাবে ডিকোড করবেন, টাইমস্ট্যাম্প কীভাবে পড়বেন — আর কেন টোকেন পড়তে পারা মানেই তাকে বিশ্বাস করা যায় না।

গঠন: header.payload.signature

JWT হলো ডট দিয়ে জোড়া তিন টুকরো লেখা। প্রথম দুটি JSON অবজেক্ট, যা Base64url দিয়ে এনকোড করা — Base64-এর URL-safe সংস্করণ, যেখানে + ও /-এর বদলে - ও _ থাকে, আর শেষের = প্যাডিং বাদ দেওয়া হয়। তৃতীয় টুকরোটি সিগনেচার; এটাও Base64url, তবে ভেতরে JSON নয়, কাঁচা বাইট।

অংশকী থাকেপড়া যায়?
Headerঅ্যালগরিদম (alg), টাইপ (typ), কখনো কী-এর আইডি (kid)হ্যাঁ — শুধু ডিকোড করলেই হয়
Payloadক্লেইমগুলো: ইউজার কে, টোকেন কে দিয়েছে, মেয়াদ কখন শেষহ্যাঁ — শুধু ডিকোড করলেই হয়
Signatureheader.payload-এর ওপর MAC বা ডিজিটাল সিগনেচারনা — সঠিক কী দিয়েই কেবল যাচাই করা যায়

সব JWT কেন eyJ দিয়ে শুরু হয়? কারণ হেডার সবসময় {" দিয়ে শুরু, আর Base64-এ {" হয় eyJ।

একটি হাতে-কলমে উদাহরণ

এই গাইডের জন্য আমরা একটি ডেমো টোকেন বানিয়েছি। এটি ফেলে দেওয়ার মতো সিক্রেট demo-secret-not-for-production দিয়ে সাইন করা এবং একজন কাল্পনিক ইউজারের তথ্য বহন করে, তাই যেকোনো জায়গায় পেস্ট করা নিরাপদ।

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJodHRwczovL2F1dGguZXhhbXBsZS5jb20iLCJzdWIiOiJ1c2VyXzEwMjQiLCJhdWQiOiJhcGkuZXhhbXBsZS5jb20iLCJuYW1lIjoiRGVtbyBVc2VyIiwicm9sZSI6ImVkaXRvciIsImlhdCI6MTc5MDQ2NzIwMCwiZXhwIjoxNzkwNDcwODAwfQ.wn-vHA442i0OIgh1K406fNWRgxPgNev3VDMf2NZyzxg

ডট ধরে ভাগ করুন, তারপর প্রথম দুটি অংশ ডিকোড করুন:

// header
{ "alg": "HS256", "typ": "JWT" }

// payload
{
  "iss": "https://auth.example.com",
  "sub": "user_1024",
  "aud": "api.example.com",
  "name": "Demo User",
  "role": "editor",
  "iat": 1790467200,
  "exp": 1790470800
}

তৃতীয় অংশ wn-vHA44… ডিকোড করলে পাওয়া যায় ৩২ বাইট: HMAC-SHA256-এর আউটপুট। দেখতে এলোমেলো লাগে, কারণ এটা আসলেই এলোমেলো দেখানোর জন্যই তৈরি।

রেজিস্টার্ড ক্লেইম ও সেগুলো পড়ার নিয়ম

RFC 7519 সাতটি ছোট নামের ক্লেইম ঠিক করে দিয়েছে। কোনোটিই বাধ্যতামূলক নয়, তবে বেশিরভাগ API এর কয়েকটি ব্যবহার করে:

ক্লেইমমানেআমাদের টোকেনে
issIssuer — টোকেনটি কে বানিয়েছেhttps://auth.example.com
subSubject — সাধারণত ইউজারের আইডিuser_1024
audAudience — কোন API এটি গ্রহণ করবে (স্ট্রিং বা অ্যারে)api.example.com
expমেয়াদ শেষের সময়1790470800
nbfNot before — এই সময়ের আগে অবৈধনেই
iatIssued at — কখন ইস্যু হয়েছে1790467200
jtiJWT ID — টোকেনের অনন্য আইডি, ব্লকলিস্ট ও রিপ্লে ধরার কাজে লাগেনেই

বাকিগুলো (name, role) প্রাইভেট ক্লেইম — ইস্যুকারী নিজের মতো করে বানিয়েছে।

সময়ের ক্লেইমগুলো হলো ১ জানুয়ারি ১৯৭০ UTC থেকে গোনা সেকেন্ডে ইউনিক্স টাইমস্ট্যাম্প — মিলিসেকেন্ড নয়। JavaScript-এর Date মিলিসেকেন্ডে কাজ করে, তাই ১০০০ দিয়ে গুণ করুন:

new Date(1790470800 * 1000).toISOString()
// '2026-09-27T01:00:00.000Z'  → ঢাকায় সকাল ৭টা (UTC+6)

অর্থাৎ টোকেনটি UTC রাত ১২টায় ইস্যু হয়েছে এবং ঠিক এক ঘণ্টা বৈধ ছিল (exp − iat = 3600)। ভ্যালিডেটররা সাধারণত ঘড়ির অমিলের জন্য এক মিনিটের মতো ছাড় দেয়।

পরিচিত বাগ: exp-কে সরাসরি Date.now()-এর সাথে তুলনা করা। Date.now() মিলিসেকেন্ডে, ফলে সব টোকেনই মনে হবে মেয়াদোত্তীর্ণ। তুলনা করুন Date.now() / 1000-এর সাথে।

কোডে ডিকোড করা

JavaScript (ব্রাউজার বা Node)

function decodePart(part) {
  const b64 = part.replace(/-/g, '+').replace(/_/g, '/');
  const bytes = Uint8Array.from(atob(b64), (c) => c.charCodeAt(0));
  return JSON.parse(new TextDecoder().decode(bytes)); // UTF-8 ঠিক থাকে
}
const [header, payload] = token.split('.').map(decodePart);
console.log(payload.exp < Date.now() / 1000); // true = মেয়াদ শেষ

TextDecoder-এর ধাপটা জরুরি: atob প্রতিটি বাইটকে একটা করে অক্ষর হিসেবে ফেরত দেয়, তাই এটা ছাড়া বাংলা নাম বা অ্যাকসেন্টওয়ালা অক্ষর ভেঙে যাবে। Node-এ সহজভাবে Buffer.from(part, 'base64url') লিখলেই চলে।

Python

import base64, json

def b64url_decode(part):
    part += "=" * (-len(part) % 4)   # প্যাডিং ফিরিয়ে আনা
    return base64.urlsafe_b64decode(part)

header_b64, payload_b64, signature_b64 = token.split(".")
print(json.loads(b64url_decode(payload_b64)))

কমান্ড লাইন

node -e "console.log(JSON.parse(Buffer.from(process.argv[1].split('.')[1], 'base64url')))" "$TOKEN"

সাধারণ base64 -d দিয়ে JWT ডিকোড করতে গেলে প্রায়ই এরর আসে — প্যাডিং নেই, আর -/_ অক্ষর আছে। আগে এগুলো বদলে নিন, অথবা ওপরের মতো এক লাইনের কমান্ড ব্যবহার করুন।

ডিকোড করা মানে যাচাই করা নয়

ওপরের কোনো কাজেই সিক্রেট লাগেনি। আসল কথা এটাই: যে কেউ JWT ডিকোড করতে পারে, আবার যে কেউ JWT বানাতেও পারে। একজন অ্যাটাকার "role": "editor" বদলে "admin" করে পেলোড আবার এনকোড করে পাঠিয়ে দিতে পারে। এই জালিয়াতি ধরার একমাত্র উপায় সিগনেচার, কারণ তখন সেটা আর মিলবে না। কোনো ক্লেইম বিশ্বাস করার আগে সার্ভারকে অবশ্যই সিগনেচার যাচাই করতে হবে — তারপর exp, nbf, iss ও aud চেক করতে হবে।

সার্ভারে ডিকোড করা কিন্তু যাচাই না করা পেলোড দেখে কখনোই অনুমতির সিদ্ধান্ত নেবেন না। ফ্রন্টএন্ডে নাম দেখানো বা মেয়াদের কাউন্টডাউনের জন্য ডিকোড করা ঠিক আছে; কিন্তু ব্যাকএন্ডকে যাচাই করতেই হবে।

যাচাই কীভাবে কাজ করে: HS256 বনাম RS256/ES256

HS256 (SHA-256 সহ HMAC) একটি শেয়ার্ড সিক্রেট ব্যবহার করে। ইস্যুকারী হিসাব করে HMAC(secret, header + "." + payload); যাচাইকারী একই সিক্রেট দিয়ে আবার হিসাব করে মিলিয়ে দেখে। সহজ ও দ্রুত, কিন্তু যে সার্ভিস যাচাই করতে পারে সে নতুন টোকেনও বানাতে পারে — তাই সিক্রেট শুধু বিশ্বস্ত সার্ভারেই থাকা উচিত।

import { createHmac, timingSafeEqual } from 'node:crypto';

function verifyHS256(token, secret) {
  const [h, p, s] = token.split('.');
  const header = JSON.parse(Buffer.from(h, 'base64url'));
  if (header.alg !== 'HS256') throw new Error('unexpected alg');
  const expected = createHmac('sha256', secret).update(`${h}.${p}`).digest();
  const given = Buffer.from(s, 'base64url');
  if (given.length !== expected.length || !timingSafeEqual(given, expected)) {
    throw new Error('bad signature');
  }
  return JSON.parse(Buffer.from(p, 'base64url'));
}

এই স্কেচটা শুধু পদ্ধতিটা বোঝানোর জন্য — এতে এখনো exp/aud/iss চেক নেই। প্রোডাকশনে jose (JavaScript) বা PyJWT (Python)-এর মতো নিয়মিত রক্ষণাবেক্ষণ হওয়া লাইব্রেরি ব্যবহার করুন, আর অনুমোদিত অ্যালগরিদম স্পষ্ট করে বলে দিন।

RS256 (RSA) ও ES256 (P-256 কার্ভে ECDSA) অ্যাসিমেট্রিক। ইস্যুকারী একটি প্রাইভেট কী দিয়ে সাইন করে, যা সে কাউকে দেয় না; আর যে কেউ মিলে যাওয়া পাবলিক কী দিয়ে যাচাই করতে পারে। আইডেন্টিটি প্রোভাইডাররা তাদের পাবলিক কী JWKS (JSON Web Key Set) হিসেবে প্রকাশ করে, সাধারণত /.well-known/jwks.json-এর মতো কোনো URL-এ। টোকেনের kid হেডার বলে দেয় সেটের কোন কী দিয়ে যাচাই করতে হবে — এতে প্রোভাইডার ক্লায়েন্ট না ভেঙেই কী বদলাতে (rotate) পারে। যাচাইকারী আর ইস্যুকারী আলাদা পক্ষ হলে অ্যাসিমেট্রিক অ্যালগরিদমই ব্যবহার করুন।

আর JWE? সাইন করা JWT (JWS) যে কেউ পড়তে পারে; JSON Web Encryption টোকেন আসলেই পেলোড এনক্রিপ্ট করে। চেনার উপায়: এতে ডট দিয়ে ভাগ করা তিনটির বদলে পাঁচটি অংশ থাকে, আর কী ছাড়া পেলোড ডিকোড করা যায় না।

এক জায়গায় ডিকোড ও যাচাই। JWT ডিকোডার হেডার ও ক্লেইম দেখায়, exp-কে পড়ার মতো তারিখে বদলে দেয় এবং HS, RS, PS ও ES সিগনেচার আপনার ব্রাউজারেই যাচাই করে — টোকেন আপনার ডিভাইসের বাইরে যায় না। এখনই চেষ্টা করুন

এরপর পড়ুন JWT নিয়ে ৮টি নিরাপত্তা-ভুল — অসাবধানে যাচাই করলে কী কী ভুল হয় তা জানতে।

প্রশ্নোত্তর

সিক্রেট কী ছাড়া কি JWT ডিকোড করা যায়?

হ্যাঁ। হেডার ও পেলোড শুধু Base64url-এ এনকোড করা, এনক্রিপ্ট করা নয়। কী লাগে সিগনেচার যাচাই করতে (বা নতুন বৈধ টোকেন বানাতে), ক্লেইম পড়তে নয়।

টোকেন এইমাত্র এলো, তবু ডিকোডার বলছে মেয়াদ শেষ — কেন?

আগে এককটা দেখুন: exp সেকেন্ডে। তারপর সার্ভার আর ক্লায়েন্টের ঘড়ি মিলিয়ে দেখুন — কয়েক মিনিট আগে-পিছে চলা মেশিন নতুন টোকেনও বাতিল করে দেয়। বেশিরভাগ লাইব্রেরিতে এর জন্য ছোট একটা leeway সেটিং থাকে।

আসল টোকেন কি অনলাইন ডিকোডারে পেস্ট করা নিরাপদ?

কেবল তখনই, যদি ডিকোডিং আপনার ডিভাইসেই হয়। বৈধ access token যতক্ষণ বেঁচে থাকে, ততক্ষণ সেটা পাসওয়ার্ডের মতোই। JSON বন্ধু ব্রাউজারেই ডিকোড ও যাচাই করে, তবু প্রোডাকশন টোকেনের জন্য সবচেয়ে নিরাপদ অভ্যাস হলো ডেমো টোকেন ব্যবহার করা বা মেয়াদ শেষ হওয়া পর্যন্ত অপেক্ষা করা।

sub আর jti-এর পার্থক্য কী?

sub ইউজারকে চিহ্নিত করে (ওই ইউজারের সব টোকেনে একই থাকে); jti চিহ্নিত করে নির্দিষ্ট এই টোকেনটিকে, যাতে সার্ভার সেটা বাতিল করতে বা আবার ব্যবহার হচ্ছে কি না ধরতে পারে।

Related guides

More free tools