Encryption

Encryption — Aspose.Cells FOSS for Python API Reference

aspose-cells-foss 支持通过模块级函数对 XLSX 文件进行 AES 加密和解密 encrypt_xlsxdecrypt_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_128128
AES_192192
AES_256256

Properties

PropertiesPropertiesProperties
algorithm_namestrProperties "AES".
key_bitsintAES 密钥长度(位),可为 128、192 或 256。.
alg_idint在 OOXML 加密记录中使用的数值算法标识符。.
key_bytesint密钥长度(字节)(key_bits // 8).
block_sizeintProperties 16 (AES 块大小,单位为字节)。.

HashAlgorithm

HashAlgorithm 是一个 IntEnum 指定用于 PBKDF2 密钥派生的哈希函数的。.

Properties哈希字节alg_id
SHA120
SHA25632
SHA38448
SHA51264

Properties

PropertiesPropertiesProperties
algorithm_namestr哈希函数名称(例如,., "SHA-512").
hash_bytesint摘要长度(字节)。.
alg_idint用于 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
)
PropertiesPropertiesPropertiesProperties
cipher_algorithmCipherAlgorithmAES_256要使用的 AES 密钥长度。.
hash_algorithmHashAlgorithmSHA512用于 PBKDF2 密钥派生的哈希函数。.
spin_countint100000PBKDF2 迭代次数。更高的数值会提升抗暴力破解能力,但会增加打开/保存的时间。.

Properties

PropertiesPropertiesProperties
encryption_typestrProperties "agile".
cipher_algorithmCipherAlgorithm已配置的密码算法。.
hash_algorithmHashAlgorithm已配置的哈希函数。.
spin_countintPBKDF2 迭代计数。.
salt_sizeint盐的长度(字节),来源于密码块大小。.
block_sizeint密码块大小(字节)(始终 16 针对 AES)。.
key_bitsint密钥长度(位)。.

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.

PropertiesPropertiesProperties
input_pathstr源 XLSX 文件的路径。.
output_pathstr加密文件的目标路径。可能与 input_path 用于就地加密的相同。.
passwordstr加密密码。.
encryption_params`AgileEncryptionParametersStandardEncryptionParameters

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

另见

 中文