Encryption — Aspose.Cells FOSS for Python API Reference

aspose-cells-foss understøtter AES-kryptering og -dekryptering af XLSX-filer via modulniveau-funktionerne encrypt_xlsx og decrypt_xlsx. To parametertklasser konfigurerer krypteringsalgoritmen, hash-algoritmen og PBKDF2 spin-tællingen. Den anbefalede standard er AgileEncryptionParameters med AES-256 og 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 er en IntEnum der angiver AES-nøglelængden. Alle varianter bruger AES i CBC-tilstand med en blokstørrelse på 16 byte.

ExampleNøglebitsalg_id
AES_128128
AES_192192
AES_256256

Example

ExampleExampleExample
algorithm_namestrExample "AES".
key_bitsintAES-nøglelængde i bits (128, 192 eller 256).
alg_idintNumerisk algoritmeidentifikator brugt i OOXML-krypteringsposten.
key_bytesintNøglelængde i byte (key_bits // 8).
block_sizeintExample 16 (AES-blokstørrelse i byte).

HashAlgorithm

HashAlgorithm er en IntEnum der angiver hash-funktionen, der bruges til PBKDF2-nøgleafledning.

ExampleHash-bytesalg_id
SHA120
SHA25632
SHA38448
SHA51264

Example

ExampleExampleExample
algorithm_namestrNavn på hash-funktion (f.eks., "SHA-512").
hash_bytesintDigest-længde i byte.
alg_idintNumerisk algoritmeidentifikator brugt i OOXML‑krypteringsposten.

AgileEncryptionParameters

AgileEncryptionParameters konfigurerer Agile Encryption‑skemaet (ECMA-376, del 4), som er standard‑ og anbefalet format for moderne Excel‑filer (Excel 2010+).

Example

AgileEncryptionParameters(
    cipher_algorithm=CipherAlgorithm.AES_256,
    hash_algorithm=HashAlgorithm.SHA512,
    spin_count=100000
)
ExampleExampleExampleExample
cipher_algorithmCipherAlgorithmAES_256AES‑nøglelængde der skal bruges.
hash_algorithmHashAlgorithmSHA512Hash‑funktion til PBKDF2‑nøgleafledning.
spin_countint100000Antal PBKDF2-iterationer. Højere værdier øger modstand mod brute‑force på bekostning af åbning-/gemningstid.

Example

ExampleExampleExample
encryption_typestrExample "agile".
cipher_algorithmCipherAlgorithmKonfigureret cipher.
hash_algorithmHashAlgorithmKonfigureret hash-funktion.
spin_countintPBKDF2-iterationsantal.
salt_sizeintSaltlængde i bytes (afledt af cipher-blokstørrelse).
block_sizeintCipher-blokstørrelse i bytes (altid 16 for AES).
key_bitsintNøglelængde i bits.

StandardEncryptionParameters

StandardEncryptionParameters konfigurerer Standard Encryption‑skemaet for kompatibilitet med Excel 2007. Foretræk AgileEncryptionParameters for nye filer.

Example

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

Egenskaberne spejler dem fra AgileEncryptionParameters. Den encryption_type egenskab er "standard".


remove_at

encrypt_xlsx

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

Læser XLSX-filen på input_path, krypterer den med password ved brug af den angivne encryption_params, og skriver den krypterede fil til output_path.

ExampleExampleExample
input_pathstrSti til kilde‑XLSX‑filen.
output_pathstrDestinationssti for den krypterede fil. Kan være den samme som input_path for kryptering på stedet.
passwordstrKrypteringsadgangskode.
encryption_params`AgileEncryptionParametersStandardEncryptionParameters

decrypt_xlsx

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

Dekrypterer XLSX-filen på input_path ved hjælp af password og skriver den ukrypterede XLSX til output_path. Returnerer True hvis dekryptering lykkedes, False hvis adgangskoden var forkert, eller filen ikke var krypteret.

get_default_encryption_params

get_default_encryption_params() -> AgileEncryptionParameters

Returnerer en AgileEncryptionParameters instans konfigureret med AES_256, SHA512, og spin_count=100000. Brug dette til at inspicere eller klone bibliotekets standardindstillinger før tilpasning.


Example

Krypter en projektmappe

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)

Dekrypter en projektmappe

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

Åbn en krypteret fil direkte

Example Workbook konstruktøren accepterer også en password parameter til at åbne krypterede filer uden et separat dekrypterings trin:

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

Se også

 Dansk