โค้ด: เลือกทั้งหมด
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(" เสร็จสิ้นทุกขั้นตอน")