ภาพรวม
ตัวอย่างนี้สาธิตวิธี เข้ารหัสสตริง (Encode) ใน Android โดยใช้ Base64 คลาสมาตรฐานของ Android SDK เหมาะสำหรับส่งข้อความผ่านระบบที่รองรับเฉพาะ ASCII
ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่
เปิด Android Studio ไปที่ File → New Project กรอกรายละเอียดที่จำเป็นทั้งหมดเพื่อสร้างโปรเจกต์ใหม่
ขั้นตอนที่ 2: จัดทำ Layout (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_marginTop="30dp"
tools:context=".MainActivity">
<EditText
android:id="@+id/edit_query"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter text to encode" />
<Button
android:id="@+id/buttonPanel"
android:text="Encode"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_marginTop="20dp" />
</LinearLayout>
ขั้นตอนที่ 3: เขียนโค้ดหลัก (src/MainActivity.java)
package com.example.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Base64;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView textView;
EditText editQuery;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editQuery = findViewById(R.id.edit_query);
textView = findViewById(R.id.text);
findViewById(R.id.buttonPanel).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String input = editQuery.getText().toString();
if (!input.isEmpty()) {
// Encode ด้วย Base64
String encoded = Base64.encodeToString(
input.getBytes(),
Base64.DEFAULT
);
textView.setText("Encoded: " + encoded);
} else {
textView.setText("Please enter text");
}
}
});
}
}
การทดสอบ
รันแอป ป้อนข้อความ "Hello World" คลิก Encode จะได้ "SGVsbG8gV29ybGQ="
Base64 Flags ใน Android
| Flag | คำอธิบาย | ตัวอย่าง Output |
|---|---|---|
Base64.DEFAULT | มาตรฐาน มี Line Breaks ทุก 76 ตัวอักษร | SGVsbG8gV29ybGQ= |
Base64.NO_WRAP | ไม่มี Line Breaks (แนะนำสำหรับ Network/JSON) | SGVsbG8gV29ybGQ= |
Base64.URL_SAFE | ใช้ - และ _ แทน + และ / (ปลอดภัยสำหรับ URL) | SGVsbG8gV29ybGQ= |
Base64.NO_PADDING | ตัด = ท้ายออก (ต้อง Decode ด้วย Flag เดียวกัน) | SGVsbG8gV29ybGQ |
การรวม Flag (Bitwise OR)
// แนะนำสำหรับการใช้งานทั่วไป: ไม่มี Line Break + URL Safe
String encoded = Base64.encodeToString(
input.getBytes(),
Base64.NO_WRAP | Base64.URL_SAFE
);
การถอดรหัส (Decode) - ดูรายละเอียดในบทความถัดไป
byte[] decodedBytes = Base64.decode(encodedString, Base64.DEFAULT); String decoded = new String(decodedBytes, StandardCharsets.UTF_8);
กรณีใช้งาน Base64 Encoding
- ส่งข้อมูล Binary (รูปภาพ, ไฟล์) ผ่าน JSON/XML
- เก็บข้อมูลใน SharedPreferences / Database ที่รองรับ String เท่านั้น
- Basic Authentication Header (username:password → Base64)
- Data URI สำหรับรูปภาพเล็กใน HTML/CSS
- JWT Token (Header.Payload.Signature ทุกส่วนเป็น Base64URL)
ข้อควรระวัง
- Base64 เพิ่มขนาดข้อมูลประมาณ 33%
- ไม่ใช่การเข้ารหัสทางเข้ารหัส (Encryption) - ใครก็ถอดรหัสได้
- ใช้ UTF-8 Encoding สำหรับ getBytes() เพื่อรองรับภาษาไทย
- Android 8.0+ มี
java.util.Base64ด้วย (API 26+)