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_128 | 128 | — |
AES_192 | 192 | — |
AES_256 | 256 | — |
Properties
| Properties | Properties | Properties |
|---|---|---|
algorithm_name | str | Properties "AES". |
key_bits | int | ความยาวคีย์ AES เป็นบิต (128, 192 หรือ 256). |
alg_id | int | ตัวระบุอัลกอริทึมเชิงตัวเลขที่ใช้ในบันทึกการเข้ารหัส OOXML. |
key_bytes | int | ความยาวคีย์เป็นไบต์ (key_bits // 8). |
block_size | int | Properties 16 (ขนาดบล็อก AES เป็นไบต์). |
HashAlgorithm
HashAlgorithm เป็น IntEnum ที่ระบุฟังก์ชันแฮชที่ใช้สำหรับการสกัดกุญแจ PBKDF2.
| Properties | ไบต์แฮช | alg_id |
|---|---|---|
SHA1 | 20 | — |
SHA256 | 32 | — |
SHA384 | 48 | — |
SHA512 | 64 | — |
Properties
| Properties | Properties | Properties |
|---|---|---|
algorithm_name | str | ชื่อฟังก์ชันแฮช (เช่น,., "SHA-512"). |
hash_bytes | int | ความยาวดิจสต์เป็นไบต์. |
alg_id | int | ตัวระบุอัลกอริทึมเชิงตัวเลขที่ใช้ในบันทึกการเข้ารหัส 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
)| Properties | Properties | Properties | Properties |
|---|---|---|---|
cipher_algorithm | CipherAlgorithm | AES_256 | ความยาวคีย์ AES ที่จะใช้. |
hash_algorithm | HashAlgorithm | SHA512 | ฟังก์ชันแฮชสำหรับการสกัดคีย์ PBKDF2. |
spin_count | int | 100000 | จำนวนรอบของ PBKDF2. ค่าที่สูงขึ้นจะเพิ่มความต้านทานต่อการโจมตีแบบ brute-force แต่จะทำให้เวลาเปิด/บันทึกเพิ่มขึ้น. |
Properties
| Properties | Properties | Properties |
|---|---|---|
encryption_type | str | Properties "agile". |
cipher_algorithm | CipherAlgorithm | ไซเฟอร์ที่กำหนดค่า. |
hash_algorithm | HashAlgorithm | ฟังก์ชันแฮชที่กำหนดค่า. |
spin_count | int | จำนวนรอบ PBKDF2. |
salt_size | int | ความยาวของ salt เป็นไบต์ (ได้มาจากขนาดบล็อกของไซเฟอร์). |
block_size | int | ขนาดบล็อกของไซเฟอร์เป็นไบต์ (เสมอ 16 สำหรับ AES). |
key_bits | int | ความยาวคีย์เป็นบิต. |
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.
| Properties | Properties | Properties |
|---|---|---|
input_path | str | เส้นทางไปยังไฟล์ XLSX ต้นฉบับ. |
output_path | str | เส้นทางปลายทางสำหรับไฟล์ที่เข้ารหัส อาจเป็นเดียวกับ input_path สำหรับการเข้ารหัสในที่เดียว. |
password | str | รหัสผ่านการเข้ารหัส. |
encryption_params | `AgileEncryptionParameters | StandardEncryptionParameters |
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!")