Encryption — Aspose.Cells FOSS for Python API Reference
aspose-cells-foss unterstützt die AES‑Verschlüsselung und -Entschlüsselung von XLSX‑Dateien über die Funktionen auf Modulebene encrypt_xlsx und decrypt_xlsx. Zwei Parameterklassen konfigurieren den Cipher‑Algorithmus, den Hash‑Algorithmus und die PBKDF2‑Spin‑Count. Der empfohlene Standard ist AgileEncryptionParameters mit AES-256 und 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 ist ein IntEnum der die AES‑Schlüssellänge angibt. Alle Varianten verwenden AES im CBC‑Modus mit einer Blockgröße von 16 Byte.
| Example | Schlüsselbits | alg_id |
|---|---|---|
AES_128 | 128 | — |
AES_192 | 192 | — |
AES_256 | 256 | — |
Example
| Example | Example | Example |
|---|---|---|
algorithm_name | str | Example "AES". |
key_bits | int | AES‑Schlüssellänge in Bits (128, 192 oder 256). |
alg_id | int | Numerischer Algorithmus‑Bezeichner, der im OOXML‑Verschlüsselungs‑Record verwendet wird. |
key_bytes | int | Schlüssellänge in Bytes (key_bits // 8). |
block_size | int | Example 16 (AES‑Blockgröße in Bytes). |
HashAlgorithm
HashAlgorithm ist ein IntEnum gibt die für die PBKDF2-Schlüsselableitung verwendete Hash-Funktion an.
| Example | Hash-Bytes | alg_id |
|---|---|---|
SHA1 | 20 | — |
SHA256 | 32 | — |
SHA384 | 48 | — |
SHA512 | 64 | — |
Example
| Example | Example | Example |
|---|---|---|
algorithm_name | str | Name der Hash-Funktion (z. B., "SHA-512"). |
hash_bytes | int | Digest-Länge in Bytes. |
alg_id | int | Numerischer Algorithmus-Identifikator, der im OOXML-Verschlüsselungsdatensatz verwendet wird. |
AgileEncryptionParameters
AgileEncryptionParameters konfiguriert das Agile Encryption‑Schema (ECMA‑376, Teil 4), das das Standard‑ und empfohlene Format für moderne Excel‑Dateien (Excel 2010 +) ist.
Example
AgileEncryptionParameters(
cipher_algorithm=CipherAlgorithm.AES_256,
hash_algorithm=HashAlgorithm.SHA512,
spin_count=100000
)| Example | Example | Example | Example |
|---|---|---|---|
cipher_algorithm | CipherAlgorithm | AES_256 | Zu verwendende AES‑Schlüssellänge. |
hash_algorithm | HashAlgorithm | SHA512 | Hash‑Funktion für die PBKDF2‑Schlüsselableitung. |
spin_count | int | 100000 | Anzahl der PBKDF2‑Iterationen. Höhere Werte erhöhen die Brute‑Force‑Resistenz auf Kosten der Öffnungs‑/Speicherzeit. |
Example
| Example | Example | Example |
|---|---|---|
encryption_type | str | Example "agile". |
cipher_algorithm | CipherAlgorithm | Konfigurierter Cipher. |
hash_algorithm | HashAlgorithm | Konfigurierte Hash-Funktion. |
spin_count | int | PBKDF2-Iterationszahl. |
salt_size | int | Salt-Länge in Bytes (abgeleitet von der Blockgröße des Cipher). |
block_size | int | Cipher-Blockgröße in Bytes (immer 16 für AES). |
key_bits | int | Schlüssellänge in Bits. |
StandardEncryptionParameters
StandardEncryptionParameters konfiguriert das Standardverschlüsselungsschema für die Kompatibilität mit Excel 2007. Bevorzuge AgileEncryptionParameters für neue Dateien.
Example
StandardEncryptionParameters(
cipher_algorithm=CipherAlgorithm.AES_128,
hash_algorithm=HashAlgorithm.SHA1,
spin_count=50000
)Die Eigenschaften spiegeln die von AgileEncryptionParameters. Die encryption_type Eigenschaft ist "standard".
add_validation
encrypt_xlsx
encrypt_xlsx(
input_path: str,
output_path: str,
password: str,
encryption_params=None
) -> NoneLiest die XLSX-Datei unter input_path, verschlüsselt sie mit password unter Verwendung des angegebenen encryption_params, und schreibt die verschlüsselte Datei nach output_path.
| Example | Example | Example |
|---|---|---|
input_path | str | Pfad zur Quell-XLSX-Datei. |
output_path | str | Zielpfad für die verschlüsselte Datei. Kann derselbe sein wie input_path für die In-Place-Verschlüsselung. |
password | str | Verschlüsselungspasswort. |
encryption_params | `AgileEncryptionParameters | StandardEncryptionParameters |
decrypt_xlsx
decrypt_xlsx(
input_path: str,
output_path: str,
password: str
) -> boolEntschlüsselt die XLSX-Datei unter input_path unter Verwendung von password und schreibt die Klartext‑XLSX nach output_path. Gibt zurück True wenn die Entschlüsselung erfolgreich war, False wenn das Passwort falsch war oder die Datei nicht verschlüsselt war.
get_default_encryption_params
get_default_encryption_params() -> AgileEncryptionParametersGibt ein AgileEncryptionParameters Instanz konfiguriert mit AES_256, SHA512, und spin_count=100000. Verwenden Sie dies, um die Bibliotheks‑Standardwerte zu inspizieren oder zu klonen, bevor Sie sie anpassen.
Example
Arbeitsmappe verschlüsseln
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)Arbeitsmappe entschlüsseln
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.")Verschlüsselte Datei direkt öffnen
Example Workbook Konstruktor akzeptiert auch ein password Parameter, um verschlüsselte Dateien ohne separaten Entschlüsselungsschritt zu öffnen:
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!")