IPSEC Tunnel ทำงานอย่างไร
Back to articles

IPSEC Tunnel ทำงานอย่างไร

IPSEC Tunnel ทำงานอย่างไร

Network Updated: February 5, 2026

IPsec Tunnel คืออะไร?

IPsec Tunnel (Internet Protocol Security Tunnel) เป็นเทคนิคในการสร้างการเชื่อมต่อระหว่างเครือข่ายผ่านเครือข่ายไม่น่าเชื่อถือ โดยมุ่งหวังในการป้องกันความปลอดภัยของข้อมูลที่ถูกส่งผ่านเครือข่ายด้วยการเข้ารหัสข้อมูลและสร้างต้นไม้คีย์

ขั้นตอนการทำงานของ IPsec Tunnel

  1. กำหนดค่าและเป้าหมาย: กำหนดค่าเครือข่ายและ IP ที่เป็นเครื่องหมายในการสร้าง IPsec Tunnel รวมถึงเลือกการใช้งานในโหมด Transport หรือ Tunnel
  1. สร้างต้นไม้คีย์ (Key Hierarchy): สร้างต้นไม้คีย์ที่จะใช้ในการเข้ารหัสและถอดรหัสข้อมูล รวมถึงเลือกวิธีการเข้ารหัสและเลือกอัลกอริทึมเข้ารหัสที่เหมาะสม
  1. สร้าง Security Associations (SAs): สร้าง Security Associations ที่ระบุกฎเกี่ยวกับการเข้ารหัสและถอดรหัส รวมถึงเลือกข้อมูลเพิ่มเติมเช่น Sequence Number เพื่อป้องกันการเลียนแบบแพคเก็ต
  1. การเข้ารหัสและถอดรหัส: ข้อมูลที่จะถูกส่งผ่านเครือข่ายจะถูกเข้ารหัสก่อนการส่งและถอดรหัสที่ปลายทางก่อนนำไปใช้งาน

ตัวอย่างโค้ด Python สำหรับ IPsec Tunnel

สมมติว่าเราต้องการสร้าง IPsec Tunnel ระหว่างเครือข่าย A (10.0.0.0/24) และเครือข่าย B (20.0.0.0/24) โดยใช้ Python

# ตั้งค่าเครือข่ายและ IP
network_A = "10.0.0.0/24"
network_B = "20.0.0.0/24"
ip_A = "10.0.0.1"
ip_B = "20.0.0.1"

# สร้างต้นไม้คีย์
def create_key_hierarchy():
# สร้างคีย์สำหรับเข้ารหัสและถอดรหัส
encryption_key = "encryption_key"
authentication_key = "authentication_key"
return encryption_key, authentication_key

# สร้าง Security Associations (SAs)
def create_security_associations():
sa_A = {"src_ip": ip_A, "dst_ip": ip_B, "encryption_key": encryption_key_A, "authentication_key": authentication_key_A}
sa_B = {"src_ip": ip_B, "dst_ip": ip_A, "encryption_key": encryption_key_B, "authentication_key": authentication_key_B}
return sa_A, sa_B

# เข้ารหัสข้อมูล
def encrypt_data(data, sa):
encrypted_data = "Encrypted: " + data
return encrypted_data

# ถอดรหัสข้อมูล
def decrypt_data(encrypted_data, sa):
decrypted_data = encrypted_data.split("Encrypted: ")[1]
return decrypted_data

# สร้างต้นไม้คีย์
encryption_key_A, authentication_key_A = create_key_hierarchy()
encryption_key_B, authentication_key_B = create_key_hierarchy()

# สร้าง Security Associations (SAs)
sa_A, sa_B = create_security_associations()

# ข้อมูลที่จะถูกส่งผ่านเครือข่าย
data_to_send = "Hello, IPsec Tunnel!"

# เข้ารหัสข้อมูลและถอดรหัสข้อมูล
encrypted_data_A = encrypt_data(data_to_send, sa_A)
decrypted_data_B = decrypt_data(encrypted_data_A, sa_B)

print("Encrypted Data Sent from A to B:")
print(encrypted_data_A)
print("\nDecrypted Data Received by B:")
print(decrypted_data_B)

D

DriteStudio | ไดรท์สตูดิโอ

Cloud, VPS, Hosting and Colocation provider in Thailand

Operated by Craft Intertech (Thailand) Co., Ltd.

Manage your cookie settings

We use different types of cookies to optimize your experience on our website. Click on the categories below to learn more and customize your preferences. Note that blocking some types of cookies may impact your experience.

Necessary Cookies

These cookies are essential for the website to function properly. They enable basic functions like page navigation and access to secure areas.

View cookies used
  • Session cookies (session management)
  • Security cookies (CSRF protection)
Always On

Functional Cookies

These cookies enable personalized features like language preferences and theme settings. Without these, some features may not work properly.

View cookies used
  • lang (language preference)
  • theme (dark/light mode)

Analytics Cookies

These cookies help us understand how visitors interact with our website by collecting and reporting information anonymously.

View cookies used
  • _ga (Google Analytics)
  • _gid (Google Analytics)

Marketing Cookies

These cookies are used to track visitors across websites to display relevant advertisements based on your interests.

View cookies used
  • Advertising cookies
  • Remarketing pixels

Privacy Policy