Encryption

Encryption — Aspose.Cells FOSS for Python API Reference

aspose-cells-foss รองรับการเข้ารหัสและถอดรหัส AES ของไฟล์ XLSX ผ่านฟังก์ชันระดับโมดูล encrypt_xlsx และ decrypt_xlsx.สองคลาสพารามิเตอร์กำหนดค่าอัลกอริทึมการเข้ารหัส, อัลกอริทึมแฮช, และจำนวนรอบ PBKDF2. ค่าเริ่มต้นที่แนะนำคือ AgileEncryptionParameters ด้วย AES-256 และ SHA-512.

Properties: aspose.cells_foss

from aspose.cells_foss import (
    CipherAlgorithm, HashAlgorithm,
    AgileEncryptionParameters, StandardEncryptionParameters,
    encrypt_xlsx, decrypt_xlsx, get_default_encryption_params,
)

CipherAlgorithm

CipherAlgorithm เป็น IntEnum ที่ระบุความยาวคีย์ AES.

Propertiesบิตของคีย์alg_id
AES_128128
AES_192192
AES_256256

Properties

PropertiesPropertiesProperties
algorithm_namestrProperties "AES".
key_bitsintความยาวคีย์ AES เป็นบิต (128, 192 หรือ 256).
alg_idintตัวระบุอัลกอริทึมเชิงตัวเลขที่ใช้ในบันทึกการเข้ารหัส OOXML.
key_bytesintความยาวคีย์เป็นไบต์ (key_bits // 8).
block_sizeintProperties 16 (ขนาดบล็อก AES เป็นไบต์).

HashAlgorithm

HashAlgorithm เป็น IntEnum ที่ระบุฟังก์ชันแฮชที่ใช้สำหรับการสกัดกุญแจ PBKDF2.

Propertiesไบต์แฮชalg_id
SHA120
SHA25632
SHA38448
SHA51264

Properties

PropertiesPropertiesProperties
algorithm_namestrชื่อฟังก์ชันแฮช (เช่น,., "SHA-512").
hash_bytesintความยาวดิจสต์เป็นไบต์.
alg_idintตัวระบุอัลกอริทึมเชิงตัวเลขที่ใช้ในบันทึกการเข้ารหัส OOXML.

AgileEncryptionParameters

AgileEncryptionParameters กำหนดค่า Agile Encryption scheme (ECMA-376, part 4) ซึ่งเป็นรูปแบบเริ่มต้นและแนะนำสำหรับไฟล์ Excel สมัยใหม่ (Excel 2010+).

Properties

AgileEncryptionParameters(
    cipher_algorithm=CipherAlgorithm.AES_256,
    hash_algorithm=HashAlgorithm.SHA512,
    spin_count=100000
)
PropertiesPropertiesPropertiesProperties
cipher_algorithmCipherAlgorithmAES_256ความยาวคีย์ AES ที่จะใช้.
hash_algorithmHashAlgorithmSHA512ฟังก์ชันแฮชสำหรับการสกัดคีย์ PBKDF2.
spin_countint100000จำนวนรอบของ PBKDF2. ค่าที่สูงขึ้นจะเพิ่มความต้านทานต่อการโจมตีแบบ brute-force แต่จะทำให้เวลาเปิด/บันทึกเพิ่มขึ้น.

Properties

PropertiesPropertiesProperties
encryption_typestrProperties "agile".
cipher_algorithmCipherAlgorithmไซเฟอร์ที่กำหนดค่า.
hash_algorithmHashAlgorithmฟังก์ชันแฮชที่กำหนดค่า.
spin_countintจำนวนรอบ PBKDF2.
salt_sizeintความยาวของ salt เป็นไบต์ (ได้มาจากขนาดบล็อกของไซเฟอร์).
block_sizeintขนาดบล็อกของไซเฟอร์เป็นไบต์ (เสมอ 16 สำหรับ AES).
key_bitsintความยาวคีย์เป็นบิต.

StandardEncryptionParameters

StandardEncryptionParameters กำหนดค่าโหมดการเข้ารหัสมาตรฐานเพื่อความเข้ากันได้กับ Excel 2007. แนะนำให้ใช้ AgileEncryptionParameters สำหรับไฟล์ใหม่.

Properties

StandardEncryptionParameters(
    cipher_algorithm=CipherAlgorithm.AES_128,
    hash_algorithm=HashAlgorithm.SHA1,
    spin_count=50000
)

คุณสมบัตินี้สะท้อนของ AgileEncryptionParameters. ส่วน encryption_type คุณสมบัตินี้คือ "standard".


ฟังก์ชันโมดูล

encrypt_xlsx

encrypt_xlsx(
    input_path: str,
    output_path: str,
    password: str,
    encryption_params=None
) -> None

อ่านไฟล์ XLSX ที่ input_path, เข้ารหัสด้วย password โดยใช้ที่ระบุ encryption_params, และเขียนไฟล์ที่เข้ารหัสไปยัง output_path.

PropertiesPropertiesProperties
input_pathstrเส้นทางไปยังไฟล์ XLSX ต้นฉบับ.
output_pathstrเส้นทางปลายทางสำหรับไฟล์ที่เข้ารหัส อาจเป็นเดียวกับ input_path สำหรับการเข้ารหัสในที่เดียว.
passwordstrรหัสผ่านการเข้ารหัส.
encryption_params`AgileEncryptionParametersStandardEncryptionParameters

decrypt_xlsx

decrypt_xlsx(
    input_path: str,
    output_path: str,
    password: str
) -> bool

ถอดรหัสไฟล์ XLSX ที่ input_path โดยใช้ password และเขียนไฟล์ XLSX แบบข้อความธรรมดาไปยัง output_path. คืนค่า True หากการถอดรหัสสำเร็จ, False หากรหัสผ่านไม่ถูกต้องหรือไฟล์ไม่ได้ถูกเข้ารหัส.

get_default_encryption_params

get_default_encryption_params() -> AgileEncryptionParameters

คืนค่า AgileEncryptionParameters อินสแตนซ์ที่กำหนดค่าโดย AES_256, SHA512, และ spin_count=100000. ใช้สิ่งนี้เพื่อตรวจสอบหรือคัดลอกค่าตั้งต้นของไลบรารีก่อนทำการปรับแต่ง.


Properties

เข้ารหัส Workbook

from aspose.cells_foss import Workbook, encrypt_xlsx, AgileEncryptionParameters, CipherAlgorithm, HashAlgorithm

# 1. Build and save an unencrypted workbook
wb = Workbook()
ws = wb.worksheets[0]
ws.cells["A1"].put_value("Confidential data")
wb.save("report.xlsx")

# 2. Encrypt with default AES-256/SHA-512 settings
encrypt_xlsx("report.xlsx", "report_secure.xlsx", password="MyP@ssw0rd!")

# 3. Alternatively, use a lower spin count for faster open/save cycles during development
params = AgileEncryptionParameters(
    cipher_algorithm=CipherAlgorithm.AES_256,
    hash_algorithm=HashAlgorithm.SHA256,
    spin_count=10000,
)
encrypt_xlsx("report.xlsx", "report_dev.xlsx", password="dev_only", encryption_params=params)

ถอดรหัส Workbook

from aspose.cells_foss import decrypt_xlsx, Workbook

success = decrypt_xlsx("report_secure.xlsx", "report_decrypted.xlsx", password="MyP@ssw0rd!")

if success:
    wb = Workbook("report_decrypted.xlsx")
    print(wb.worksheets[0].cells["A1"].value)
else:
    print("Incorrect password or file is not encrypted.")

เปิดไฟล์ที่เข้ารหัสโดยตรง

Properties Workbook คอนสตรัคเตอร์ยังรับค่า a password พารามิเตอร์เพื่อเปิดไฟล์ที่เข้ารหัสโดยไม่ต้องทำขั้นตอนการถอดรหัสแยกต่างหาก:

from aspose.cells_foss import Workbook

wb = Workbook("report_secure.xlsx", password="MyP@ssw0rd!")
print(wb.worksheets[0].cells["A1"].value)
wb.save("report_updated_secure.xlsx", password="MyP@ssw0rd!")

ดูเพิ่มเติม

 ภาษาไทย