Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> ระบบเครือข่าย >> ความปลอดภัยเครือข่าย

Network Security: ขั้นตอนการสร้าง Key Pair (Public/Private Key) สำหรับ SSH และการเข้ารหัส

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)

อัลกอริทึมยอดนิยม

ขั้นตอนการสร้าง Key Pair (Step-by-Step)

🐧 Linux / macOS / Windows (OpenSSH) - มาตรฐานอุตสาหกรรม

  1. เปิด Terminal / Command Prompt / PowerShell
  2. รันคำสั่ง (แนะนำ 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
  3. ระบบจะถาม:
    • Enter passphrase: ใส่ Passphrase ปกป้อง Private Key (แนะนำให้ใส่เสมอ)
    • Enter same passphrase again: ยืนยัน
  4. ไฟล์ที่สร้างขึ้น:
    • ~/.ssh/id_ed25519 → Private Key (เก็บเป็นความลับ!)
    • ~/.ssh/id_ed25519.pub → Public Key (แจกจ่ายได้)
  5. ตั้งค่าสิทธิ์ (Linux/macOS):
    chmod 700 ~/.ssh
    chmod 600 ~/.ssh/id_ed25519
    chmod 644 ~/.ssh/id_ed25519.pub
    

🪟 Windows (PuTTYgen - สำหรับ PuTTY/Pageant)

  1. ดาวน์โหลด PuTTYgen (puttygen.exe)
  2. Parameters: Type of key: Ed25519 (หรือ RSA 4096)
  3. คลิก Generate → เคลื่อนเมาส์สุ่มในพื้นที่ว่าง
  4. Key passphrase: ใส่ Passphrase
  5. Save private key: id_ed25519.ppk (PuTTY format)
  6. Save public key: id_ed25519.pub (OpenSSH format - คัดลอกไป authorized_keys)
  7. 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

  1. 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"
    
  2. SSH เข้า Server:
    ssh -i ~/.ssh/id_ed25519 user@server-ip
    # ถ้าใช้ ssh-agent:
    ssh-add ~/.ssh/id_ed25519
    ssh user@server-ip
    
  3. 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) สำหรับระดับสูงสุด

AlgorithmKey Sizeใช้งานสถานะ
RSA2048, 3072, 4096 bitsSSH, TLS, Email Encryption, Code Signingมาตรฐาน, รองรับทุกที่
Ed25519256 bitsSSH, TLS 1.3, Signing⭐ แนะนำล่าสุด (เร็ว, เล็ก, ปลอดภัย)
ECDSA (P-256, P-384)256, 384 bitsTLS, Blockchain, JWTดี แต่ Ed25519 ดีกว่า
DSA1024 bitsLegacy SSH❌ ล้าสมัย ไม่ปลอดภัย