PHP EnCode/DeCode คือกระบวนการแปลงข้อมูลให้อยู่ในรูปแบบหนึ่งและแปลงกลับคืน เพื่อวัตถุประสงค์ที่แตกต่างกันไป โดยมีฟังก์ชันในภาษา PHP ที่ช่วยในการทำงานนี้
เป็นการแปลงข้อมูลให้อยู่ในรูปแบบอื่นที่ไม่ใช่รูปแบบเดิมที่อ่านเข้าใจได้ง่าย เพื่อเหตุผลบางอย่าง เช่น
เป็นการแปลงข้อมูลที่ถูกเข้ารหัสแล้วให้กลับมาอยู่ในรูปแบบเดิม เพื่อให้สามารถนำกลับไปใช้งานได้ตามปกติ
| ประเภท | Function Encode | Function Decode | การถอดรหัส | ความปลอดภัย | การใช้งานทั่วไป |
|---|---|---|---|---|---|
| Base64 | base64_encode() | base64_decode() | ✅ | ต่ำ | แปลงข้อมูลส่งผ่านเว็บ |
| JSON | json_encode() | json_decode() | ✅ | ต่ำ | ส่งข้อมูลใน URL |
| JSON | json_encode() | json_decode() | ✅ | ปลอดภัยเชิงโครงสร้าง | แลกเปลี่ยนข้อมูล |
| Hash | md5(), sha1(), hash() | ❌ | ❌ | กลาง-สูง | ตรวจสอบ / รหัสผ่าน |
| Password Hash | password_hash() | password_verify() | ❌ | สูง | เข้ารหัสรหัสผ่าน |
| OpenSSL | openssl_encrypt() | openssl_decrypt() | ✅ | สูง | เข้ารหัสข้อมูลสำคัญ |
| Serialize | serialize() | unserialize() | ✅ | ต่ำ | จัดเก็บข้อมูล PHP |
| XOR / Custom | เขียนเอง | ✅ | ✅ | ต่ำ | ใช้เพื่อศึกษาเท่านั้น |
Base64 การเข้ารหัสแบบทั่วไป (ไม่ปลอดภัยทางการเข้ารหัส แต่ใช้บ่อย)
<?phpตัวอย่างรูปแบบ Base64
<?php
$text = "Hello World!";
$encoded = base64_encode($text);
$decoded = base64_decode($encoded);
echo "Encoded: $encoded";
echo "Decoded: $decoded";
?>
ตัวอย่างรูปแบบ URL
<?php
$data = "Hello World & PHP!";
$encoded = urlencode($data);
$decoded = urldecode($encoded);
echo "Encoded: $encoded";
echo "Decoded: $decoded";
?>
URL ใช้เข้ารหัสข้อมูลก่อนส่งผ่าน URL เช่น ?q=Hello%20World
<?phpJSON ใช้แปลง Array ⇄ JSON เพื่อส่งข้อมูลระหว่าง Frontend/Backend
<?phpตัวอย่างรูปแบบ JSON
<?php
$arr = ["name" => "Dex", "age" => 25];
$json = json_encode($arr);
$decoded = json_decode($json, true);
echo "JSON: $json";
print_r($decoded);
?>
ตัวอย่างรูปแบบ MD5
<?php
$hash = md5("mypassword");
echo $hash;
?>
MD5 ใช้สำหรับรหัสผ่านหรือข้อมูลที่ไม่ต้องถอดกลับ จจุบันไม่ปลอดภัยมากนัก ใช้ได้เฉพาะตรวจสอบเบื้องต้น
<?phpSHA1 / SHA256 / SHA512 ใช้สำหรับตรวจสอบความถูกต้องของไฟล์หรือข้อมูล
<?phpตัวอย่างรูปแบบ JSON
<?php
$sha1 = sha1("mypassword");
$sha256 = hash('sha256', "mypassword");
$sha512 = hash('sha512', "mypassword");
echo $sha256;
?>
ตัวอย่างรูปแบบ Password Hash
<?php
$pass = "mypassword";
$hash = password_hash($pass, PASSWORD_DEFAULT);
if (password_verify("mypassword", $hash)) {
echo "Password correct!";
}
?>
Password Hash ปลอดภัยที่สุดสำหรับรหัสผ่าน — มี salt + algorithm อัตโนมัติ
<?phpSymmetric การเข้ารหัสและถอดรหัสแบบ Symmetric (เข้ารหัสและถอดได้) ใช้ algorithm เช่น AES, DES, Blowfish * OpenSSL (แนะนำ) *
<?phpตัวอย่างรูปแบบ JSON
<?php
$data = "Secret Message";
$key = "MyStrongKey1234";
$method = "AES-256-CBC";
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length($method));
// เข้ารหัส
$encrypted = openssl_encrypt($data, $method, $key, 0, $iv);
$encrypted_data = base64_encode($iv . $encrypted);
// ถอดรหัส
$decoded = base64_decode($encrypted_data);
$iv_dec = substr($decoded, 0, openssl_cipher_iv_length($method));
$ciphertext = substr($decoded, openssl_cipher_iv_length($method));
$decrypted = openssl_decrypt($ciphertext, $method, $key, 0, $iv_dec);
echo "Encrypted: $encrypted_data";
echo "Decrypted: $decrypted";
?>
ตัวอย่างรูปแบบ serialize() / unserialize()
<?php
$arr = ["name" => "Dex", "score" => 100];
$encoded = serialize($arr);
$decoded = unserialize($encoded);
echo "Serialized: $encoded";
print_r($decoded);
?>
การเข้ารหัสด้วยฟังก์ชัน serialize() / unserialize() ใช้แปลง array / object เป็น string เพื่อจัดเก็บ
<?phpการเข้ารหัสแบบ Custom (เช่น Caesar Cipher / XOR) ตัวอย่าง XOR Encryption (เบา ๆ ใช้ศึกษาการเข้ารหัส)
<?phpตัวอย่างรูปแบบ JSON
<?php
function xor_encrypt($data, $key) {
$out = '';
for ($i = 0; $i < strlen($data); $i++) {
$out .= chr(ord($data[$i]) ^ ord($key[$i % strlen($key)]));
}
return $out;
}
$plaintext = "Hello PHP";
$key = "key123";
$encoded = base64_encode(xor_encrypt($plaintext, $key));
$decoded = xor_encrypt(base64_decode($encoded), $key);
echo "Encoded: $encoded";
echo "Decoded: $decoded";
?>