Encryption — Aspose.Cells FOSS for Python API Reference
aspose-cells-foss podržava AES šifrovanje i dešifrovanje XLSX fajlova putem funkcija na nivou modula encrypt_xlsx i decrypt_xlsx. Dve klase parametara konfigurišu algoritam šifre, algoritam haša i PBKDF2 broj okretaja. Preporučeni podrazumevani je AgileEncryptionParameters sa AES-256 i SHA-512.
: aspose.cells_foss
from aspose.cells_foss import (
CipherAlgorithm, HashAlgorithm,
AgileEncryptionParameters, StandardEncryptionParameters,
encrypt_xlsx, decrypt_xlsx, get_default_encryption_params,
)CipherAlgorithm
CipherAlgorithm je IntEnum koja specificira dužinu AES ključa.
| Bitovi ključa | alg_id | |
|---|---|---|
AES_128 | 128 | — |
AES_192 | 192 | — |
AES_256 | 256 | — |
algorithm_name | str | "AES". |
key_bits | int | Dužina AES ključa u bitovima (128, 192 ili 256). |
alg_id | int | Numerički identifikator algoritma koji se koristi u OOXML zapisu šifrovanja. |
key_bytes | int | Dužina ključa u bajtovima (key_bits // 8). |
block_size | int | 16 (veličina AES bloka u bajtovima). |
HashAlgorithm
HashAlgorithm je IntEnum koja specificira hash funkciju koja se koristi za PBKDF2 derivaciju ključa.
| Hash bajtovi | alg_id | |
|---|---|---|
SHA1 | 20 | — |
SHA256 | 32 | — |
SHA384 | 48 | — |
SHA512 | 64 | — |
algorithm_name | str | Naziv hash funkcije (npr., "SHA-512"). |
hash_bytes | int | Dužina digest-a u bajtovima. |
alg_id | int | Numerički identifikator algoritma koji se koristi u OOXML zapisu enkripcije. |
AgileEncryptionParameters
AgileEncryptionParameters konfiguriše Agile Encryption šemu (ECMA-376, deo 4), koja je podrazumevani i preporučeni format za moderne Excel fajlove (Excel 2010+).
AgileEncryptionParameters(
cipher_algorithm=CipherAlgorithm.AES_256,
hash_algorithm=HashAlgorithm.SHA512,
spin_count=100000
)cipher_algorithm | CipherAlgorithm | AES_256 | Dužina AES ključa koji se koristi. |
hash_algorithm | HashAlgorithm | SHA512 | Hash funkcija za PBKDF2 derivaciju ključa. |
spin_count | int | 100000 | Broj PBKDF2 iteracija. Veće vrednosti povećavaju otpor na napade silom na račun vremena otvaranja/čuvanja. |
encryption_type | str | "agile". |
cipher_algorithm | CipherAlgorithm | Konfigurisani šifrar. |
hash_algorithm | HashAlgorithm | Konfigurisana hash funkcija. |
spin_count | int | Broj PBKDF2 iteracija. |
salt_size | int | Dužina soli u bajtovima (izvedena iz veličine bloka šifre). |
block_size | int | Veličina bloka šifre u bajtovima (uvek 16 za AES). |
key_bits | int | Dužina ključa u bitovima. |
StandardEncryptionParameters
StandardEncryptionParameters konfiguriše standardnu šifarsku shemu za kompatibilnost sa Excel 2007. Preporučuje se AgileEncryptionParameters za nove datoteke.
StandardEncryptionParameters(
cipher_algorithm=CipherAlgorithm.AES_128,
hash_algorithm=HashAlgorithm.SHA1,
spin_count=50000
)Svojstva odražavaju ona od AgileEncryptionParameters. The encryption_type svojstvo je "standard".
Funkcije modula
encrypt_xlsx
encrypt_xlsx(
input_path: str,
output_path: str,
password: str,
encryption_params=None
) -> NoneČita XLSX fajl na input_path, šifrira ga pomoću password koristeći navedeni encryption_params, i zapisuje šifrovani fajl u output_path.
input_path | str | Putanja do izvornog XLSX fajla. |
output_path | str | Odredišna putanja za šifrovani fajl. Može biti ista kao input_path za enkripciju na mestu. |
password | str | Lozinka za šifrovanje. |
encryption_params | `AgileEncryptionParameters | StandardEncryptionParameters |
decrypt_xlsx
decrypt_xlsx(
input_path: str,
output_path: str,
password: str
) -> boolDešifruje XLSX datoteku na input_path koristeći password i zapisuje nešifrovani XLSX u output_path. Vraća True ako je dešifrovanje uspelo, False ako je lozinka bila netačna ili datoteka nije šifrovana.
get_default_encryption_params
get_default_encryption_params() -> AgileEncryptionParametersVraća AgileEncryptionParameters instancu konfigurisanu sa AES_256, SHA512, i spin_count=100000. Koristite ovo da pregledate ili klonirate podrazumevane vrednosti biblioteke pre prilagođavanja.
Enkriptujte radnu svesku
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)Dekriptujte radnu svesku
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.")Otvorite šifrovanu datoteku direktno
Workbook konstruktor takođe prihvata password parametar za otvaranje šifrovanih datoteka bez zasebnog koraka dešifrovanja: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!")