Dev / IT6 min read

Base64 คืออะไร? Developer ไทยควรรู้อะไรบ้าง

อธิบาย Base64 แบบเข้าใจง่ายสำหรับ developer ไทย ครอบคลุมการทำงาน ความแตกต่างจาก Encryption เมื่อไหรควรใช้ Base64URL การใช้ใน JWT พร้อมตัวอย่างโค้ด JavaScript

Base64 คือการเข้ารหัสข้อมูลที่ Developer เจอบ่อยมาก — ตั้งแต่ JWT Token, รูปภาพใน HTML, ไปจนถึง Basic Auth แต่หลายคนยังสับสนว่ามันคืออะไร ต่างจาก Encryption อย่างไร และใช้ตอนไหน บทความนี้อธิบายให้เข้าใจง่าย พร้อมตัวอย่างที่ใช้งานได้จริง

Base64 คืออะไร?

Base64 คือวิธีการแปลง ข้อมูล binary ให้เป็นข้อความ ASCII โดยใช้ตัวอักษร 64 ตัว ได้แก่ A-Z, a-z, 0-9, +, / (และ = สำหรับ padding)

⚡ สำคัญ: Base64 ไม่ใช่การเข้ารหัส (Encryption)

ใครก็ decode ได้โดยไม่ต้องใช้ key — เป็นแค่การแปลงรูปแบบข้อมูล ไม่ใช่การซ่อนข้อมูล

ทำไมต้องมี Base64?

ปัญหาคือระบบส่งข้อมูลบางอย่าง เช่น Email (SMTP), URL, หรือ JSON ออกแบบมาให้รับได้เฉพาะ ข้อความ textแต่ข้อมูลจริงๆ อย่างรูปภาพ, ไฟล์, หรือ binary อาจมีตัวอักษรพิเศษที่ทำให้ระบบพัง

Base64 แก้ปัญหานี้โดยแปลง binary ให้เป็น text ที่ปลอดภัยในทุกระบบ

Email Attachments

SMTP ส่งได้แค่ text — ไฟล์แนบทุกอย่างถูกแปลงเป็น Base64 ก่อนส่ง

JWT Token

Header และ Payload ของ JWT คือ JSON ที่ถูก Base64URL encode

รูปภาพใน CSS/HTML

data:image/png;base64,... ฝังรูปโดยตรงโดยไม่ต้องมีไฟล์แยก

Basic Authentication

HTTP header Authorization: Basic <base64(username:password)>

API รับ binary data

ส่งไฟล์ผ่าน JSON โดย encode เป็น Base64 string

ตัวอย่างใน JavaScript

// Browser (built-in)
const encoded = btoa('Hello World')     // "SGVsbG8gV29ybGQ="
const decoded = atob('SGVsbG8gV29ybGQ=') // "Hello World"

// Node.js
const encoded = Buffer.from('Hello').toString('base64')
const decoded = Buffer.from('SGVsbG8=', 'base64').toString('utf8')

// ภาษาไทย — ต้อง encode URI ก่อน (btoa ไม่รองรับ Unicode โดยตรง)
const thai = 'สวัสดี'
const encoded = btoa(encodeURIComponent(thai))
const decoded = decodeURIComponent(atob(encoded))

Base64 vs Base64URL

Base64 ปกติมีปัญหาเมื่อใช้ใน URL เพราะ + และ / มีความหมายพิเศษ Base64URL จึงแทนที่:

รูปแบบ+ แทนด้วย/ แทนด้วยใช้กับ
Base64+/Email, ไฟล์ทั่วไป
Base64URL-_JWT, OAuth token, URL parameter

ข้อมูลจะใหญ่ขึ้นเท่าไหร่?

Base64 เพิ่มขนาดข้อมูลประมาณ 33% — ทุก 3 bytes กลายเป็น 4 ตัวอักษร ดังนั้นควรพิจารณาก่อนฝังรูปขนาดใหญ่เป็น Base64 ใน HTML หรือ CSS

1 KB binary → ~1.37 KB Base64

1 MB binary → ~1.37 MB Base64

รูปแบบ: ceil(n / 3) × 4 ตัวอักษร (รวม padding)

เมื่อไหร่ควรใช้ Base64?

✓ ควรใช้

  • ส่ง binary ผ่าน text protocol (email, JSON)
  • ฝัง icon เล็กๆ ใน CSS/HTML
  • encode JWT payload
  • Basic Auth header

✗ ไม่ควรใช้

  • ซ่อน password หรือข้อมูลสำคัญ
  • ฝังรูปใหญ่ใน HTML (ใช้ CDN แทน)
  • แทน HTTPS สำหรับความปลอดภัย
  • compress ข้อมูล (ขนาดใหญ่ขึ้น)

TRY THE FREE TOOL

Base64 Encode / Decode

Encode or decode Base64 strings instantly

Open Tool →
N

Nattapon Tonapan

Developer & creator of FreeUtil. Building free tools for developers and Thai users.

About the author →

RELATED ARTICLES

Dev / IT5 min read

What Is Base64 Encoding? When and Why to Use It

Dev / IT6 min read

What is JWT? Understanding JSON Web Tokens

Dev / IT5 min read

Base64 Encoding Explained: What It Is and When to Use It

← Back to all articles