通过Node.js与C++加密Excel文件
Microsoft Excel (97 - 365) 可以让您对电子表格进行加密和密码保护。它使用加密服务提供商(CSP)提供的算法,即一组具有不同属性的加密算法。默认的CSP是’Office 97/2000兼容’或’弱加密(XOR)'。选择适当的加密密钥长度很重要。有些CSP不支持超过40或56位。这被视为弱加密。对于强加密,需要最小128位的密钥长度。而且,Microsoft Windows中还包含提供强加密类型的CSP,例如 ‘Microsoft Strong Cryptographic Provider’。举例来说,128位加密是银行用于与其网上银行系统进行加密连接的加密级别。
Aspose.Cells允许您使用所需的加密类型对Microsoft Excel文件进行加密和密码保护。
使用Microsoft Excel
在Microsoft Excel(例如Microsoft Excel 2003)中设置文件加密设置:
- 从工具菜单中选择选项。会出现一个对话框。
- 选择安全选项卡。
- 输入密码并点击高级。
- 选择加密类型并确认密码。
使用Aspose.Cells for Node.js via C++的加密
下面的示例显示了如何使用Aspose.Cells API对Excel文件进行加密和密码保护。
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"));
指定修改密码选项
下面的示例显示了如何使用Aspose.Cells API为现有文件设置修改密码 Microsoft Excel选项。
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);
Aspose.Cells对ODS文件的加密/解密
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()字符串设置为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");