Public/Private Key Pair คืออะไร?
Public/Private Key Pair เป็นคู่กุญแจเข้ารหัสแบบ Asymmetric Cryptography (Public Key Cryptography):
- Public Key: แจกจ่ายได้อย่างอิสระ ใช้สำหรับ Encrypt ข้อมูล หรือ Verify Signature
- Private Key: เก็บเป็นความลับ ใช้สำหรับ Decrypt ข้อมูล หรือ Sign ข้อมูล
คุณสมบัติ: ข้อมูลที่ Encrypt ด้วย Public Key → Decrypt ได้เฉพาะ Private Key ที่คู่กัน (และในทางกลับกันสำหรับ Signature)
อัลกอริทึมยอดนิยม
| Algorithm | Key Size | ใช้งาน | สถานะ |
| RSA | 2048, 3072, 4096 bits | SSH, TLS, Email Encryption, Code Signing | มาตรฐาน, รองรับทุกที่ |
| Ed25519 | 256 bits | SSH, TLS 1.3, Signing | ⭐ แนะนำล่าสุด (เร็ว, เล็ก, ปลอดภัย) |
| ECDSA (P-256, P-384) | 256, 384 bits | TLS, Blockchain, JWT | ดี แต่ Ed25519 ดีกว่า |
| DSA | 1024 bits | Legacy SSH | ❌ ล้าสมัย ไม่ปลอดภัย |
ขั้นตอนการสร้าง Key Pair (Step-by-Step)
🐧 Linux / macOS / Windows (OpenSSH) - มาตรฐานอุตสาหกรรม
- เปิด Terminal / Command Prompt / PowerShell
- รันคำสั่ง (แนะนำ Ed25519):
ssh-keygen -t ed25519 -C "your_email@example.com" -f ~/.ssh/id_ed25519
หรือ RSA 4096 (ถ้าระบบเก่าไม่รองรับ Ed25519):
ssh-keygen -t rsa -b 4096 -C "your_email@example.com" -f ~/.ssh/id_rsa
- ระบบจะถาม:
- Enter passphrase: ใส่ Passphrase ปกป้อง Private Key (แนะนำให้ใส่เสมอ)
- Enter same passphrase again: ยืนยัน
- ไฟล์ที่สร้างขึ้น:
~/.ssh/id_ed25519 → Private Key (เก็บเป็นความลับ!)
~/.ssh/id_ed25519.pub → Public Key (แจกจ่ายได้)
- ตั้งค่าสิทธิ์ (Linux/macOS):
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_ed25519
chmod 644 ~/.ssh/id_ed25519.pub
🪟 Windows (PuTTYgen - สำหรับ PuTTY/Pageant)
- ดาวน์โหลด PuTTYgen (puttygen.exe)
- Parameters: Type of key: Ed25519 (หรือ RSA 4096)
- คลิก Generate → เคลื่อนเมาส์สุ่มในพื้นที่ว่าง
- Key passphrase: ใส่ Passphrase
- Save private key:
id_ed25519.ppk (PuTTY format)
- Save public key:
id_ed25519.pub (OpenSSH format - คัดลอกไป authorized_keys)
- Conversion → Export OpenSSH key →
id_ed25519 (สำหรับ OpenSSH Client)
☁️ Cloud Providers (AWS, Azure, GCP, GitHub, GitLab)
- AWS EC2: Console → Key Pairs → Create Key Pair → Download .pem (Private Key) →
chmod 400 key.pem
- Azure: Portal → SSH Keys → Generate/Upload Public Key
- GCP: Console → Compute → Metadata → SSH Keys → Add
- GitHub/GitLab: Settings → SSH Keys → New SSH Key → Paste Public Key (
.pub content)
การใช้งาน SSH Key Pair
- Copy Public Key ไป Server:
ssh-copy-id user@server-ip
# หรือ manual:
cat ~/.ssh/id_ed25519.pub | ssh user@server "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"
- SSH เข้า Server:
ssh -i ~/.ssh/id_ed25519 user@server-ip
# ถ้าใช้ ssh-agent:
ssh-add ~/.ssh/id_ed25519
ssh user@server-ip
- Config ไฟล์ ~/.ssh/config:
Host myserver
HostName 192.168.1.100
User admin
IdentityFile ~/.ssh/id_ed25519
IdentitiesOnly yes
Best Practices สำหรับ Key Management
- ✅ ใช้ Ed25519 (Default ใน OpenSSH 6.5+)
- ✅ ตั้ง Passphrase ทุกครั้ง (ปกป้อง Private Key หากโดนขโมย)
- ✅ ใช้ ssh-agent จัดการ Key ในหน่วยความจำ (ไม่ต้องพิมพ์ Passphrase ซ้ำ)
- ✅ Rotate Keys ทุก 6-12 เดือน หรือเมื่อสงสัยว่า Key ถูก Compromise
- ✅ Hardware Security Key (YubiKey, Nitrokey): เก็บ Private Key ใน Hardware (FIDO2/U2F) - ปลอดภัยที่สุด
- ✅ Never Share Private Key - ส่งผ่าน Email, Chat, Pastebin, Git Public
- ✅ ใช้ Certificate-based SSH (SSH Certificates) สำหรับ Enterprise (Short-lived, Auditable)
- ✅ Audit & Monitor: Log SSH Access, Alert on New Keys, Review authorized_keys
Troubleshooting
- Permission denied (publickey): Permission ไฟล์ผิด (chmod 600 private, 644 pub, 700 .ssh), wrong key, user mismatch
- Agent admitted failure to sign:
ssh-add ก่อน หรือ restart ssh-agent
- Key type not supported: Server เก่าไม่รองรับ Ed25519 → ใช้ RSA 4096
- Too many authentication failures:
IdentitiesOnly yes ใน config หรือ ssh -o IdentitiesOnly=yes
สรุป: สร้าง Key Pair ด้วย ssh-keygen -t ed25519 ตั้ง Passphrase, Copy Public Key ไป Server, ใช้ ssh-agent, Rotate Keys เป็นประจำ, พิจารณา Hardware Key (YubiKey) สำหรับระดับสูงสุด