تشفير ملفات إكسل باستخدام Node.js عبر C++

استخدام Microsoft Excel

لضبط إعدادات تشفير الملف في Microsoft Excel (هنا Microsoft Excel 2003):

  1. من قائمة الأدوات، حدد الخيارات. ستظهر نافذة حوارية. ١. حدد علامة التبويب الأمان.
  2. أدخل كلمة مرور وانقر متقدم ١. اختر نوع التشفير وقم بتأكيد كلمة المرور.

التشفير باستخدام Aspose.Cells for Node.js via C++

المثال التالي يوضح كيفية تشفير وحماية ملف Excel بكلمة مرور باستخدام واجهة برمجة التطبيقات Aspose.Cells.

const path = require("path");
const AsposeCells = require("aspose.cells.node");

// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
const filePath = path.join(dataDir, "Book1.xls");

// Instantiate a Workbook object.
// Open an excel file.
const workbook = new AsposeCells.Workbook(filePath);

// Specify XOR encryption type.
workbook.setEncryptionOptions(AsposeCells.EncryptionType.XOR, 40);

// Specify Strong Encryption type (RC4, Microsoft Strong Cryptographic Provider).
workbook.setEncryptionOptions(AsposeCells.EncryptionType.StrongCryptographicProvider, 128);

// Password protect the file.
workbook.getSettings().setPassword("1234");

// Save the excel file.
workbook.save(path.join(dataDir, "encryptedBook1.out.xls"));

تحديد كلمة المرور لخيار تعديل

المثال التالي يوضح كيفية ضبط خيار Microsoft Excel كلمة المرور للتعديل لملف موجود باستخدام واجهة برمجة التطبيقات Aspose.Cells.

const path = require("path");
const AsposeCells = require("aspose.cells.node");

// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
const filePath = path.join(dataDir, "Book1.xls");

// Instantiate a Workbook object.
// Open an excel file.
const workbook = new AsposeCells.Workbook(filePath);

// Set the password for modification.
workbook.getSettings().getWriteProtection().setPassword("1234");

// Save the excel file.
workbook.save(path.join(dataDir, "SpecifyPasswordToModifyOption.out.xls"));

تحقق من كلمة المرور للملف المُشفر

لتحقق من كلمة مرور الملف المشفر، يوفر Aspose.Cells for Node.js via C++ طريقة FileFormatUtil.verifyPassword(Uint8Array, string). تقبل هذه الطرق معاملين، تيار الملف وكلمة المرور التي يجب التحقق منها. يوضح مقتطف الشيفرة التالي استخدام الطريقة FileFormatUtil.verifyPassword(Uint8Array, string) للتحقق مما إذا كانت كلمة المرور المقدمة صالحة أم لا.

const path = require("path");
const AsposeCells = require("aspose.cells.node");

// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
const filePath = path.join(dataDir, "EncryptedBook1.xlsx");

// Create a Stream object
const fs = require("fs");
const fstream = fs.readFileSync(filePath);

const isPasswordValid = AsposeCells.FileFormatUtil.verifyPassword(fstream, "1234");

console.log("Password is Valid: " + isPasswordValid);

تشفير/فك تشفير ملف ODS بـ Aspose.Cells

يسمح Aspose.Cells بتشفير وفك تشفير ملف ODS. يمكن فتح ملف ODS المفكوك في كل من Excel وOpenOffice، ولكن لا يمكن فتح ملف ODS المشفر إلا بواسطة OpenOffice بعد إدخال كلمة المرور. لا يمكن لExcel فتح ملف ODS المشفر وقد يظهر رسالة تحذير. خيارات التشفير غير قابلة للتطبيق على ملفات ODS على عكس أنواع الملفات الأخرى. لتشفير ملف ODS، قم بتحميل الملف وضبط قيمة WorkbookSettings.getPassword() على كلمة المرور الفعلية قبل حفظه. يمكن فتح ملف ODS المشفر الناتج فقط في OpenOffice.

const path = require("path");
const AsposeCells = require("aspose.cells.node");

// The path to the documents directory.
const sourceDir = RunExamples.Get_SourceDirectory();
// Output directory
const outputDir = RunExamples.Get_OutputDirectory();

// Open an ODS file
const workbook = new AsposeCells.Workbook(path.join(sourceDir, "sampleODSFile.ods"));

// Password protect the file
workbook.getSettings().setPassword("1234");

// Save the ODS file
workbook.save(path.join(outputDir, "outputEncryptedODSFile.ods"));

لفك تشفير ملف ODS، قم بتحميل الملف عن طريق تقديم كلمة مرور في LoadOptions.getPassword(). بمجرد تحميل الملف، ضبط السلسلة WorkbookSettings.getPassword() على القيمة الخالية.

const path = require("path");
const AsposeCells = require("aspose.cells.node");

// The path to the documents directory.
const sourceDir = path.join(__dirname, "data");
// Output directory
const outputDir = path.join(__dirname, "output");

// Open an encrypted ODS file
const loadOptions = new AsposeCells.LoadOptions(AsposeCells.LoadFormat.Ods);

// Set original password
loadOptions.setPassword("1234");

// Load the encrypted ODS file with the appropriate load options
const workbook = new AsposeCells.Workbook(path.join(sourceDir, "sampleEncryptedODSFile.ods"), loadOptions);

// Set the password to null
workbook.getSettings().setPassword(null);

// Save the decrypted ODS file
workbook.save(outputDir + "outputDecryptedODSFile.ods");