Encryption — Aspose.Cells FOSS for Python API Reference
aspose-cells-foss podporuje šifrování a dešifrování souborů XLSX pomocí funkcí na úrovni modulu encrypt_xlsx a decrypt_xlsx. Dvě třídy parametrů konfigurovat algoritmus šifry, hash algoritmus a počet iterací PBKDF2. Doporučená výchozí hodnota je AgileEncryptionParameters s AES-256 a SHA-512.
Example: aspose.cells_foss
from aspose.cells_foss import (
CipherAlgorithm, HashAlgorithm,
AgileEncryptionParameters, StandardEncryptionParameters,
encrypt_xlsx, decrypt_xlsx, get_default_encryption_params,
)CipherAlgorithm
CipherAlgorithm je IntEnum specifikující délku klíče AES.
| Example | Bity klíče | alg_id |
|---|---|---|
AES_128 | 128 | — |
AES_192 | 192 | — |
AES_256 | 256 | — |
Example
| Example | Example | Example |
|---|---|---|
algorithm_name | str | Example "AES". |
key_bits | int | Délka klíče AES v bitech (128, 192 nebo 256). |
alg_id | int | Číselný identifikátor algoritmu použitý v záznamu šifrování OOXML. |
key_bytes | int | Délka klíče v bajtech (key_bits // 8). |
block_size | int | Example 16 (velikost bloku AES v bajtech). |
HashAlgorithm
HashAlgorithm je IntEnum specifikující hash funkci používanou pro odvození klíče PBKDF2.
| Example | Bajty hash | alg_id |
|---|---|---|
SHA1 | 20 | — |
SHA256 | 32 | — |
SHA384 | 48 | — |
SHA512 | 64 | — |
Example
| Example | Example | Example |
|---|---|---|
algorithm_name | str | Název hash funkce (např., "SHA-512"). |
hash_bytes | int | Délka digestu v bajtech. |
alg_id | int | Číselný identifikátor algoritmu používaný v záznamu šifrování OOXML. |
AgileEncryptionParameters
AgileEncryptionParameters konfiguruje schéma Agile Encryption (ECMA-376, část 4), které je výchozím a doporučeným formátem pro moderní soubory Excel (Excel 2010+).
Example
AgileEncryptionParameters(
cipher_algorithm=CipherAlgorithm.AES_256,
hash_algorithm=HashAlgorithm.SHA512,
spin_count=100000
)| Example | Example | Example | Example |
|---|---|---|---|
cipher_algorithm | CipherAlgorithm | AES_256 | Délka klíče AES, která se má použít. |
hash_algorithm | HashAlgorithm | SHA512 | Hashovací funkce pro odvození klíče PBKDF2. |
spin_count | int | 100000 | Počet iterací PBKDF2. Vyšší hodnoty zvyšují odolnost proti brute‑force útokům na úkor doby otevření/uložení. |
Example
| Example | Example | Example |
|---|---|---|
encryption_type | str | Example "agile". |
cipher_algorithm | CipherAlgorithm | Nastavený šifrovací algoritmus. |
hash_algorithm | HashAlgorithm | Nastavená hashovací funkce. |
spin_count | int | Počet iterací PBKDF2. |
salt_size | int | Délka soli v bajtech (odvozená od velikosti bloku šifry). |
block_size | int | Velikost bloku šifry v bajtech (vždy 16 pro AES). |
key_bits | int | Délka klíče v bitech. |
StandardEncryptionParameters
StandardEncryptionParameters konfiguruje schéma Standard Encryption pro kompatibilitu s Excel 2007. Upřednostněte AgileEncryptionParameters pro nové soubory.
Example
StandardEncryptionParameters(
cipher_algorithm=CipherAlgorithm.AES_128,
hash_algorithm=HashAlgorithm.SHA1,
spin_count=50000
)Vlastnosti odrážejí ty z AgileEncryptionParameters. V encryption_type vlastnost je "standard".
Funkce modulu
encrypt_xlsx
encrypt_xlsx(
input_path: str,
output_path: str,
password: str,
encryption_params=None
) -> NoneNačte soubor XLSX z input_path, zašifruje jej pomocí password používajícím zadaný encryption_params, a zapíše šifrovaný soubor do output_path.
| Example | Example | Example |
|---|---|---|
input_path | str | Cesta k zdrojovému souboru XLSX. |
output_path | str | Cílová cesta pro šifrovaný soubor. Může být stejná jako input_path pro šifrování na místě. |
password | str | Šifrovací heslo. |
encryption_params | `AgileEncryptionParameters | StandardEncryptionParameters |
decrypt_xlsx
decrypt_xlsx(
input_path: str,
output_path: str,
password: str
) -> boolDešifruje soubor XLSX na input_path používá password a zapíše nešifrovaný XLSX do output_path. Vrací True pokud dešifrování uspělo, False pokud bylo heslo nesprávné nebo soubor nebyl zašifrován.
get_default_encryption_params
get_default_encryption_params() -> AgileEncryptionParametersVrací AgileEncryptionParameters instanci nakonfigurovanou s AES_256, SHA512, a spin_count=100000. Použijte to k prozkoumání nebo klonování výchozích nastavení knihovny před úpravou.
Example
Zašifrovat sešit
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)Dešifrovat sešit
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.")Otevřít šifrovaný soubor přímo
Example Workbook konstruktor také přijímá password parametr pro otevření šifrovaných souborů bez samostatného dešifrovacího kroku:
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!")