Encrypting Excel Files with Node.js via C++

Using Microsoft Excel

To set file encryption settings in Microsoft Excel (here Microsoft Excel 2003):

  1. From the Tools menu, select Options. A dialog will appear.
  2. Select the Security tab.
  3. Input a password and click Advanced
  4. Choose the encryption type and confirm the password.

Encryption with Aspose.Cells for Node.js via C++

The following example shows how to encrypt and password protect an excel file using the Aspose.Cells API.

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

Specifying Password to modify Option

The following example shows how to set the Password to modify Microsoft Excel option for an existing file using the Aspose.Cells API.

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

Verify the password of the encrypted file

To verify the password of the encrypted file, Aspose.Cells for Node.js via C++ provides the FileFormatUtil.verifyPassword(Uint8Array, string) method. These methods accept two parameters, the file stream and the password that needs to be verified. The following code snippet demonstrates the use of the FileFormatUtil.verifyPassword(Uint8Array, string) method to verify whether the provided password is valid or not.

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

Encryption/Decryption of ODS file with Aspose.Cells

Aspose.Cells allows you to encrypt and decrypt ODS file. Decrypted ODS file can be opened both in Excel and OpenOffice, however encrypted ODS file can only be opened by OpenOffice after providing the password. Excel cannot open the encrypted ODS file and may raise a warning message. The Encryption options are not applicable for ODS file unlike other file types. For encrypting an ODS file, load the file and set the WorkbookSettings.getPassword() value to the actual password before saving it. The output encrypted ODS file can be opened in OpenOffice only.

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

For decrypting an ODS file, load the file by providing a password in the LoadOptions.getPassword(). Once the file is loaded, set the WorkbookSettings.getPassword() string to 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");