Encryption

Encryption — Aspose.Cells FOSS for Python API Reference

aspose-cells-foss ondersteunt AES-encryptie en -decryptie van XLSX-bestanden via de module‑niveau functies encrypt_xlsx en decrypt_xlsx.Twee parameterklassen configureren het versleutelingsalgoritme, het hash‑algoritme en de PBKDF2‑spin‑telling. De aanbevolen standaard is AgileEncryptionParameters met AES-256 en SHA-512.

ImageRenderOptions: aspose.cells_foss

from aspose.cells_foss import (
    CipherAlgorithm, HashAlgorithm,
    AgileEncryptionParameters, StandardEncryptionParameters,
    encrypt_xlsx, decrypt_xlsx, get_default_encryption_params,
)

CipherAlgorithm

CipherAlgorithm is een IntEnum die de AES‑sleutellengte specificeert. Alle varianten gebruiken AES in CBC-modus met een blokgrootte van 16 byte.

ImageRenderOptionsSleutelbitsalg_id
AES_128128
AES_192192
AES_256256

ImageRenderOptions

ImageRenderOptionsImageRenderOptionsImageRenderOptions
algorithm_namestrImageRenderOptions "AES".
key_bitsintAES-sleutellengte in bits (128, 192 of 256).
alg_idintNumerieke algoritme‑identificatie gebruikt in het OOXML‑encryptierecord.
key_bytesintSleutellengte in bytes (key_bits // 8).
block_sizeintImageRenderOptions 16 (AES‑blokgrootte in bytes).

HashAlgorithm

HashAlgorithm is een IntEnum die de hash‑functie specificeert die wordt gebruikt voor PBKDF2‑sleutelafleiding.

ImageRenderOptionsHashbytesalg_id
SHA120
SHA25632
SHA38448
SHA51264

ImageRenderOptions

ImageRenderOptionsImageRenderOptionsImageRenderOptions
algorithm_namestrNaam van hash‑functie (bijv., "SHA-512").
hash_bytesintDigest‑lengte in bytes.
alg_idintNumerieke algoritme‑identificator die wordt gebruikt in het OOXML‑versleutelingsrecord.

AgileEncryptionParameters

AgileEncryptionParameters configureert het Agile‑versleutelingsschema (ECMA-376, deel 4), dat de standaard‑ en aanbevolen indeling is voor moderne Excel‑bestanden (Excel 2010+).

ImageRenderOptions

AgileEncryptionParameters(
    cipher_algorithm=CipherAlgorithm.AES_256,
    hash_algorithm=HashAlgorithm.SHA512,
    spin_count=100000
)
ImageRenderOptionsImageRenderOptionsImageRenderOptionsImageRenderOptions
cipher_algorithmCipherAlgorithmAES_256Te gebruiken AES‑sleutellengte.
hash_algorithmHashAlgorithmSHA512Hash‑functie voor PBKDF2‑sleutelafleiding.
spin_countint100000Aantal PBKDF2‑iteraties. Hogere waarden verhogen de weerstand tegen brute‑force, ten koste van de open‑/opslagtijd.

ImageRenderOptions

ImageRenderOptionsImageRenderOptionsImageRenderOptions
encryption_typestrImageRenderOptions "agile".
cipher_algorithmCipherAlgorithmGeconfigureerde cipher.
hash_algorithmHashAlgorithmGeconfigureerde hash‑functie.
spin_countintPBKDF2‑iteratietelling.
salt_sizeintZoutlengte in bytes (afgeleid van de cipher‑bloks grootte).
block_sizeintCipher‑bloks grootte in bytes (altijd 16 voor AES).
key_bitsintSleutellengte in bits.

StandardEncryptionParameters

StandardEncryptionParameters configureert het Standard Encryption‑schema voor compatibiliteit met Excel 2007. Geef de voorkeur aan AgileEncryptionParameters voor nieuwe bestanden.

ImageRenderOptions

StandardEncryptionParameters(
    cipher_algorithm=CipherAlgorithm.AES_128,
    hash_algorithm=HashAlgorithm.SHA1,
    spin_count=50000
)

De eigenschappen weerspiegelen die van AgileEncryptionParameters. De encryption_type eigenschap is "standard".


Modulefuncties

encrypt_xlsx

encrypt_xlsx(
    input_path: str,
    output_path: str,
    password: str,
    encryption_params=None
) -> None

Leest het XLSX‑bestand op input_path, versleutelt het met password met gebruik van de gespecificeerde encryption_params, en schrijft het versleutelde bestand naar output_path.

ImageRenderOptionsImageRenderOptionsImageRenderOptions
input_pathstrPad naar het bron-XLSX-bestand.
output_pathstrDoelpad voor het versleutelde bestand. Kan hetzelfde zijn als input_path voor versleuteling op dezelfde locatie.
passwordstrEncryptiewachtwoord.
encryption_params`AgileEncryptionParametersStandardEncryptionParameters

decrypt_xlsx

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

Decodeert het XLSX‑bestand op input_path met behulp van password en schrijft de platte tekst XLSX naar output_path. Retourneert True als decodering geslaagd is, False als het wachtwoord onjuist was of het bestand niet versleuteld was.

get_default_encryption_params

get_default_encryption_params() -> AgileEncryptionParameters

Retourneert een AgileEncryptionParameters instantie geconfigureerd met AES_256, SHA512, en spin_count=100000. Gebruik dit om de standaardinstellingen van de bibliotheek te inspecteren of te klonen voordat je ze aanpast.


ImageRenderOptions

Versleutel een werkmap

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)

Ontsleutel een werkmap

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.")

Open een versleuteld bestand direct

ImageRenderOptions Workbook constructor accepteert ook een password parameter om versleutelde bestanden te openen zonder een aparte decryptiestap:

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

Zie ook

 Nederlands