PHP EnCode/DeCode


PHP EnCode/DeCode คือกระบวนการแปลงข้อมูลให้อยู่ในรูปแบบหนึ่งและแปลงกลับคืน เพื่อวัตถุประสงค์ที่แตกต่างกันไป โดยมีฟังก์ชันในภาษา PHP ที่ช่วยในการทำงานนี้

PHP

การเข้ารหัส (Encode)

           เป็นการแปลงข้อมูลให้อยู่ในรูปแบบอื่นที่ไม่ใช่รูปแบบเดิมที่อ่านเข้าใจได้ง่าย เพื่อเหตุผลบางอย่าง เช่น

  • ความปลอดภัย: เพื่อไม่ให้บุคคลภายนอกเข้าถึงข้อมูลสำคัญได้โดยตรง เช่น การเก็บรหัสผ่านในฐานข้อมูล
  • ความเข้ากันได้: เพื่อให้ข้อมูลสามารถส่งผ่านเครือข่ายหรือเก็บไว้ในระบบบางอย่างได้อย่างถูกต้อง เช่น การเข้ารหัส URL ให้สามารถส่งผ่านเว็บเบราว์เซอร์ได้
  • ลดขนาดข้อมูล: เพื่อบีบอัดข้อมูลให้มีขนาดเล็กลง

การถอดรหัส (Decode)

           เป็นการแปลงข้อมูลที่ถูกเข้ารหัสแล้วให้กลับมาอยู่ในรูปแบบเดิม เพื่อให้สามารถนำกลับไปใช้งานได้ตามปกติ


ประเภท 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 เขียนเอง ต่ำ ใช้เพื่อศึกษาเท่านั้น
PHP EnCode/DeCode

Base64

Base64          การเข้ารหัสแบบทั่วไป (ไม่ปลอดภัยทางการเข้ารหัส แต่ใช้บ่อย)

<?php
           $text = "Hello World!";
           $encoded = base64_encode($text);
           $decoded = base64_decode($encoded);
           echo "Encoded: $encoded";
           echo "Decoded: $decoded";
?>
Encoded: SGVsbG8gV29ybGQh
Decoded: Hello World!

ตัวอย่างรูปแบบ Base64


<?php 
    $text = "Hello World!";
    $encoded = base64_encode($text);
    $decoded = base64_decode($encoded);

    echo "Encoded: $encoded";
    echo "Decoded: $decoded";
?>
        
PHP EnCode/DeCode

URL

ตัวอย่างรูปแบบ URL


<?php 
    $data = "Hello World & PHP!";
    $encoded = urlencode($data);
    $decoded = urldecode($encoded);

    echo "Encoded: $encoded";
    echo "Decoded: $decoded";
?>

URL            ใช้เข้ารหัสข้อมูลก่อนส่งผ่าน URL เช่น ?q=Hello%20World

<?php
           $data = "Hello World & PHP!";
           $encoded = urlencode($data);
           $decoded = urldecode($encoded);
           echo "Encoded: $encoded";
           echo "Decoded: $decoded";
?>
Encoded: Hello+World+%26+PHP%21
Decoded: Hello World & PHP!
PHP EnCode/DeCode

JSON

JSON            ใช้แปลง Array ⇄ JSON เพื่อส่งข้อมูลระหว่าง Frontend/Backend

<?php
           $arr = ["name" => "Dex", "age" => 25];
           $json = json_encode($arr);
           $decoded = json_decode($json, true);
                      echo "JSON: $json";
           print_r($decoded);
?>
JSON: {"name":"Dex","age":25}
Array ( [name] => Dex [age] => 25 )

ตัวอย่างรูปแบบ JSON


                        
<?php $arr = ["name" => "Dex", "age" => 25]; $json = json_encode($arr); $decoded = json_decode($json, true); echo "JSON: $json"; print_r($decoded); ?>
PHP EnCode/DeCode

MD5

ตัวอย่างรูปแบบ MD5


<?php
$hash = md5("mypassword"); echo $hash; ?>

MD5            ใช้สำหรับรหัสผ่านหรือข้อมูลที่ไม่ต้องถอดกลับ จจุบันไม่ปลอดภัยมากนัก ใช้ได้เฉพาะตรวจสอบเบื้องต้น

<?php
           $hash = md5("mypassword");
           echo $hash;
?>
34819d7beeabb9260a5c854bc85b3e44
PHP EnCode/DeCode

SHA1 / SHA256 / SHA512

SHA1 / SHA256 / SHA512            ใช้สำหรับตรวจสอบความถูกต้องของไฟล์หรือข้อมูล

<?php
           $sha1 = sha1("mypassword");
           $sha256 = hash('sha256', "mypassword");
           $sha512 = hash('sha512', "mypassword");
           echo $sha1;
           echo $sha256;
           echo $sha512;
?>
sha1 : 91dfd9ddb4198affc5c194cd8ce6d338fde470e2
sha256 : 89e01536ac207279409d4de1e5253e01f4a1769e696db0d6062ca9b8f56767c8
sha512 : a336f671080fbf4f2a230f313560ddf0d0c12dfcf1741e49e8722a234673037dc493caa8d291d8025f71089d63cea809cc8ae53e5b17054806837dbe4099c4ca

ตัวอย่างรูปแบบ JSON


                        
<?php $sha1 = sha1("mypassword"); $sha256 = hash('sha256', "mypassword"); $sha512 = hash('sha512', "mypassword"); echo $sha256; ?>
PHP EnCode/DeCode

Password Hash

ตัวอย่างรูปแบบ Password Hash


<?php
$pass = "mypassword"; $hash = password_hash($pass, PASSWORD_DEFAULT); if (password_verify("mypassword", $hash)) { echo "Password correct!"; } ?>

Password Hash            ปลอดภัยที่สุดสำหรับรหัสผ่าน — มี salt + algorithm อัตโนมัติ

<?php
           $pass = "mypassword";
           $hash = password_hash($pass, PASSWORD_DEFAULT);
           if (password_verify("mypassword", $hash)) {
                      echo "Password correct!";
           }
?>
Password correct!
PHP EnCode/DeCode

Symmetric

Symmetric            การเข้ารหัสและถอดรหัสแบบ Symmetric (เข้ารหัสและถอดได้) ใช้ algorithm เช่น AES, DES, Blowfish * OpenSSL (แนะนำ) *

<?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";
?>
Encrypted: Uw3ymLEC/FqnAYy0AsJKmG5GK3BUczdqbFhaSFhzNitQSDl2bGc9PQ==
Decrypted: Secret Message

ตัวอย่างรูปแบบ 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"; ?>
PHP EnCode/DeCode

serialize() / unserialize()

ตัวอย่างรูปแบบ 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
           $arr = ["name" => "Dex", "score" => 100];
           $encoded = serialize($arr);
           $decoded = unserialize($encoded);
           echo "Serialized: $encoded";
           print_r($decoded);
?>
Serialized: a:2:{s:4:"name";s:3:"Dex";s:5:"score";i:100;}
Array ( [name] => Dex [score] => 100 )
PHP EnCode/DeCode

Custom

การเข้ารหัสแบบ Custom (เช่น Caesar Cipher / XOR)            ตัวอย่าง XOR Encryption (เบา ๆ ใช้ศึกษาการเข้ารหัส)

<?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";
?>
Encoded: IwAVXV0TOy0p
Decoded: Hello 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"; ?>