Encryption — Aspose.Cells FOSS for Python API Reference
aspose-cells-foss 支持通过模块级函数对 XLSX 文件进行 AES 加密和解密 encrypt_xlsx 和 decrypt_xlsx.。两个参数类用于配置密码算法、哈希算法和 PBKDF2 迭代计数。推荐的默认值是 AgileEncryptionParameters 使用 AES-256 和 SHA-512。.
Properties: aspose.cells_foss
from aspose.cells_foss import (
CipherAlgorithm, HashAlgorithm,
AgileEncryptionParameters, StandardEncryptionParameters,
encrypt_xlsx, decrypt_xlsx, get_default_encryption_params,
)CipherAlgorithm
CipherAlgorithm 是一个 IntEnum 指定 AES 密钥长度的。所有变体都使用 CBC 模式的 AES,块大小为 16 字节。.
| Properties | 密钥位数 | alg_id |
|---|---|---|
AES_128 | 128 | — |
AES_192 | 192 | — |
AES_256 | 256 | — |
Properties
| Properties | Properties | Properties |
|---|---|---|
algorithm_name | str | Properties "AES". |
key_bits | int | AES 密钥长度(位),可为 128、192 或 256。. |
alg_id | int | 在 OOXML 加密记录中使用的数值算法标识符。. |
key_bytes | int | 密钥长度(字节)(key_bits // 8). |
block_size | int | Properties 16 (AES 块大小,单位为字节)。. |
HashAlgorithm
HashAlgorithm 是一个 IntEnum 指定用于 PBKDF2 密钥派生的哈希函数的。.
| Properties | 哈希字节 | alg_id |
|---|---|---|
SHA1 | 20 | — |
SHA256 | 32 | — |
SHA384 | 48 | — |
SHA512 | 64 | — |
Properties
| Properties | Properties | Properties |
|---|---|---|
algorithm_name | str | 哈希函数名称(例如,., "SHA-512"). |
hash_bytes | int | 摘要长度(字节)。. |
alg_id | int | 用于 OOXML 加密记录的数值算法标识符。. |
AgileEncryptionParameters
AgileEncryptionParameters 配置 Agile Encryption 方案(ECMA-376,第 4 部分),该方案是现代 Excel 文件(Excel 2010+)的默认且推荐的格式。.
Properties
AgileEncryptionParameters(
cipher_algorithm=CipherAlgorithm.AES_256,
hash_algorithm=HashAlgorithm.SHA512,
spin_count=100000
)| Properties | Properties | Properties | Properties |
|---|---|---|---|
cipher_algorithm | CipherAlgorithm | AES_256 | 要使用的 AES 密钥长度。. |
hash_algorithm | HashAlgorithm | SHA512 | 用于 PBKDF2 密钥派生的哈希函数。. |
spin_count | int | 100000 | PBKDF2 迭代次数。更高的数值会提升抗暴力破解能力,但会增加打开/保存的时间。. |
Properties
| Properties | Properties | Properties |
|---|---|---|
encryption_type | str | Properties "agile". |
cipher_algorithm | CipherAlgorithm | 已配置的密码算法。. |
hash_algorithm | HashAlgorithm | 已配置的哈希函数。. |
spin_count | int | PBKDF2 迭代计数。. |
salt_size | int | 盐的长度(字节),来源于密码块大小。. |
block_size | int | 密码块大小(字节)(始终 16 针对 AES)。. |
key_bits | int | 密钥长度(位)。. |
StandardEncryptionParameters
StandardEncryptionParameters 配置标准加密方案以兼容 Excel 2007。建议使用 AgileEncryptionParameters 用于新文件。.
Properties
StandardEncryptionParameters(
cipher_algorithm=CipherAlgorithm.AES_128,
hash_algorithm=HashAlgorithm.SHA1,
spin_count=50000
)这些属性对应于 AgileEncryptionParameters.。该 encryption_type 属性是 "standard".
模块函数
encrypt_xlsx
encrypt_xlsx(
input_path: str,
output_path: str,
password: str,
encryption_params=None
) -> None读取位于 input_path,的 XLSX 文件,并使用 password 使用指定的 encryption_params, 并将加密文件写入 output_path.
| Properties | Properties | Properties |
|---|---|---|
input_path | str | 源 XLSX 文件的路径。. |
output_path | str | 加密文件的目标路径。可能与 input_path 用于就地加密的相同。. |
password | str | 加密密码。. |
encryption_params | `AgileEncryptionParameters | StandardEncryptionParameters |
decrypt_xlsx
decrypt_xlsx(
input_path: str,
output_path: str,
password: str
) -> bool解密位于的 XLSX 文件 input_path 使用 password 并将明文 XLSX 写入 output_path.。返回 True 如果解密成功,, False 如果密码不正确或文件未加密。.
get_default_encryption_params
get_default_encryption_params() -> AgileEncryptionParameters返回一个 AgileEncryptionParameters 已配置的实例,使用 AES_256, SHA512,,以及 spin_count=100000.。.
Properties
加密工作簿
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)解密工作簿
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.")直接打开加密文件
Properties Workbook 构造函数还接受一个 password 参数,以在不进行单独解密步骤的情况下打开加密文件::
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!")