Encryption — Aspose.Cells FOSS for Python API Reference
aspose-cells-foss hỗ trợ mã hoá và giải mã AES cho các tệp XLSX thông qua các hàm cấp mô-đun encrypt_xlsx và decrypt_xlsx. Hai lớp tham số cấu hình thuật toán cipher, thuật toán hash và số vòng PBKDF2. Mặc định được khuyến nghị là AgileEncryptionParameters với AES-256 và SHA-512.
Enumerations: aspose.cells_foss
from aspose.cells_foss import (
CipherAlgorithm, HashAlgorithm,
AgileEncryptionParameters, StandardEncryptionParameters,
encrypt_xlsx, decrypt_xlsx, get_default_encryption_params,
)CipherAlgorithm
CipherAlgorithm là một IntEnum chỉ định độ dài khóa AES. Tất cả các biến thể sử dụng AES ở chế độ CBC với kích thước khối 16 byte.
| Enumerations | Số bit khóa | alg_id |
|---|---|---|
AES_128 | 128 | — |
AES_192 | 192 | — |
AES_256 | 256 | — |
Enumerations
| Enumerations | Enumerations | Enumerations |
|---|---|---|
algorithm_name | str | Enumerations "AES". |
key_bits | int | Độ dài khóa AES tính bằng bit (128, 192 hoặc 256). |
alg_id | int | Định danh thuật toán số được sử dụng trong bản ghi mã hoá OOXML. |
key_bytes | int | Độ dài khóa tính bằng byte (key_bits // 8). |
block_size | int | Enumerations 16 (kích thước khối AES tính bằng byte). |
HashAlgorithm
HashAlgorithm là một IntEnum chỉ định hàm băm được sử dụng cho việc suy xuất khóa PBKDF2.
| Enumerations | Số byte băm | alg_id |
|---|---|---|
SHA1 | 20 | — |
SHA256 | 32 | — |
SHA384 | 48 | — |
SHA512 | 64 | — |
Enumerations
| Enumerations | Enumerations | Enumerations |
|---|---|---|
algorithm_name | str | Tên hàm băm (ví dụ,., "SHA-512"). |
hash_bytes | int | Độ dài digest tính bằng byte. |
alg_id | int | Định danh thuật toán số được sử dụng trong bản ghi mã hóa OOXML. |
AgileEncryptionParameters
AgileEncryptionParameters cấu hình sơ đồ Mã hóa Agile (ECMA-376, phần 4), là định dạng mặc định và được khuyến nghị cho các tệp Excel hiện đại (Excel 2010+).
Enumerations
AgileEncryptionParameters(
cipher_algorithm=CipherAlgorithm.AES_256,
hash_algorithm=HashAlgorithm.SHA512,
spin_count=100000
)| Enumerations | Enumerations | Enumerations | Enumerations |
|---|---|---|---|
cipher_algorithm | CipherAlgorithm | AES_256 | Độ dài khóa AES cần sử dụng. |
hash_algorithm | HashAlgorithm | SHA512 | Hàm băm cho việc suy xuất khóa PBKDF2. |
spin_count | int | 100000 | Số lần lặp PBKDF2. Giá trị cao hơn tăng khả năng chống tấn công brute-force nhưng làm tăng thời gian mở/lưu. |
Enumerations
| Enumerations | Enumerations | Enumerations |
|---|---|---|
encryption_type | str | Enumerations "agile". |
cipher_algorithm | CipherAlgorithm | Mã hoá đã cấu hình. |
hash_algorithm | HashAlgorithm | Hàm băm đã cấu hình. |
spin_count | int | Số lần lặp PBKDF2. |
salt_size | int | Độ dài muối tính bằng byte (được suy ra từ kích thước khối mã hoá). |
block_size | int | Kích thước khối mã hoá tính bằng byte (luôn 16 đối với AES). |
key_bits | int | Độ dài khóa tính bằng bit. |
StandardEncryptionParameters
StandardEncryptionParameters cấu hình chế độ Mã hoá Tiêu chuẩn để tương thích với Excel 2007. Nên dùng AgileEncryptionParameters cho các tệp mới.
Enumerations
StandardEncryptionParameters(
cipher_algorithm=CipherAlgorithm.AES_128,
hash_algorithm=HashAlgorithm.SHA1,
spin_count=50000
)Các thuộc tính phản ánh của AgileEncryptionParameters. Thuộc tính encryption_type là "standard".
Các hàm mô-đun
encrypt_xlsx
encrypt_xlsx(
input_path: str,
output_path: str,
password: str,
encryption_params=None
) -> NoneĐọc tệp XLSX tại input_path, mã hoá nó bằng password sử dụng được chỉ định encryption_params, và ghi tệp đã mã hóa vào output_path.
| Enumerations | Enumerations | Enumerations |
|---|---|---|
input_path | str | Đường dẫn tới tệp XLSX nguồn. |
output_path | str | Đường dẫn đích cho tệp đã mã hóa. Có thể giống như input_path đối với mã hóa tại chỗ. |
password | str | Mật khẩu mã hóa. |
encryption_params | `AgileEncryptionParameters | StandardEncryptionParameters |
decrypt_xlsx
decrypt_xlsx(
input_path: str,
output_path: str,
password: str
) -> boolGiải mã tệp XLSX tại input_path bằng cách sử dụng password và ghi tệp XLSX bản rõ vào output_path. Trả về True nếu giải mã thành công, False nếu mật khẩu không đúng hoặc tệp không được mã hóa.
get_default_encryption_params
get_default_encryption_params() -> AgileEncryptionParametersTrả về một AgileEncryptionParameters đối tượng được cấu hình với AES_256, SHA512, và spin_count=100000. Sử dụng điều này để kiểm tra hoặc sao chép các mặc định của thư viện trước khi tùy chỉnh.
Enumerations
Mã hoá một 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)Giải mã một 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.")Mở tệp được mã hóa trực tiếp
Enumerations Workbook constructor cũng chấp nhận một password tham số để mở các tệp được mã hóa mà không cần bước giải mã riêng:
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!")