Encryption — Aspose.Cells FOSS for Python API Reference

يدعم aspose-cells-foss تشفير AES وفك تشفير ملفات XLSX عبر وظائف مستوى الوحدة. encrypt_xlsx و decrypt_xlsx. فئتان من المعلمات تُكوِّن خوارزمية التشفير، خوارزمية التجزئة، وعدد دورات PBKDF2. الإعداد الافتراضي الموصى به هو AgileEncryptionParameters مع AES-256 و 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 هو IntEnum تحديد طول مفتاح AES. جميع المتغيرات تستخدم AES في وضع CBC مع حجم كتلة 16 بايت.

Exampleالبتات الرئيسيةalg_id
AES_128128
AES_192192
AES_256256

Example

ExampleExampleExample
algorithm_namestrExample "AES".
key_bitsintطول مفتاح AES بالبتات (128، 192، أو 256).
alg_idintمعرّف الخوارزمية الرقمي المستخدم في سجل تشفير OOXML.
key_bytesintطول المفتاح بالبايت (key_bits // 8).
block_sizeintExample 16 (حجم كتلة AES بالبايت).

HashAlgorithm

HashAlgorithm هو IntEnum يحدد دالة التجزئة المستخدمة لاشتقاق المفتاح عبر PBKDF2.

Exampleبايتات التجزئةalg_id
SHA120
SHA25632
SHA38448
SHA51264

Example

ExampleExampleExample
algorithm_namestrاسم دالة التجزئة (مثالاً،., "SHA-512").
hash_bytesintطول الملخص بالبايت.
alg_idintمعرّف الخوارزمية الرقمي المستخدم في سجل تشفير OOXML.

AgileEncryptionParameters

AgileEncryptionParameters يضبط مخطط التشفير Agile (ECMA-376، الجزء 4)، وهو التنسيق الافتراضي والمُوصى به لملفات Excel الحديثة (Excel 2010+).

Example

AgileEncryptionParameters(
    cipher_algorithm=CipherAlgorithm.AES_256,
    hash_algorithm=HashAlgorithm.SHA512,
    spin_count=100000
)
ExampleExampleExampleExample
cipher_algorithmCipherAlgorithmAES_256طول مفتاح AES المراد استخدامه.
hash_algorithmHashAlgorithmSHA512دالة التجزئة لاشتقاق المفتاح عبر PBKDF2.
spin_countint100000عدد تكرارات PBKDF2. القيم الأعلى تزيد من مقاومة هجمات القوة العمياء على حساب وقت الفتح/الحفظ.

Example

ExampleExampleExample
encryption_typestrExample "agile".
cipher_algorithmCipherAlgorithmالشيفرة المُكوَّنة.
hash_algorithmHashAlgorithmدالة التجزئة المُكوَّنة.
spin_countintعدد تكرارات PBKDF2.
salt_sizeintطول الملح بالبايت (مستمد من حجم كتلة الشيفرة).
block_sizeintحجم كتلة الشيفرة بالبايت (دائمًا 16 لـ AES).
key_bitsintطول المفتاح بالبت.

StandardEncryptionParameters

StandardEncryptionParameters يضبط مخطط التشفير القياسي للتوافق مع Excel 2007. يفضَّل AgileEncryptionParameters للملفات الجديدة.

Example

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

الخصائص تعكس تلك الخاصة بـ AgileEncryptionParameters. الـ encryption_type الخاصية هي "standard".


يدعم aspose-cells-foss إضافة قواعد التحقق من البيانات إلى نطاقات الخلايا. تُدار قواعد التحقق من خلال ws.data_validations، وهو DataValidationCollection المرتبط بكل Worksheet.

encrypt_xlsx

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

يقرأ ملف XLSX الموجود في input_path,، يشفره باستخدام password باستخدام المحدد encryption_params,، ويكتب الملف المشفر إلى output_path.

ExampleExampleExample
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

يُرجع an AgileEncryptionParameters مثيل مُكوَّن بـ AES_256, SHA512,، و spin_count=100000. استخدم هذا لتفقد أو استنساخ الإعدادات الافتراضية للمكتبة قبل التخصيص.


Example

تشفير مصنف

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

افتح ملفًا مشفرًا مباشرة

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

انظر أيضًا

 العربية