Encryption

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_xlsxdecrypt_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.

EnumerationsSố bit khóaalg_id
AES_128128
AES_192192
AES_256256

Enumerations

EnumerationsEnumerationsEnumerations
algorithm_namestrEnumerations "AES".
key_bitsintĐộ dài khóa AES tính bằng bit (128, 192 hoặc 256).
alg_idintĐịnh danh thuật toán số được sử dụng trong bản ghi mã hoá OOXML.
key_bytesintĐộ dài khóa tính bằng byte (key_bits // 8).
block_sizeintEnumerations 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.

EnumerationsSố byte bămalg_id
SHA120
SHA25632
SHA38448
SHA51264

Enumerations

EnumerationsEnumerationsEnumerations
algorithm_namestrTên hàm băm (ví dụ,., "SHA-512").
hash_bytesintĐộ dài digest tính bằng byte.
alg_idintĐị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
)
EnumerationsEnumerationsEnumerationsEnumerations
cipher_algorithmCipherAlgorithmAES_256Độ dài khóa AES cần sử dụng.
hash_algorithmHashAlgorithmSHA512Hàm băm cho việc suy xuất khóa PBKDF2.
spin_countint100000Số 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

EnumerationsEnumerationsEnumerations
encryption_typestrEnumerations "agile".
cipher_algorithmCipherAlgorithmMã hoá đã cấu hình.
hash_algorithmHashAlgorithmHàm băm đã cấu hình.
spin_countintSố lần lặp PBKDF2.
salt_sizeintĐộ dài muối tính bằng byte (được suy ra từ kích thước khối mã hoá).
block_sizeintKích thước khối mã hoá tính bằng byte (luôn 16 đối với AES).
key_bitsintĐộ 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"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.

EnumerationsEnumerationsEnumerations
input_pathstrĐường dẫn tới tệp XLSX nguồn.
output_pathstrĐườ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ỗ.
passwordstrMật khẩu mã hóa.
encryption_params`AgileEncryptionParametersStandardEncryptionParameters

decrypt_xlsx

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

Giả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() -> AgileEncryptionParameters

Trả 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!")

Xem Thêm

 Tiếng Việt