Cryptography (ทฤษฎีการเข้ารหัส) คืออะไร?
Cryptography เป็นวิทยาศาสตร์ของการ ปกป้องข้อมูล โดยการแปลงข้อมูลที่อ่านได้ (Plaintext) ให้กลายเป็นข้อมูลที่อ่านไม่ได้ (Ciphertext) เพื่อให้เฉพาะฝ่ายที่มีคีย์ (Key) เท่านั้นที่สามารถกลับมาอ่านได้ (Decrypt) เป้าหมายหลักคือ:
- Confidentiality (ความลับ): ป้องกันการอ่านโดยไม่ได้รับอนุญาต
- Integrity (ความสมบูรณ์): รับรองข้อมูลไม่ถูกแก้ไขระหว่างทาง
- Authentication (การยืนยันตัวตน): ยืนยันตัวตนผู้ส่ง/ผู้รับ
- Non-repudiation (ไม่ thểปฏิเสธ): ไม่สามารถปฏิเสธการกระทำที่ทำไปแล้ว
ประเภทหลักของ Cryptography
1. Symmetric Encryption (เข้ารหัสแบบสมมาตร)
- ใช้ คีย์เดียวกัน สำหรับ Encrypt และ Decrypt
- เร็ว เหมาะกับข้อมูลปริมาณมาก
- อัลกอริธึมยอดนิยม: AES-256 (มาตรฐานปัจจุบัน), ChaCha20, DES/3DES (ล้าสมัย)
- ปัญหา: Key Distribution Problem - ต้องส่งคีย์ให้ฝ่ายรับอย่างปลอดภัย
2. Asymmetric Encryption / Public Key Cryptography (เข้ารหัสแบบอสมมาตร)
- ใช้ คู่คีย์ (Key Pair):
- Public Key: แจกจ่ายสาธารณะ ใช้ Encrypt / Verify Signature
- Private Key: เก็บไว้ลับ ใช้ Decrypt / Sign
- ช้ากว่า Symmetric ใช้กับข้อมูลเล็ก หรือ Key Exchange
- อัลกอริธึมยอดนิยม: RSA (2048/4096-bit), ECC (Elliptic Curve - P-256, P-384, Curve25519), EdDSA
- ใช้ใน: TLS/SSL, SSH, PGP, Blockchain, Digital Signature
3. Hash Functions (ฟังก์ชันแฮช)
- แปลงข้อมูลความยาวเท่าไรก็ได้ → Fixed-length Digest (Hash)
- One-way: ไม่สามารถ Reverse ได้
- คุณสมบัติ: Deterministic, Fast, Avalanche Effect, Collision Resistance
- อัลกอริธึม: SHA-256, SHA-3, BLAKE2/3 (หลีกเลี่ยง MD5, SHA-1)
- ใช้ใน: Password Storage (กับ Salt), Digital Signature, Blockchain, Integrity Check
การประยุกต์ใช้ในชีวิตจริง (Real-world Applications)
| การใช้งาน | Cryptography ที่ใช้ |
|---|---|
| HTTPS/TLS (เว็บไซต์) | RSA/ECC (Key Exchange) + AES-GCM (Data Encryption) + SHA-256 (Integrity) |
| Email Encryption (PGP/S/MIME) | RSA/ECC + AES + SHA |
| VPN (WireGuard, OpenVPN, IPsec) | ChaCha20-Poly1305 / AES-GCM + Curve25519 / ECDH |
| Password Storage | Argon2id, bcrypt, scrypt, PBKDF2 (Key Derivation Functions) |
| Digital Signature (Code Signing, Document) | RSA-PSS, ECDSA, EdDSA |
| Blockchain/Cryptocurrency | ECDSA (secp256k1), EdDSA, SHA-256, Keccak-256 |
| ATM/Chip Card (EMV) | 3DES (Legacy), AES, RSA, ECC |
| GSM/4G/5G | AES, SNOW 3G, ZUC, HMAC-SHA-256 |
Key Management (การจัดการคีย์) - หัวใจสำคัญ
- Key Generation: ใช้ CSPRNG (Cryptographically Secure PRNG)
- Key Storage: HSM (Hardware Security Module), TPM, Secure Enclave, KMS (AWS KMS, HashiCorp Vault)
- Key Rotation: หมุนเวียนคีย์เป็นระยะ (NIST แนะนำ 1-2 ปี)
- Key Revocation: ระงับคีย์ที่ถูก Compromise (CRL, OCSP)
- Key Backup/Escrow: สำหรับ Disaster Recovery
มาตรฐานและแนวทางปฏิบัติ
- NIST SP 800-57: Key Management Guidelines
- FIPS 140-2/3: มาตรฐานโมดูลเข้ารหัสฮาร์ดแวร์
- PCI-DSS: การจัดการคีย์การ์ดเครดิต
- Post-Quantum Cryptography (PQC): NIST กำลังมาตรฐาน化 CRYSTALS-Kyber (KEM), CRYSTALS-Dilithium (Signature), SPHINCS+, FALCON
ข้อผิดพลาดที่พบบ่อย (Crypto Pitfalls)
- สร้าง Crypto เอง (Don't Roll Your Own Crypto)
- ใช้ ECB Mode (ไม่ปลอดภัยสำหรับข้อมูลมากกว่า 1 Block)
- Hardcode Key/IV ใน Code
- ใช้ Random แบบไม่ปลอดภัย (Math.random, timestamp)
- ไม่ Verify Signature/Integrity ก่อน Decrypt
- ใช้ Algorithm ที่ล้าสมัย (MD5, SHA-1, DES, RSA < 2048-bit)