วิธีการเข้ารหัสไฟล์ ด้วย Python ใช้ PUBLIC KEY และ PGP แล้ว อัพโหลดไฟล์ด้วย sftp ไปยัง Server ปลายทาง

แชร์ความรู้ภาษา Python ไพทอน การเขียนโปรแกรมภาษาไพทอน

Moderator: mindphp, ผู้ดูแลกระดาน

ภาพประจำตัวสมาชิก
mindphp
ผู้ดูแลระบบ MindPHP
ผู้ดูแลระบบ MindPHP
โพสต์: 48003
ลงทะเบียนเมื่อ: 22/09/2008 6:18 pm
ติดต่อ:

วิธีการเข้ารหัสไฟล์ ด้วย Python ใช้ PUBLIC KEY และ PGP แล้ว อัพโหลดไฟล์ด้วย sftp ไปยัง Server ปลายทาง

โพสต์ที่ยังไม่ได้อ่าน โดย mindphp »

วิธีการเข้ารหัสไฟล์ ด้วย Python ใช้ PUBLIC KEY และ PGP แล้ว อัพโหลดไฟล์ด้วย sftp ไปยัง Server ปลายทาง

โค้ด: เลือกทั้งหมด

import pgpy
from pathlib import Path
import paramiko

# ---------- CONFIG ----------
public_key_path = "public_uat.pgp"
files_to_encrypt = ["data.csv", "report.pdf"]
sftp_config = {
    "host": "sftp.example.com",
    "port": 22,
    "username": "your_username",
    "password": "your_password",
    "remote_path": "/upload/path/"  # ปลายทางที่ SFTP
}
# ----------------------------

# โหลด public key
public_key, _ = pgpy.PGPKey.from_file(public_key_path)

# เข้ารหัสทุกไฟล์ในรายการ
encrypted_files = []
for filepath in files_to_encrypt:
    data = Path(filepath).read_bytes()
    message = pgpy.PGPMessage.new(data, file=True)
    encrypted = public_key.encrypt(message)

    output_path = f"{filepath}.pgp"
    with open(output_path, 'w') as f:
        f.write(str(encrypted))
    encrypted_files.append(output_path)
    print(f" เข้ารหัส: {output_path}")

# เชื่อมต่อ SFTP และอัปโหลดไฟล์
transport = paramiko.Transport((sftp_config["host"], sftp_config["port"]))
transport.connect(username=sftp_config["username"], password=sftp_config["password"])
sftp = paramiko.SFTPClient.from_transport(transport)

for enc_file in encrypted_files:
    remote_file_path = sftp_config["remote_path"] + Path(enc_file).name
    sftp.put(enc_file, remote_file_path)
    print(f" อัปโหลดไปยัง SFTP: {remote_file_path}")

sftp.close()
transport.close()
print(" เสร็จสิ้นทุกขั้นตอน")
ตัวอย่างนี้ใช้ทำระบบ E-tax ส่งเข้า Server ปลายทางด้วย sftp
ติดตาม VDO: http://www.youtube.com/c/MindphpVideoman
ติดตาม FB: https://www.facebook.com/pages/MindphpC ... 9517401606
หมวดแชร์ความรู้: https://www.mindphp.com/forums/viewforum.php?f=29
รับอบรม และพัฒนาระบบ: https://www.mindphp.com/forums/viewtopic.php?f=6&t=2042
ภาพประจำตัวสมาชิก
MBMoo
PHP VIP Members
PHP VIP Members
โพสต์: 32362
ลงทะเบียนเมื่อ: 04/06/2020 10:05 am

Re: วิธีการเข้ารหัสไฟล์ ด้วย Python ใช้ PUBLIC KEY และ PGP แล้ว อัพโหลดไฟล์ด้วย sftp ไปยัง Server ปลายทาง

โพสต์ที่ยังไม่ได้อ่าน โดย MBMoo »

สอบถามค่ะ ค่า str(encrypted) ที่ได้มาถือว่าเป็น base64 หรือยังคะ ตรวจสอบได้ type เป็น str ถ้าต้องการเอาไปบันทึกข้อมูลประเภท binary ต้องแปลงอะไรก่อนมั้ยคะ
ภาพประจำตัวสมาชิก
mindphp
ผู้ดูแลระบบ MindPHP
ผู้ดูแลระบบ MindPHP
โพสต์: 48003
ลงทะเบียนเมื่อ: 22/09/2008 6:18 pm
ติดต่อ:

Re: วิธีการเข้ารหัสไฟล์ ด้วย Python ใช้ PUBLIC KEY และ PGP แล้ว อัพโหลดไฟล์ด้วย sftp ไปยัง Server ปลายทาง

โพสต์ที่ยังไม่ได้อ่าน โดย mindphp »

การวางไฟล์ config

โค้ด: เลือกทั้งหมด

  "remote_path": "/upload/path/"  # ปลายทางที่ SFTP
Path เริ่มต้น

ตอนวางจริง

โค้ด: เลือกทั้งหมด

 remote_file_path = sftp_config["remote_path"] + Path(enc_file).name
    sftp.put(enc_file, remote_file_path)
    print(f" อัปโหลดไปยัง SFTP: {remote_file_path}")
ระบุแบบเต็มถึงแต่ path เริ่มต้น
ติดตาม VDO: http://www.youtube.com/c/MindphpVideoman
ติดตาม FB: https://www.facebook.com/pages/MindphpC ... 9517401606
หมวดแชร์ความรู้: https://www.mindphp.com/forums/viewforum.php?f=29
รับอบรม และพัฒนาระบบ: https://www.mindphp.com/forums/viewtopic.php?f=6&t=2042
ภาพประจำตัวสมาชิก
MBMoo
PHP VIP Members
PHP VIP Members
โพสต์: 32362
ลงทะเบียนเมื่อ: 04/06/2020 10:05 am

Re: วิธีการเข้ารหัสไฟล์ ด้วย Python ใช้ PUBLIC KEY และ PGP แล้ว อัพโหลดไฟล์ด้วย sftp ไปยัง Server ปลายทาง

โพสต์ที่ยังไม่ได้อ่าน โดย MBMoo »

ถ้าตอนเข้ารหัสในขั้นตอน

โค้ด: เลือกทั้งหมด

message = pgpy.PGPMessage.new(data, file=True)
มีการปรับเป็น

โค้ด: เลือกทั้งหมด

message = pgpy.PGPMessage.new(data)
เนื่องจากเจอปัญหาแจ้งเตือน ไฟล์.pdf : stat: embedded null character in path จะทำให้การเข้ารหัสไฟล์ใช้งานได้ปกติเหมือนเดิมไหมคะ
ปัญหานี้เป็นเฉพาะไฟล์ pdf
ภาพประจำตัวสมาชิก
mindphp
ผู้ดูแลระบบ MindPHP
ผู้ดูแลระบบ MindPHP
โพสต์: 48003
ลงทะเบียนเมื่อ: 22/09/2008 6:18 pm
ติดต่อ:

Re: วิธีการเข้ารหัสไฟล์ ด้วย Python ใช้ PUBLIC KEY และ PGP แล้ว อัพโหลดไฟล์ด้วย sftp ไปยัง Server ปลายทาง

โพสต์ที่ยังไม่ได้อ่าน โดย mindphp »

ก่อนหน้าโค้ดที่ยกมามีอะไร

การเข้ารหัส
แยก สองประเด็นคือ เข้ารหัสทั้งไฟล์ หรือ อ่านไฟล์ภายในแล้วเข้ารหัส
ถ้าเป็น pdf อ่านไฟล์ ออกมาเข้ารหัสไม่น่าจะทำได้เพราะมันเป็น binary ต้องอ่านไฟล์มาเป็น binary ก่อน base64
ลองดูใหม่ตามนี้

โค้ด: เลือกทั้งหมด

import base64
from pgpy import PGPMessage, PGPKey

# โหลด public key ผู้รับ
public_key, _ = PGPKey.from_file("recipient_public_key.asc")

# อ่านไฟล์ต้นฉบับ (สามารถเปลี่ยนชื่อไฟล์ได้ เช่น 'document.xlsx')
with open("example.pdf", "rb") as f:
    file_data = f.read()

# แปลงเป็นข้อความ base64
encoded_data = base64.b64encode(file_data).decode("utf-8")

# สร้างข้อความ PGP
message = PGPMessage.new(encoded_data)

# เข้ารหัส
encrypted_message = public_key.encrypt(message)

# บันทึกผลลัพธ์เป็นไฟล์ .pgp หรือ .asc
with open("example.pdf.pgp", "w") as f:
    f.write(str(encrypted_message))

print(" เข้ารหัสเรียบร้อยแล้ว: example.pdf.pgp")
ถ้าปลายทางต้องการเข้ารหัสทั้งไฟล์โดยตรง โดยไม่ถอดออกมาก่อน ต้องใช้ lib ตัวอื่นเช่น python-gnupg

ตัวอย่าง

โค้ด: เลือกทั้งหมด

import gnupg

# สร้างอินสแตนซ์ของ GPG
gpg = gnupg.GPG(gnupghome="./gnupg_home")  # หรือไม่ระบุ gnupghome ก็ได้

# --- 1. โหลด public key ของผู้รับจากไฟล์ .asc ---
with open("recipient_public_key.asc", "r") as f:
    public_key_data = f.read()

import_result = gpg.import_keys(public_key_data)

if not import_result.count:
    raise Exception(" ไม่สามารถนำเข้า Public Key ได้")

# ดึง fingerprint ของ public key ที่นำเข้า
fingerprint = import_result.fingerprints[0]
print(" นำเข้า public key เรียบร้อย:", fingerprint)

# --- 2. เข้ารหัสไฟล์ myfile.pdf ด้วย public key นั้น ---
with open("myfile.pdf", "rb") as f:
    status = gpg.encrypt_file(
        f,
        recipients=[fingerprint],
        output="myfile.pdf.gpg"
    )

# --- 3. ตรวจสอบสถานะ ---
if status.ok:
    print(" เข้ารหัสสำเร็จ! ได้ไฟล์: myfile.pdf.gpg")
else:
    print(" เข้ารหัสไม่สำเร็จ:", status.status)
**** วิธีการทดสอบว่า เข้ารหัสและถอดกลับมาได้ โดยการสร้าง pkey , private key เองแล้ว สำหรับเข้า และ ถอดรหัส
แล้วตอนใช้งานจริงค่อยเปลี่ยน pkey เป็นของที่จะใช้จริง
ติดตาม VDO: http://www.youtube.com/c/MindphpVideoman
ติดตาม FB: https://www.facebook.com/pages/MindphpC ... 9517401606
หมวดแชร์ความรู้: https://www.mindphp.com/forums/viewforum.php?f=29
รับอบรม และพัฒนาระบบ: https://www.mindphp.com/forums/viewtopic.php?f=6&t=2042
ตอบกลับโพส
  • Similar Topics
    ตอบกลับ
    แสดง
    โพสต์ล่าสุด

ผู้ใช้งานขณะนี้

สมาชิกกำลังดูบอร์ดนี้: ไม่มีสมาชิกใหม่ และบุคลทั่วไป 5