Excel Dateien mit Node.js via C++ verschlüsseln

Verwendung von Microsoft Excel

Um die Dateiverschlüsselungseinstellungen in Microsoft Excel festzulegen (hier Microsoft Excel 2003):

  1. Wählen Sie im Menü Extras die Option Optionen aus. Es wird ein Dialogfeld angezeigt.
  2. Wählen Sie den Tab Sicherheit aus.
  3. Geben Sie ein Passwort ein und klicken Sie auf Erweitert
  4. Wählen Sie den Verschlüsselungstyp aus und bestätigen Sie das Passwort.

Verschlüsselung mit Aspose.Cells for Node.js via C++

Das folgende Beispiel zeigt, wie Sie mit dem Aspose.Cells-API eine Excel-Datei verschlüsseln und kennwortgeschützt machen können.

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

Passwort zum Ändern der Option festlegen

Das folgende Beispiel zeigt, wie Sie die Passwort zum Ändern Microsoft Excel-Option für eine vorhandene Datei mithilfe der Aspose.Cells-API festlegen.

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

Das Passwort der verschlüsselten Datei verifizieren

Um das Passwort der verschlüsselten Datei zu überprüfen, stellt Aspose.Cells for Node.js via C++ die Methode FileFormatUtil.verifyPassword(Uint8Array, string) bereit. Diese Methoden akzeptieren zwei Parameter, den Datei-Stream und das zu überprüfende Passwort. Der folgende Code-Schnipsel zeigt die Verwendung der Methode FileFormatUtil.verifyPassword(Uint8Array, string) zur Überprüfung, ob das angegebene Passwort gültig ist oder nicht.

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);

Verschlüsselung/Entschlüsselung von ODS-Dateien mit Aspose.Cells

Aspose.Cells ermöglicht die Verschlüsselung und Entschlüsselung von ODS-Dateien. Entschlüsselte ODS-Dateien können sowohl in Excel als auch in OpenOffice geöffnet werden, während verschlüsselte ODS-Dateien nur in OpenOffice nach Eingabe des Passworts geöffnet werden können. Excel kann die verschlüsselte ODS-Datei nicht öffnen und zeigt möglicherweise eine Warnmeldung an. Die Verschlüsselungsoptionen sind für ODS-Dateien nicht anwendbar, im Gegensatz zu anderen Dateitypen. Um eine ODS-Datei zu verschlüsseln, laden Sie die Datei und setzen Sie den Wert WorkbookSettings.getPassword() auf das tatsächliche Passwort, bevor Sie sie speichern. Die verschlüsselte ODS-Ausgabedatei kann nur in OpenOffice geöffnet werden.

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

Für das Entschlüsseln einer ODS-Datei laden Sie die Datei, indem Sie ein Passwort in LoadOptions.getPassword() angeben. Sobald die Datei geladen ist, setzen Sie den Wert WorkbookSettings.getPassword() auf null.

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