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.

ExampleBity klíčealg_id
AES_128128
AES_192192
AES_256256

Example

ExampleExampleExample
algorithm_namestrExample "AES".
key_bitsintDélka klíče AES v bitech (128, 192 nebo 256).
alg_idintČíselný identifikátor algoritmu použitý v záznamu šifrování OOXML.
key_bytesintDélka klíče v bajtech (key_bits // 8).
block_sizeintExample 16 (velikost bloku AES v bajtech).

HashAlgorithm

HashAlgorithm je IntEnum specifikující hash funkci používanou pro odvození klíče PBKDF2.

ExampleBajty hashalg_id
SHA120
SHA25632
SHA38448
SHA51264

Example

ExampleExampleExample
algorithm_namestrNázev hash funkce (např., "SHA-512").
hash_bytesintDélka digestu v bajtech.
alg_idintČí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
)
ExampleExampleExampleExample
cipher_algorithmCipherAlgorithmAES_256Délka klíče AES, která se má použít.
hash_algorithmHashAlgorithmSHA512Hashovací funkce pro odvození klíče PBKDF2.
spin_countint100000Počet iterací PBKDF2. Vyšší hodnoty zvyšují odolnost proti brute‑force útokům na úkor doby otevření/uložení.

Example

ExampleExampleExample
encryption_typestrExample "agile".
cipher_algorithmCipherAlgorithmNastavený šifrovací algoritmus.
hash_algorithmHashAlgorithmNastavená hashovací funkce.
spin_countintPočet iterací PBKDF2.
salt_sizeintDélka soli v bajtech (odvozená od velikosti bloku šifry).
block_sizeintVelikost bloku šifry v bajtech (vždy 16 pro AES).
key_bitsintDé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
) -> None

Nač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.

ExampleExampleExample
input_pathstrCesta k zdrojovému souboru XLSX.
output_pathstrCílová cesta pro šifrovaný soubor. Může být stejná jako input_path pro šifrování na místě.
passwordstrŠifrovací heslo.
encryption_params`AgileEncryptionParametersStandardEncryptionParameters

decrypt_xlsx

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

Deš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() -> AgileEncryptionParameters

Vrací 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!")

Viz také

 Čeština