### Install officecrypto-tool Source: https://github.com/zurmokeeper/officecrypto-tool/blob/main/README.md Install the library using npm, yarn, or pnpm. ```bash npm/yarn/pnpm install officecrypto-tool ``` -------------------------------- ### Install officecrypto-tool Source: https://context7.com/zurmokeeper/officecrypto-tool/llms.txt Install the officecrypto-tool package using npm. ```bash npm install officecrypto-tool ``` -------------------------------- ### Install Dependencies and Run Tests with Jest Source: https://github.com/zurmokeeper/officecrypto-tool/blob/main/README.md Installs project dependencies and runs tests using Jest. Ensure submodules are updated first. ```bash pnpm i pnpm run test ``` -------------------------------- ### Complete Office File Processing Workflow (JavaScript) Source: https://context7.com/zurmokeeper/officecrypto-tool/llms.txt This example demonstrates a full workflow for processing Office files, including reading, checking encryption status, decrypting if necessary, and re-encrypting the output. It requires the file path, password, and output path as arguments. ```javascript const officeCrypto = require('officecrypto-tool'); const fs = require('fs').promises; const path = require('path'); // Process an Office file: check, decrypt if needed, modify, re-encrypt async function processOfficeFile(inputPath, password, outputPath) { // Read the file const input = await fs.readFile(inputPath); // Check if file is encrypted const encrypted = officeCrypto.isEncrypted(input); console.log(`File encrypted: ${encrypted}`); let decryptedContent; if (encrypted) { // Decrypt the file try { decryptedContent = await officeCrypto.decrypt(input, { password }); console.log('File decrypted successfully'); } catch (error) { throw new Error(`Decryption failed: ${error.message}`); } } else { decryptedContent = input; } // Process the decrypted content here (e.g., with xlsx library) // ... // Re-encrypt and save const encryptedOutput = officeCrypto.encrypt(decryptedContent, { password }); await fs.writeFile(outputPath, encryptedOutput); console.log(`Processed file saved to ${outputPath}`); } // Batch process multiple files async function batchDecrypt(directory, password, outputDir) { const files = await fs.readdir(directory); const officeExtensions = ['.xlsx', '.xls', '.docx', '.doc', '.pptx', '.ppt']; for (const file of files) { const ext = path.extname(file).toLowerCase(); if (officeExtensions.includes(ext)) { const inputPath = path.join(directory, file); const outputPath = path.join(outputDir, `decrypted_${file}`); try { const input = await fs.readFile(inputPath); if (officeCrypto.isEncrypted(input)) { const output = await officeCrypto.decrypt(input, { password }); await fs.writeFile(outputPath, output); console.log(`Decrypted: ${file}`); } else { console.log(`Skipped (not encrypted): ${file}`); } } catch (error) { console.error(`Failed to process ${file}: ${error.message}`); } } } } processOfficeFile('input.xlsx', 'password123', 'output.xlsx'); ``` -------------------------------- ### Initialize and Update Git Submodules Source: https://github.com/zurmokeeper/officecrypto-tool/blob/main/README.md Initializes and updates git submodules, necessary for accessing test files. ```bash -> tests/ 1: git submodule init 2: git submodule update ``` -------------------------------- ### encrypt(input, options) Source: https://context7.com/zurmokeeper/officecrypto-tool/llms.txt Encrypts an Office file with password protection using ECMA-376 encryption. ```APIDOC ## encrypt(input, options) ### Description Encrypts an Office file with password protection using ECMA-376 encryption. This synchronous function supports docx, xlsx, and pptx formats. ### Parameters #### Request Body - **input** (Buffer|ArrayBuffer|TypedArray) - Required - The unencrypted file content. - **options** (Object) - Required - Configuration object containing the password and encryption type. - **password** (string) - Required - The password to protect the file (max 255 characters). - **type** (string) - Optional - Encryption type, either 'agile' (default) or 'standard'. ### Response #### Success Response (200) - **output** (Buffer|ArrayBuffer|TypedArray) - The encrypted file contents. ### Request Example ```javascript const output = officeCrypto.encrypt(input, { password: 'mySecretPassword', type: 'standard' }); ``` ``` -------------------------------- ### Encrypt Office File with Password Source: https://github.com/zurmokeeper/officecrypto-tool/blob/main/README.md Encrypts an Office file with a specified password. The input file should be read into a buffer. ```javascript const officeCrypto = require('officecrypto-tool'); const fs = require('fs').promises; //Setting up encrypted files with passwords (async ()=>{ const input = await fs.readFile(`test.xlsx`); const output = officeCrypto.encrypt(input, {password: '123456'}); await fs.writeFile(`standard_out_success.xlsx`, output); })() ``` -------------------------------- ### Encrypt Office File from Uint8Array Source: https://context7.com/zurmokeeper/officecrypto-tool/llms.txt Encrypts an Office file using a Uint8Array as input. Requires the Uint8Array and password. ```javascript // Handle encryption with TypedArray input async function encryptFromUint8Array(uint8Array, password) { const output = officeCrypto.encrypt(uint8Array, { password }); return output; } ``` -------------------------------- ### decrypt(input, options) Source: https://context7.com/zurmokeeper/officecrypto-tool/llms.txt Decrypts a password-protected Office file and returns the unencrypted file contents. ```APIDOC ## decrypt(input, options) ### Description Decrypts a password-protected Office file and returns the unencrypted file contents. This asynchronous function supports all major Office formats including xlsx, docx, pptx (OOXML formats) and xls, doc, ppt (legacy binary formats). ### Parameters #### Request Body - **input** (Buffer|ArrayBuffer|TypedArray) - Required - The encrypted file content. - **options** (Object) - Required - Configuration object containing the password. - **password** (string) - Required - The password used to decrypt the file. ### Response #### Success Response (200) - **output** (Buffer|ArrayBuffer|TypedArray) - The decrypted file contents. ### Request Example ```javascript const output = await officeCrypto.decrypt(input, { password: '123456' }); ``` ``` -------------------------------- ### Decrypt Office File with Password Source: https://github.com/zurmokeeper/officecrypto-tool/blob/main/README.md Decrypts an encrypted Office file using a provided password. Ensure the file is read into a buffer before decryption. ```javascript const officeCrypto = require('officecrypto-tool'); const fs = require('fs').promises; //decrypt a file with a password (async ()=>{ const input = await fs.readFile(`pass_test.xlsx`); const output = await officeCrypto.decrypt(input, {password: '123456'}); await fs.writeFile(`out_success.xlsx`, output); })() ``` -------------------------------- ### isEncrypted(input) Source: https://context7.com/zurmokeeper/officecrypto-tool/llms.txt Checks whether an Office file is password-protected by analyzing the file structure. ```APIDOC ## isEncrypted(input) ### Description Checks whether an Office file is password-protected. This synchronous function analyzes the file structure to detect encryption without requiring a password. It supports both modern OOXML formats (xlsx, docx, pptx) and legacy binary formats (xls, doc, ppt), detecting various encryption schemes including ECMA-376, RC4, RC4 CryptoAPI, and XOR obfuscation. ### Parameters #### Request Body - **input** (Buffer) - Required - The file content to analyze. ``` -------------------------------- ### Encrypt Excel File with Agile Encryption Source: https://context7.com/zurmokeeper/officecrypto-tool/llms.txt Encrypts an Excel file (xlsx) using ECMA-376 Agile encryption, which is the default. Requires the input file content and password. ```javascript const officeCrypto = require('officecrypto-tool'); const fs = require('fs').promises; // Encrypt an Excel file with Agile encryption (default) async function encryptExcelAgile() { const input = await fs.readFile('report.xlsx'); const output = officeCrypto.encrypt(input, { password: '123456' }); await fs.writeFile('protected_report.xlsx', output); console.log('File encrypted with Agile encryption'); } encryptExcelAgile(); ``` -------------------------------- ### Encrypt PowerPoint Presentation Source: https://context7.com/zurmokeeper/officecrypto-tool/llms.txt Encrypts a PowerPoint presentation (pptx) with password protection. Requires the input file content and password. ```javascript // Encrypt a PowerPoint presentation async function encryptPowerPoint() { const input = await fs.readFile('presentation.pptx'); const output = officeCrypto.encrypt(input, { password: 'presentationPass' }); await fs.writeFile('protected_presentation.pptx', output); } encryptPowerPoint(); ``` -------------------------------- ### Decrypt Office File from ArrayBuffer Source: https://context7.com/zurmokeeper/officecrypto-tool/llms.txt Decrypts an Office file using an ArrayBuffer as input, suitable for browser environments. Requires the ArrayBuffer and password. ```javascript // Decrypt using ArrayBuffer input (useful for browser environments) async function decryptFromArrayBuffer(arrayBuffer, password) { const output = await officeCrypto.decrypt(arrayBuffer, { password }); return output; } ``` -------------------------------- ### Encrypt Word Document with Standard Encryption Source: https://context7.com/zurmokeeper/officecrypto-tool/llms.txt Encrypts a Word document (docx) using ECMA-376 Standard encryption. Requires the input file content, password, and specifying type: 'standard'. ```javascript // Encrypt a Word document with Standard encryption async function encryptWordStandard() { const input = await fs.readFile('document.docx'); const output = officeCrypto.encrypt(input, { password: 'mySecretPassword', type: 'standard' }); await fs.writeFile('protected_document.docx', output); console.log('File encrypted with Standard encryption'); } encryptWordStandard(); ``` -------------------------------- ### Decrypt Legacy Word Document with RC4 Encryption Source: https://context7.com/zurmokeeper/officecrypto-tool/llms.txt Decrypts a legacy Word document (doc) protected with RC4 encryption. Requires the file path and password. ```javascript // Decrypt a legacy Word document (doc format with RC4 encryption) async function decryptWordDoc() { const input = await fs.readFile('protected_document.doc'); const output = await officeCrypto.decrypt(input, { password: 'secretpass' }); await fs.writeFile('unlocked_document.doc', output); } decryptWordDoc(); ``` -------------------------------- ### Check if Office File is Encrypted (JavaScript) Source: https://context7.com/zurmokeeper/officecrypto-tool/llms.txt Use the `isEncrypted` function to synchronously check if an Office file is password-protected. This function analyzes the file structure and supports various encryption schemes across modern and legacy formats. It requires the file content as input. ```javascript const officeCrypto = require('officecrypto-tool'); const fs = require('fs').promises; // Check if a file is encrypted before processing async function processFile(filePath) { const input = await fs.readFile(filePath); const encrypted = officeCrypto.isEncrypted(input); if (encrypted) { console.log(`${filePath} is password protected`); return { status: 'encrypted', needsPassword: true }; } else { console.log(`${filePath} is not encrypted`); return { status: 'unprotected', needsPassword: false }; } } // Batch check multiple files async function checkMultipleFiles(filePaths) { const results = []; for (const filePath of filePaths) { const input = await fs.readFile(filePath); results.push({ file: filePath, isEncrypted: officeCrypto.isEncrypted(input) }); } return results; } // Check encryption status of various Office formats async function checkAllFormats() { const files = [ 'spreadsheet.xlsx', // Modern Excel 'legacy_sheet.xls', // Legacy Excel (BIFF8/BIFF5) 'document.docx', // Modern Word 'old_doc.doc', // Legacy Word 'slides.pptx', // Modern PowerPoint 'old_slides.ppt' // Legacy PowerPoint ]; for (const file of files) { try { const input = await fs.readFile(file); const status = officeCrypto.isEncrypted(input); console.log(`${file}: ${status ? 'Encrypted' : 'Not encrypted'}`); } catch (error) { console.log(`${file}: File not found`); } } } processFile('report.xlsx'); ``` -------------------------------- ### Decrypt Excel File with ECMA-376 Agile Encryption Source: https://context7.com/zurmokeeper/officecrypto-tool/llms.txt Decrypts an Excel file (xlsx) protected with ECMA-376 Agile encryption. Requires the file path and password. Handles potential incorrect password errors. ```javascript const officeCrypto = require('officecrypto-tool'); const fs = require('fs').promises; // Decrypt an Excel file (xlsx format with ECMA-376 Agile encryption) async function decryptExcelFile() { try { const input = await fs.readFile('encrypted_report.xlsx'); const output = await officeCrypto.decrypt(input, { password: '123456' }); await fs.writeFile('decrypted_report.xlsx', output); console.log('File decrypted successfully'); } catch (error) { if (error.message === 'The password is incorrect') { console.error('Wrong password provided'); } else { console.error('Decryption failed:', error.message); } } } decryptExcelFile(); ``` -------------------------------- ### Check if Office File is Encrypted Source: https://github.com/zurmokeeper/officecrypto-tool/blob/main/README.md Determines if an Excel file (xls or xlsx format) is encrypted. Returns true if encrypted, false otherwise. Reads the file into a buffer for checking. ```javascript const officeCrypto = require('officecrypto-tool'); const fs = require('fs').promises; //Determine whether excel file is encrypted or not, support xls and xlsx format, encrypted is true, not encrypted is false. (async ()=>{ const input = await fs.readFile(`encrypted_test.xlsx`); const isEncrypted = officeCrypto.isEncrypted(input); // output: true const input1 = await fs.readFile(`not_encrypted_test.xlsx`); const isEncrypted1 = officeCrypto.isEncrypted(input1); // output: false })() ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.