ToolDeck

Python Base64 Decode — คู่มือ base64.b64decode()

·Backend Developer·ตรวจสอบโดยDmitri Volkov·เผยแพร่เมื่อ

ใช้ ถอดรหัส Base64 ออนไลน์ ฟรีโดยตรงในเบราว์เซอร์ของคุณ — ไม่ต้องติดตั้ง

ลอง ถอดรหัส Base64 ออนไลน์ ออนไลน์ →

เมื่อ API ส่งคืนฟิลด์ content ที่ดูเหมือน eyJob3N0IjogImRiLXByb2Qi…, หรือตัวจัดการ secrets ส่งข้อมูลรับรองที่เข้ารหัสมาให้, หรือคุณต้องแยก JWT payload—— การถอดรหัส base64 ใน Python คือจุดแรกของคุณ โมดูล base64 ที่ติดตั้งมาพร้อมกันจัดการทั้งหมดได้ แต่รายละเอียดเล็กน้อยเกี่ยวกับ bytes กับ strings, ตัวอักษรที่ปลอดภัยสำหรับ URL และ padding ที่หายไปมักทำให้นักพัฒนาเกือบทุกคนสะดุดอย่างน้อยครั้งหนึ่ง—— ผมได้ debug ข้อผิดพลาดประเภทนี้ใน code review บ่อยกว่าที่อยากจะยอมรับ คู่มือนี้ครอบคลุม base64.b64decode(), urlsafe_b64decode(), การซ่อมแซม padding อัตโนมัติ, การถอดรหัสจากไฟล์และ HTTP response, เครื่องมือ CLI, การตรวจสอบ input และข้อผิดพลาดทั่วไปสี่อย่างพร้อมวิธีแก้ไขก่อน/หลัง——ตัวอย่างทั้งหมดรันได้บน Python 3.8+ หากคุณต้องการถอดรหัสครั้งเดียวอย่างรวดเร็วโดยไม่ต้องเขียนโค้ด เครื่องมือถอดรหัส Base64 ของ ToolDeck จัดการ Base64 ทั้งแบบมาตรฐาน และแบบปลอดภัยสำหรับ URL ได้ทันทีในเบราว์เซอร์ของคุณ

  • base64.b64decode(s) มีอยู่ใน Python stdlib แล้ว — ไม่ต้องติดตั้งเพิ่ม; คืนค่าเป็น bytes เสมอ ไม่ใช่ str
  • ต่อ .decode("utf-8") ต่อจาก b64decode() เพื่อแปลง bytes เป็น Python string — ฟังก์ชันนี้ไม่รู้ encoding ข้อความต้นฉบับ
  • สำหรับ Base64 ที่ปลอดภัยสำหรับ URL (ใช้ - และ _ แทน + และ /) ให้ใช้ base64.urlsafe_b64decode() — มาตรฐานใน JWT, OAuth token และ Google API credentials
  • แก้ไขข้อผิดพลาด "Incorrect padding" ที่พบบ่อยด้วย: padded = s + "=" * (-len(s) % 4) — เพิ่ม 0, 1 หรือ 2 ตัวอักษรตามที่จำเป็น
  • ตั้ง validate=True สำหรับ input จากแหล่งภายนอก เพื่อ raise binascii.Error กับตัวอักษรที่ไม่ใช่ Base64 แทนการข้ามผ่านเงียบๆ

การถอดรหัส Base64 คืออะไร?

Base64 เป็นรูปแบบการเข้ารหัสที่แสดงข้อมูลไบนารีใดๆ ในรูปแบบสตริงของ 64 ตัวอักษร ASCII ที่พิมพ์ได้:A–Z, a–z, 0–9, + และ /, โดยใช้ = เป็น padding ทุกๆ 4 ตัวอักษร Base64 เข้ารหัส 3 bytes ต้นฉบับพอดี ดังนั้นรูปแบบที่เข้ารหัสจะใหญ่กว่าต้นฉบับประมาณ 33% การถอดรหัสย้อนกลับกระบวนการนี้ — แปลงการแสดง ASCII กลับเป็น bytes ต้นฉบับ

Base64 ไม่เข้ารหัสข้อมูล มันเป็นเพียงการเข้ารหัสแบบไบนารีเป็นข้อความ — ใครก็ตามที่รันผ่านตัวถอดรหัส สามารถอ่านสตริงที่เข้ารหัสได้อย่างสมบูรณ์:

ก่อน — Base64 ที่เข้ารหัสแล้ว

eyJob3N0IjogImRiLXByb2QubXljb21wYW55LmludGVybmFsIiwgInBvcnQiOiA1NDMyLCAidXNlciI6ICJhcHBfc3ZjIn0=

หลัง — ถอดรหัสแล้ว

{"host": "db-prod.mycompany.internal", "port": 5432, "user": "app_svc"}

base64.b64decode() — การถอดรหัสด้วย Standard Library

โมดูล base64 ของ Python มาพร้อมกับ standard library——ไม่ต้องติดตั้งเพิ่ม พร้อมใช้งานเสมอ ฟังก์ชันหลักคือ base64.b64decode(s, altchars=None, validate=False)รับ str, bytes หรือ bytearray และคืนค่าเป็น bytes เสมอ

ตัวอย่างขั้นต่ำที่ใช้งานได้

Python 3.8+
import base64
import json

# Encoded database config received from a secrets manager
encoded_config = (
    "eyJob3N0IjogImRiLXByb2QubXljb21wYW55LmludGVybmFsIiwgInBvcnQiOiA1NDMyLCAid"
    "XNlciI6ICJhcHBfc3ZjIiwgInBhc3N3b3JkIjogInM0ZmVQYXNzITIwMjYifQ=="
)

# Step 1: decode Base64 bytes
raw_bytes = base64.b64decode(encoded_config)
print(raw_bytes)
# b'{"host": "db-prod.mycompany.internal", "port": 5432, "user": "app_svc", "password": "s4fePass!2026"}'

# Step 2: convert bytes → str
config_str = raw_bytes.decode("utf-8")

# Step 3: parse into a dict
config = json.loads(config_str)
print(config["host"])    # db-prod.mycompany.internal
print(config["port"])    # 5432
หมายเหตุ:b64decode() คืนค่าเป็น bytes เสมอ — ไม่ใช่ string หากข้อมูลต้นฉบับเป็นข้อความ ให้ต่อ .decode("utf-8")หากเป็นข้อมูลไบนารี (รูปภาพ, PDF, kzip archive) ให้เก็บ bytes ไว้ตามเดิม แล้วเขียนลงไฟล์หรือส่งต่อให้ library ที่ใช้งานโดยตรง

ตัวอย่างขยาย: sort_keys, ensure_ascii และการตรวจสอบอย่างเข้มงวด

Python 3.8+
import base64
import binascii

# Token from an internal event bus — validate strictly (external input)
encoded_event = (
    "eyJldmVudCI6ICJvcmRlci5zaGlwcGVkIiwgIm9yZGVyX2lkIjogIk9SRC04ODQ3MiIsICJ"
    "0aW1lc3RhbXAiOiAiMjAyNi0wMy0xM1QxNDozMDowMFoiLCAicmVnaW9uIjogImV1LXdlc3QtMSJ9"
)

try:
    # validate=True raises binascii.Error on any non-Base64 character
    raw = base64.b64decode(encoded_event, validate=True)
    event = raw.decode("utf-8")
    print(event)
    # {"event": "order.shipped", "order_id": "ORD-88472", "timestamp": "2026-03-13T14:30:00Z", "region": "eu-west-1"}

except binascii.Error as exc:
    print(f"Invalid Base64: {exc}")
except UnicodeDecodeError as exc:
    print(f"Not UTF-8 text: {exc}")

การถอดรหัส Base64 ที่ปลอดภัยสำหรับ URL (base64url)

Base64 มาตรฐานใช้ + และ /ซึ่งเป็นตัวอักษรสงวนใน URL ตัวแปรที่ปลอดภัยสำหรับ URL (RFC 4648 §5 เรียกว่า “base64url”) แทนที่ด้วย - และ _นี่คือการเข้ารหัสที่ใช้ใน JWT token, OAuth 2.0 PKCE challenge, Google Cloud credentials และ web authentication flows สมัยใหม่ส่วนใหญ่

การส่ง Base64 ที่ปลอดภัยสำหรับ URL ไปยัง b64decode() โดยไม่ปรับตัวอักษรจะทำให้ ข้อมูลเสียหายอย่างเงียบๆ หรือ raise binascii.Errorใช้ base64.urlsafe_b64decode() แทน — มันจัดการการแทนที่-+ และ _/ โดยอัตโนมัติ

Python 3.8+
import base64
import json

# JWT payload segment (the middle part between the two dots)
# JWTs use URL-safe Base64 without trailing "=" padding
jwt_payload_b64 = (
    "eyJ1c2VyX2lkIjogMjg5MywgInJvbGUiOiAiYWRtaW4iLCAiaXNzIjogImF1dGgubXljb21w"
    "YW55LmNvbSIsICJleHAiOiAxNzQwOTAwMDAwLCAianRpIjogImFiYzEyMzQ1LXh5ei05ODc2In0"
)

# Restore padding before decoding (JWT deliberately omits '=')
padded = jwt_payload_b64 + "=" * (-len(jwt_payload_b64) % 4)

payload_bytes = base64.urlsafe_b64decode(padded)
payload = json.loads(payload_bytes.decode("utf-8"))

print(payload["role"])    # admin
print(payload["iss"])     # auth.mycompany.com
print(payload["user_id"]) # 2893
หมายเหตุ:นิพจน์ "=" * (-len(s) % 4) เพิ่มตัวอักษร padding ได้พอดี 0, 1 หรือ 2 ตัว ตามที่จำเป็น และเป็น no-op เมื่อ string มี padding ถูกต้องแล้ว นี่คือวิธีแก้ไขปัญหา padding ของ JWT และ OAuth แบบ idiomatic Python

คู่มือ Parameter ของ base64.b64decode()

parameter ทั้งหมดด้านล่างใช้ได้กับทั้ง b64decode() และ urlsafe_b64decode()ยกเว้น altchars ที่ใช้ได้เฉพาะใน b64decode()

Parameterประเภทค่าเริ่มต้นคำอธิบาย
sbytes | str | bytearrayInput ที่เข้ารหัส Base64 สำหรับถอดรหัส; รับ ASCII str พร้อมกับประเภท bytes
altcharsbytes | NoneNoneลำดับ 2 bytes ที่แทนที่ + และ /; ช่วยให้ใช้ตัวอักษร Base64 แบบกำหนดเองนอกเหนือจากตัวแปรปลอดภัย URL มาตรฐาน
validateboolFalseเมื่อ True จะ raise binascii.Error กับตัวอักษรนอกตัวอักษร Base64; เมื่อ False bytes ที่ไม่ใช่ตัวอักษร (บรรทัดใหม่, ช่องว่าง) จะถูกละเว้นอย่างเงียบๆ

ค่าเริ่มต้น validate=False มีจุดประสงค์สำหรับข้อมูลรูปแบบ PEM และ Base64 หลายบรรทัด (ที่บรรทัดใหม่เป็นเรื่องปกติ) สำหรับ API payload, การอัปโหลดของผู้ใช้ หรือ input ที่ไม่น่าเชื่อถือใดๆ ให้ส่ง validate=True เพื่อตรวจจับข้อมูลที่เสียหายหรือถูกแทรกได้เร็วขึ้นและแสดงข้อผิดพลาดที่ชัดเจน

ข้อผิดพลาด Padding การถอดรหัส Base64 ใน Python — วิธีแก้ไข

ข้อผิดพลาดที่พบบ่อยที่สุดเมื่อถอดรหัส Base64 ใน Python คือ:

Python 3.8+
import base64

base64.b64decode("eyJ0eXBlIjogImFjY2VzcyJ9")
# binascii.Error: Incorrect padding

Base64 ต้องการความยาว string เป็นจำนวนคูณของ 4 เมื่อข้อมูลผ่าน URL, HTTP header หรือ JWT library padding = ท้ายจะถูกตัดออก เพื่อประหยัด bytes มีสองวิธีที่เชื่อถือได้ในการแก้ไขนี้

ตัวเลือก 1: คืน padding แบบ inline (แนะนำ)

Python 3.8+
import base64
import json

def b64decode_unpadded(data: str | bytes) -> bytes:
    """Decode Base64 with automatic padding correction."""
    if isinstance(data, str):
        data = data.encode("ascii")
    data += b"=" * (-len(data) % 4)
    return base64.b64decode(data)

# Works regardless of how many '=' were stripped
token_a = "eyJ0eXBlIjogImFjY2VzcyJ9"       # 0 chars of padding stripped
token_b = "eyJ0eXBlIjogInJlZnJlc2gifQ"      # 1 char stripped
token_c = "eyJ0eXBlIjogImFwaV9rZXkifQ=="    # already padded

for token in (token_a, token_b, token_c):
    result = json.loads(b64decode_unpadded(token).decode("utf-8"))
    print(result["type"])
# access
# refresh
# api_key

ตัวเลือก 2: ถอดรหัสแบบปลอดภัย URL พร้อม padding สำหรับ OAuth / JWT

Python 3.8+
import base64
import json

def decode_jwt_segment(segment: str) -> dict:
    """Decode a single JWT segment (header or payload)."""
    # Add padding, use URL-safe alphabet
    padded = segment + "=" * (-len(segment) % 4)
    raw = base64.urlsafe_b64decode(padded)
    return json.loads(raw.decode("utf-8"))

# Google OAuth ID token payload (simplified)
id_token_payload = (
    "eyJzdWIiOiAiMTEwNTY5NDkxMjM0NTY3ODkwMTIiLCAiZW1haWwiOiAic2FyYS5jaGVuQGV4"
    "YW1wbGUuY29tIiwgImhkIjogImV4YW1wbGUuY29tIiwgImlhdCI6IDE3NDA5MDAwMDB9"
)

claims = decode_jwt_segment(id_token_payload)
print(claims["email"])   # sara.chen@example.com
print(claims["hd"])      # example.com

การถอดรหัส Base64 จากไฟล์และ API Response

การอ่าน Base64 จากดิสก์และการถอดรหัส API payload คือสองสถานการณ์ production ที่พบบ่อยที่สุด ทั้งสองต้องการการจัดการข้อผิดพลาดที่เหมาะสม — padding ที่เสียหายและประเภทไบนารีที่ไม่คาดคิด เป็นเรื่องที่เกิดขึ้นจริง ไม่ใช่แค่ edge case ทางทฤษฎี

การอ่านและถอดรหัสไฟล์ Base64

Python 3.8+
import base64
import json
from pathlib import Path

def decode_attachment(envelope_path: str, output_path: str) -> None:
    """
    Read a JSON envelope with a Base64-encoded attachment,
    decode it, and write the binary output to disk.
    """
    try:
        envelope = json.loads(Path(envelope_path).read_text(encoding="utf-8"))
        encoded_data = envelope["attachment"]["data"]
        file_bytes = base64.b64decode(encoded_data, validate=True)
        Path(output_path).write_bytes(file_bytes)
        print(f"Saved {len(file_bytes):,} bytes → {output_path}")
    except FileNotFoundError:
        print(f"Envelope file not found: {envelope_path}")
    except (KeyError, TypeError):
        print("Unexpected envelope structure — 'attachment.data' missing")
    except base64.binascii.Error as exc:
        print(f"Invalid Base64 content: {exc}")

# Example envelope:
# {"attachment": {"filename": "invoice_2026_03.pdf", "data": "JVBERi0xLjQK..."}}
decode_attachment("order_ORD-88472.json", "invoice_2026_03.pdf")

การถอดรหัส Base64 จาก HTTP API response

Python 3.8+
import base64
import json
import urllib.request

def fetch_and_decode_secret(vault_url: str, secret_name: str) -> str:
    """
    Retrieve a Base64-encoded secret from an internal vault API
    and return the decoded plaintext value.
    """
    url = f"{vault_url}/v1/secrets/{secret_name}"
    req = urllib.request.Request(url, headers={"X-Vault-Token": "s.internal"})

    try:
        with urllib.request.urlopen(req, timeout=5) as resp:
            body = json.loads(resp.read().decode("utf-8"))
            # Vault returns: {"data": {"value": "<base64>", "encoding": "base64"}}
            encoded = body["data"]["value"]
            return base64.b64decode(encoded).decode("utf-8")

    except urllib.error.URLError as exc:
        raise RuntimeError(f"Vault unreachable: {exc}") from exc
    except (KeyError, UnicodeDecodeError, base64.binascii.Error) as exc:
        raise ValueError(f"Unexpected secret format: {exc}") from exc

# db_pass = fetch_and_decode_secret("https://vault.internal", "db-prod-password")
# print(db_pass)  # s4feP@ss!2026
หมายเหตุ:หากคุณใช้ library requests ให้แทนที่ urllib.request ด้วย resp = requests.get(url, timeout=5, headers=headers) และ body = resp.json() logic การถอดรหัส Base64 เหมือนกันทุกประการ

การถอดรหัส Base64 ผ่าน Command Line

สำหรับการตรวจสอบ terminal อย่างรวดเร็ว — การยืนยัน token, การดู encoded config blob หรือการ pipe API output ผ่านตัวถอดรหัส — คำสั่ง base64 มีให้ใช้บน Linux และ macOS โมดูล -m base64 ของ Python ทำงานข้ามแพลตฟอร์ม รวมถึง Windows

Bash
# Decode a Base64 string and print the result (Linux / macOS)
echo "eyJob3N0IjogImRiLXByb2QubXljb21wYW55LmludGVybmFsIn0=" | base64 --decode
# {"host": "db-prod.mycompany.internal"}

# Decode a file, save decoded output
base64 --decode encoded_payload.txt > decoded_output.json

# Python's cross-platform CLI decoder (works on Windows too)
python3 -m base64 -d encoded_payload.txt

# Decode a JWT payload segment inline — strip header/signature first
echo "eyJ1c2VyX2lkIjogMjg5MywgInJvbGUiOiAiYWRtaW4ifQ" | python3 -c "
import sys, base64, json
s = sys.stdin.read().strip()
padded = s + '=' * (-len(s) % 4)
print(json.dumps(json.loads(base64.urlsafe_b64decode(padded)), indent=2))
"

สำหรับงานสำรวจที่การเขียน shell pipeline ดูเกินความจำเป็น วาง string ลงใน ตัวถอดรหัส Base64 ออนไลน์ — มันตรวจจับ input ที่ปลอดภัยสำหรับ URL โดยอัตโนมัติและแก้ไข padding ทันที

การตรวจสอบ Input Base64 ก่อนถอดรหัส

เมื่อข้อมูล Base64 มาจาก user input, webhook หรือ API จากบุคคลที่สามที่ไม่น่าเชื่อถือ ให้ตรวจสอบก่อนถอดรหัสเพื่อแสดงข้อผิดพลาดที่ชัดเจนและดำเนินการได้ แทนที่จะเป็น traceback binascii.Error ที่สร้างความสับสนในส่วนลึกของ business logic Python มีสองวิธี: จับ exception หรือตรวจสอบล่วงหน้าด้วย regex

Python 3.8+
import base64
import binascii
import re

# ── Option A: try/except (recommended for most code paths) ──────────────────

def safe_b64decode(data: str) -> bytes | None:
    """Return decoded bytes, or None if the input is not valid Base64."""
    try:
        padded = data + "=" * (-len(data) % 4)
        return base64.b64decode(padded, validate=True)
    except (binascii.Error, ValueError):
        return None

print(safe_b64decode("not-base64!!"))                     # None
print(safe_b64decode("eyJ0eXBlIjogInJlZnJlc2gifQ"))      # b'{"type": "refresh"}'


# ── Option B: regex pre-validation ──────────────────────────────────────────

# Standard Base64 (alphabet: A-Z a-z 0-9 + /)
_STANDARD_RE = re.compile(r"^[A-Za-z0-9+/]*={0,2}$")

# URL-safe Base64 (alphabet: A-Z a-z 0-9 - _)
_URLSAFE_RE = re.compile(r"^[A-Za-z0-9-_]*={0,2}$")

def is_valid_base64(s: str) -> bool:
    """True if s is a syntactically valid standard Base64 string."""
    # Length must be a multiple of 4 for fully padded strings
    stripped = s.rstrip("=")
    padded = stripped + "=" * (-len(stripped) % 4)
    return bool(_STANDARD_RE.match(padded))

print(is_valid_base64("SGVsbG8gV29ybGQ="))   # True
print(is_valid_base64("SGVsbG8gV29ybGQ!"))   # False  (! is not Base64)

ทางเลือกประสิทธิภาพสูง: pybase64

สำหรับกรณีการใช้งานส่วนใหญ่ โมดูล base64 ของ stdlib Python เพียงพออย่างสมบูรณ์ หากคุณประมวลผล API payload หลายพันรายการต่อวินาที, ถอดรหัสไฟล์แนบไบนารีหลาย megabyte ในลูปที่แน่น หรือ profiler แสดงว่าการดำเนินการ Base64 เป็น hotspot — ลองพิจารณา pybase64มันคือ C-extension wrapper รอบ libbase64 และโดยทั่วไปจะ เร็วกว่าการใช้งาน stdlib 2–5 เท่าสำหรับ input ขนาดใหญ่

Bash
pip install pybase64
Python 3.8+
import pybase64

# Drop-in replacement — identical API to the stdlib base64 module
encoded_image = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAC0lEQVQI12NgAAIABQ..."

image_bytes = pybase64.b64decode(encoded_image, validate=False)
print(f"Decoded {len(image_bytes):,} bytes")

# URL-safe variant — same as base64.urlsafe_b64decode()
token_bytes = pybase64.urlsafe_b64decode("eyJpZCI6IDQ3MX0=")
print(token_bytes)  # b'{"id": 471}'

# Benchmark note: on strings under ~10 KB the function-call overhead dominates
# and the speedup is negligible. Profile before switching.

API ตั้งใจให้เหมือนกันกับ base64 — แค่เปลี่ยน import และไม่มีอะไรเปลี่ยน ใช้เฉพาะเมื่อ profiling ยืนยันว่า Base64 เป็น bottleneck จริงๆ ซึ่งพบไม่บ่อยนอก data pipeline ที่มี throughput สูง

ข้อผิดพลาดทั่วไป

ผมเห็นข้อผิดพลาดสี่อย่างนี้ใน code review ซ้ำๆ — พบบ่อยเป็นพิเศษ ในหมู่นักพัฒนาที่มาจากภาษาอย่าง JavaScript หรือ PHP ที่การถอดรหัส Base64 คืนค่า string โดยตรง หรือจาก tutorial ที่ข้ามการจัดการข้อผิดพลาดทั้งหมด

ข้อผิดพลาดที่ 1: ลืมเรียก .decode() กับผลลัพธ์

Before · Python
After · Python
# ❌ b64decode() returns bytes — this crashes downstream
import base64

raw = base64.b64decode("eyJ1c2VyX2lkIjogNDcxLCAicm9sZSI6ICJhZG1pbiJ9")

# TypeError: byte indices must be integers or slices, not str
user_id = raw["user_id"]
# ✅ decode bytes → str, then parse
import base64, json

raw = base64.b64decode("eyJ1c2VyX2lkIjogNDcxLCAicm9sZSI6ICJhZG1pbiJ9")
payload = json.loads(raw.decode("utf-8"))
print(payload["user_id"])  # 471
print(payload["role"])     # admin

ข้อผิดพลาดที่ 2: ใช้ b64decode() กับ input Base64 ที่ปลอดภัยสำหรับ URL

Before · Python
After · Python
# ❌ JWT and OAuth tokens use '-' and '_' — not in standard alphabet
import base64

jwt_segment = "eyJ1c2VyX2lkIjogMjg5M30"
# binascii.Error or silently wrong bytes — unpredictable behaviour
base64.b64decode(jwt_segment)
# ✅ use urlsafe_b64decode() for any token with '-' or '_'
import base64, json

jwt_segment = "eyJ1c2VyX2lkIjogMjg5M30"
padded = jwt_segment + "=" * (-len(jwt_segment) % 4)
data = base64.urlsafe_b64decode(padded)
print(json.loads(data.decode("utf-8")))
# {'user_id': 2893}

ข้อผิดพลาดที่ 3: ไม่แก้ไข padding ใน token ที่ถูกตัด

Before · Python
After · Python
# ❌ JWTs and most URL-transmitted tokens strip '=' — this crashes
import base64

# Valid JWT payload segment — no padding, as per spec
segment = "eyJ0eXBlIjogImFjY2VzcyIsICJqdGkiOiAiMzgxIn0"
base64.urlsafe_b64decode(segment)
# binascii.Error: Incorrect padding
# ✅ add padding before every urlsafe_b64decode() call
import base64, json

segment = "eyJ0eXBlIjogImFjY2VzcyIsICJqdGkiOiAiMzgxIn0"
padded = segment + "=" * (-len(segment) % 4)
result = json.loads(base64.urlsafe_b64decode(padded).decode("utf-8"))
print(result["type"])  # access
print(result["jti"])   # 381

ข้อผิดพลาดที่ 4: เรียก .decode("utf-8") กับข้อมูลไบนารี

Before · Python
After · Python
# ❌ Binary files (PDF, PNG, ZIP) are not UTF-8 text — this crashes
import base64

# Base64-encoded PDF starts with JVBERi... (%PDF-)
pdf_b64 = "JVBERi0xLjQKJeLjz9MKNyAwIG9iago8PC9U..."
pdf_text = base64.b64decode(pdf_b64).decode("utf-8")
# UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe2
# ✅ write binary directly to a file — no .decode() needed
import base64
from pathlib import Path

pdf_b64 = "JVBERi0xLjQKJeLjz9MKNyAwIG9iago8PC9U..."
pdf_bytes = base64.b64decode(pdf_b64)
Path("report_q1_2026.pdf").write_bytes(pdf_bytes)
print(f"Saved {len(pdf_bytes):,} bytes")

การถอดรหัสไฟล์ Base64 ขนาดใหญ่ใน Python

การโหลดไฟล์ Base64 ขนาด 200 MB ด้วย Path.read_text() และถอดรหัสในครั้งเดียว จะจัดสรร string ที่เข้ารหัส, bytes ที่ถอดรหัส และการแสดงระดับกลางใดๆ พร้อมกัน — ทำให้หน่วยความจำหมดได้ง่ายบน server ที่มีข้อจำกัดหรือ Lambda function สำหรับไฟล์ที่ใหญ่กว่า ~50–100 MB ให้ใช้วิธี chunked แทน

การถอดรหัสแบบ chunk ลงดิสก์ (ไม่โหลดไฟล์ทั้งหมดเข้า RAM)

Python 3.8+
import base64

def decode_large_b64_file(input_path: str, output_path: str, chunk_size: int = 65536) -> None:
    """
    Decode a large Base64 file in chunks to avoid loading the entire encoded
    string into memory. chunk_size must be a multiple of 4 to keep Base64
    block boundaries aligned across reads.
    """
    assert chunk_size % 4 == 0, "chunk_size must be a multiple of 4"

    bytes_written = 0
    with open(input_path, "rb") as src, open(output_path, "wb") as dst:
        while True:
            chunk = src.read(chunk_size)
            if not chunk:
                break
            # Strip whitespace that may appear in wrapped/multiline Base64
            chunk = chunk.strip()
            if chunk:
                dst.write(base64.b64decode(chunk))
                bytes_written += len(chunk)

    print(f"Decoded {bytes_written:,} Base64 bytes → {output_path}")

# Example: decode a 300 MB database snapshot stored as Base64
decode_large_b64_file("snapshot_2026_03_13.b64", "snapshot_2026_03_13.sql.gz")

การถอดรหัส Base64 ด้วย base64.decodebytes() สำหรับข้อมูล PEM / หลายบรรทัด

Python 3.8+
import base64

# base64.decodebytes() is designed for MIME / PEM Base64 that wraps at 76 chars.
# It silently ignores whitespace and newlines — perfect for certificate files.

with open("server_cert.pem", "rb") as f:
    pem_data = f.read()

# Strip PEM headers if present, then decode
lines = [
    line for line in pem_data.splitlines()
    if not line.startswith(b"-----")
]
raw_cert = base64.decodebytes(b"
".join(lines))
print(f"Certificate DER payload: {len(raw_cert):,} bytes")
หมายเหตุ:ใช้ base64.decodebytes() สำหรับ PEM certificate, MIME attachment และ Base64 ใดๆ ที่ขึ้นบรรทัดใหม่ที่ความกว้างคงที่ ใช้วิธี chunked ข้างบนสำหรับ blob ขนาดใหญ่ที่ไม่โปร่งใส (backup, ไฟล์ media) สำหรับ token บรรทัดเดียวที่กระชับ (JWT, OAuth) b64decode() หรือ urlsafe_b64decode() เป็นตัวเลือกที่ถูกต้องเสมอ

วิธีการถอดรหัส Base64 ใน Python — เปรียบเทียบอย่างรวดเร็ว

วิธีการตัวอักษรPaddingOutputต้องติดตั้งเหมาะสำหรับ
base64.b64decode()มาตรฐาน (A–Z a–z 0–9 +/)จำเป็นbytesไม่ (stdlib)ทั่วไป, email, PEM
base64.decodebytes()มาตรฐาน (A–Z a–z 0–9 +/)ละเว้น (ลบช่องว่าง)bytesไม่ (stdlib)PEM cert, MIME attachment, Base64 หลายบรรทัด
base64.urlsafe_b64decode()URL-safe (A–Z a–z 0–9 -_)จำเป็นbytesไม่ (stdlib)JWT, OAuth, Google Cloud API
base64.b32decode()32 ตัวอักษร (A–Z, 2–7)จำเป็นbytesไม่ (stdlib)TOTP secret, DNS-safe ID
base64.b16decode()Hex (0–9, A–F)ไม่มีbytesไม่ (stdlib)checksum และ hash แบบ hex
pybase64.b64decode()มาตรฐาน (A–Z a–z 0–9 +/)จำเป็นbytesใช่ (pip)pipeline ที่ต้องการ throughput สูง, payload ขนาดใหญ่
CLI: base64 --decodeมาตรฐานอัตโนมัติstdoutไม่ (ระบบ)ตรวจสอบบน terminal อย่างรวดเร็ว

ใช้ b64decode() เป็นค่าเริ่มต้น เปลี่ยนเป็น urlsafe_b64decode()ทันทีที่คุณเห็น - หรือ _ ใน input — ตัวอักษรเหล่านั้นคือ สัญญาณที่ชัดเจนของ Base64 ที่ปลอดภัยสำหรับ URL ใช้ pybase64 เฉพาะหลังจาก profiling ยืนยัน bottleneck สำหรับการตรวจสอบครั้งเดียวระหว่างการพัฒนา เครื่องมือถอดรหัส Base64 ของ ToolDeck รองรับ ทั้งสองตัวอักษรและซ่อมแซม padding อัตโนมัติ — ไม่ต้องมี Python environment

คำถามที่พบบ่อย

จะถอดรหัส Base64 string เป็น string ปกติใน Python ได้อย่างไร?

เรียก base64.b64decode(encoded) เพื่อรับ bytes จากนั้นเรียก .decode("utf-8")กับผลลัพธ์เพื่อรับ Python strสองขั้นตอนนี้แยกกันเสมอเพราะ b64decode() แค่ย้อนกลับตัวอักษร Base64 — มันไม่รู้ว่าเนื้อหาต้นฉบับเป็น UTF-8, Latin-1 หรือไบนารี หากข้อมูลใช้ encoding ที่ไม่ใช่ UTF-8 ให้ส่งชื่อ codec ที่ถูกต้องให้ .decode()ตัวอย่างเช่น .decode("latin-1")

ทำไมถึงเกิด "Incorrect padding" เมื่อถอดรหัส Base64 ใน Python?

Base64 string ต้องมีความยาวเป็นจำนวนคูณของ 4 ตัวอักษร JWT, OAuth token และข้อมูล ที่ส่งผ่าน URL มักจะตัด padding = ท้ายออก แก้ไขโดยเพิ่ม "=" * (-len(s) % 4) ก่อนถอดรหัส สูตรนี้เพิ่มได้พอดี 0, 1 หรือ 2 ตัวอักษรตามที่จำเป็น และเป็น no-op ที่ปลอดภัยเมื่อ string มี padding ถูกต้องแล้ว

ความแตกต่างระหว่าง b64decode() และ urlsafe_b64decode() ใน Python คืออะไร?

ทั้งคู่ถอดรหัสด้วย algorithm Base64 เดียวกันแต่ใช้ตัวอักษรต่างกันสำหรับอักขระที่ 62 และ 63b64decode() ใช้ + และ /; urlsafe_b64decode() ใช้ - และ _ตัวแปรที่ปลอดภัยสำหรับ URL ถูกกำหนดใน RFC 4648 §5 และใช้เมื่อ Base64 ต้องอยู่รอด ใน URL, HTTP header หรือค่า cookie โดยไม่ต้องมี percent-encoding การสับสนกันทำให้เกิด binascii.Error หรือ output ที่เสียหายอย่างเงียบๆ

จะถอดรหัสรูปภาพที่เข้ารหัส Base64 ใน Python ได้อย่างไร?

ถอดรหัสเป็น bytes ด้วย base64.b64decode(encoded) จากนั้นเขียน bytes เหล่านั้น โดยตรงลงไฟล์ — อย่าเรียก .decode("utf-8") กับข้อมูลรูปภาพ หาก input เป็น data URL (เช่น data:image/png;base64,iVBORw0KGgo…) ให้ลบ prefix ก่อน:

Python 3.8+
import base64
from pathlib import Path

# Data URL from an <img src="..."> or an API response
data_url = (
    "data:image/png;base64,"
    "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAC0lEQVQI12NgAAIABQ=="
)

# Split off the "data:image/png;base64," prefix
_, encoded = data_url.split(",", 1)
image_bytes = base64.b64decode(encoded)
Path("avatar_jsmith.png").write_bytes(image_bytes)
print(f"Saved {len(image_bytes)} bytes")

สามารถถอดรหัส Base64 ใน Python โดยไม่ import module ได้ไหม?

ทางเทคนิคได้ แต่ไม่มีเหตุผลที่จะทำเช่นนั้น โมดูล base64 เป็นส่วนหนึ่งของ standard library ของ Python พร้อมใช้งานเสมอ ติดตั้งเสมอ — ไม่มี dependency และฟังก์ชันถูก implement ใน C การ implement Base64 ใหม่ตั้งแต่ต้นจะ ช้ากว่า, เกิดข้อผิดพลาดได้ง่ายกว่า และดูแลรักษายากกว่า ให้ใช้ import base64 เสมอ

จะถอดรหัส Base64 ใน Python เมื่อ input เป็น bytes ไม่ใช่ string ได้อย่างไร?

base64.b64decode() รับ str, bytes และ bytearray แทนกันได้ — ไม่ต้องแปลง หากคุณได้รับ b"SGVsbG8=" จาก socket หรือการอ่านไฟล์ ส่งผ่านโดยตรง การซ่อมแซม padding ทำงานเหมือนกันในโหมด bytes: data + b"=" * (-len(data) % 4) เมื่อทำงานในโหมด bytes

เครื่องมือที่เกี่ยวข้อง

  • เข้ารหัส Base64 — เข้ารหัสข้อความหรือไฟล์ไบนารีเป็น Base64 ทันที; มีประโยชน์สำหรับสร้าง test fixture สำหรับโค้ดถอดรหัส Python โดยไม่ต้องรัน script
  • ถอดรหัส JWT — ตรวจสอบ JWT header และ payload โดยไม่ต้องเขียนโค้ด; payload ถูกถอดรหัสด้วย Base64 ที่ปลอดภัยสำหรับ URL ภายใต้ hood เหมือนที่แสดงในตัวอย่างข้างบน
  • ถอดรหัส URL — percent-decode query string และ path segment; มักต้องใช้ควบคู่กับการถอดรหัส Base64 เมื่อ parse OAuth callback URL หรือ webhook payload
  • เข้ารหัส URL — percent-encode ตัวอักษรพิเศษ; มีประโยชน์เมื่อต้องฝังค่าที่เข้ารหัส Base64 อย่างปลอดภัยใน URL query parameter
มีให้ในภาษาอื่นด้วย:JavaScriptGoJavaC#
MS
Maria SantosBackend Developer

Maria is a backend developer specialising in Python and API integration. She has broad experience with data pipelines, serialisation formats, and building reliable server-side services. She is an active member of the Python community and enjoys writing practical, example-driven guides that help developers solve real problems without unnecessary theory.

DV
Dmitri Volkovผู้ตรวจสอบทางเทคนิค

Dmitri is a DevOps engineer who relies on Python as his primary scripting and automation language. He builds internal tooling, CI/CD pipelines, and infrastructure automation scripts that run in production across distributed teams. He writes about the Python standard library, subprocess management, file processing, encoding utilities, and the practical shell-adjacent Python that DevOps engineers use every day.