Birch UI v0.1
Pricing

Password-protected workbooksSpreadsheet

Save xlsx files that cannot be read without a password, open encrypted files from Excel and LibreOffice, handle wrong and missing passwords, and see which encryption schemes are supported.

Open .md

An xlsx file can be encrypted with a password, which Excel calls Encrypt with Password. Without the password the file cannot be read at all: not the cells, not the sheet names. Birch UI Spreadsheet writes and opens such files, in the browser or in Node, with no server involved.

This is different from sheet and workbook protection. Protection locks the user interface: it stops people from editing locked cells or renaming sheets, but the file is an ordinary xlsx and anyone who has it can read every value. Use protection to prevent accidental edits and encryption to keep the contents secret. The two can be combined.

Saving with a password#

TypeScript
const bytes = await sheet.exportXlsx({ password: 'board-pack-2026' });

Without the component, pass the same option to jsonToXlsx():

scripts/encrypt-report.ts
import { writeFile } from 'node:fs/promises';
import { jsonToXlsx } from '@birchui/spreadsheet';

const bytes = await jsonToXlsx(workbook, { password: process.env.REPORT_PASSWORD });
await writeFile('reports/q3-salaries.xlsx', bytes);

An empty or missing password writes an ordinary, unencrypted file. downloadXlsx() takes no options; to download an encrypted file, call exportXlsx({ password }) and save the bytes yourself.

The file is written with ECMA-376 Agile encryption, the scheme current versions of Excel use:

Property Value
Cipher AES-256 in CBC mode
Hash SHA-512
Key derivation 100,000 iterations of SHA-512 over the salted password. encryptionSpinCount in jsonToXlsx() changes the number.
Salts and keys Random for every save, so two saves of the same workbook with the same password differ.
Integrity An HMAC-SHA-512 over the encrypted package, checked when the file is opened.
Container A compound file (CFB), written by the package itself, with the EncryptionInfo, EncryptedPackage and DataSpaces streams.

The strength of the result depends on the password. The iteration count slows down guessing; it does not make a short password safe. Lowering encryptionSpinCount makes saving and opening faster and guessing cheaper, so leave it alone outside tests.

Opening an encrypted file#

TypeScript
await sheet.importXlsx(file, { password: 'board-pack-2026' });
TypeScript
import { xlsxToJSON } from '@birchui/spreadsheet';

const workbook = await xlsxToJSON(bytes, { password });

xlsxToJSONWithReport() and inspectXlsx() take the same option. These schemes open:

Scheme Written by Supported
Agile encryption Excel 2010 and later, and this package AES with 128, 192 or 256-bit keys in CBC mode, with SHA-1, SHA-256, SHA-384 or SHA-512
Standard encryption Excel 2007, LibreOffice AES with SHA-1. Read only.

A password given for a file that is not encrypted is ignored.

Asking for the password#

isEncryptedPackage() tells you whether a file needs a password before you try to open it, and describeEncryption() says how it is encrypted. Neither needs the password. Both take a Uint8Array.

TypeScript
import { describeEncryption, isEncryptedPackage } from '@birchui/spreadsheet/encryption';

const bytes = new Uint8Array(await file.arrayBuffer());

if (isEncryptedPackage(bytes)) {
  console.info(describeEncryption(bytes));
  // { kind: 'agile', cipher: 'AES', keyBits: 256, hash: 'SHA-512', spinCount: 100000 }
}

describeEncryption() returns { kind: 'agile' | 'standard', cipher, keyBits, hash, spinCount }, or undefined for a file that is not encrypted.

Errors#

A failed open rejects with an Error whose name is 'EncryptionError' and whose code says what went wrong:

code Meaning
passwordRequired The file is encrypted and no password was given.
wrongPassword The password does not open the file. Passwords are case sensitive.
unsupported The file uses a scheme that is not supported, or the Web Crypto API is not available.
corrupt The container or the encryption data cannot be read, or the integrity check failed, which means the file was changed after it was encrypted.

A legacy .xls file is also a compound file, but it is not an encrypted xlsx. It is opened as a legacy workbook without asking for a password; an .xls file that has a password of its own is refused with an XlsError.

A prompt that asks again until the password is right:

TypeScript
import type { EncryptionError } from '@birchui/spreadsheet';

async function openWithPrompt(file: File): Promise<void> {
  let password: string | undefined;
  for (;;) {
    try {
      await sheet.importXlsx(file, { password });
      return;
    } catch (error) {
      const code = (error as EncryptionError).name === 'EncryptionError' ? (error as EncryptionError).code : undefined;
      if (code !== 'passwordRequired' && code !== 'wrongPassword') throw error;
      const answer = window.prompt(code === 'wrongPassword' ? 'Wrong password. Try again:' : 'Password:');
      if (answer === null) return;
      password = answer;
    }
  }
}

In the component each failed attempt also emits an error event with the code import, and the workbook that was open stays as it was.

Requirements and speed#

Encryption uses the Web Crypto API (crypto.subtle). Browsers provide it only in a secure context: https: pages and localhost. On a plain http: page, opening or saving an encrypted file fails with the code unsupported. In Node it needs version 20 or later.

Deriving the key from the password is the slow part, by design. Measured on Node 20 on an Intel Core i9-14900K, it takes about 0.64 s for each open and each save with the default 100,000 iterations. Encrypting or decrypting a 5 MB package adds only a few milliseconds on top. On slower hardware the key derivation takes proportionally longer. It runs through asynchronous Web Crypto calls, and the functions work in a Web Worker.

How this is verified#

  • LibreOffice 26.8 opens workbooks encrypted by this package when given the password, through its UNO interface. This is checked with a 3-row and a 6,000-row workbook, comparing the values and the total LibreOffice reads.
  • The package opens a workbook that LibreOffice encrypted. LibreOffice writes Standard encryption, so this covers the older scheme.
  • msoffcrypto-tool 6.1, an independent Python implementation, decrypts workbooks encrypted by this package (a 3-row and a 6,000-row workbook) with the password and the integrity check on, and the result holds the same workbook as the unencrypted export. It rejects a wrong password.
  • The package opens a workbook that msoffcrypto-tool encrypted with Agile encryption (AES-256, SHA-512, 100,000 iterations), checks its integrity value, and rejects a wrong password and a file with one bit flipped. A file written this way is part of the normal test suite. msoffcrypto-tool writes packages under 4,096 bytes that fail its own integrity check, so the check uses larger workbooks.
  • olefile, a Python reader for compound files, parses the container the package writes. This check is not part of the automated script.
  • The unit tests cover packages of several sizes around the 4,096-byte segment boundary, passwords with non-ASCII characters, a wrong and a missing password, and a file with one bit flipped, which is rejected as corrupt.

Encrypted files are not verified in Microsoft Excel. The LibreOffice and msoffcrypto-tool checks run with node packages/spreadsheet/scripts/validate-xlsx.mjs, together with the xlsx checks. The msoffcrypto-tool checks are skipped unless python3, or the interpreter named in BIRCHUI_MSOFFCRYPTO_PYTHON, can import msoffcrypto.

Not supported#

  • The legacy RC4 and XOR protection of .xls files and of very old xlsx writers. A password-protected .xls file is refused.
  • Encryption of OpenDocument files. exportOds() has no password option and an encrypted .ods file does not open.
  • Certificate-based key encryptors. Only password-based encryption is read and written.
  • Writing Standard encryption. Files are always written with Agile encryption.

↑ ↓ to move, Enter to open, Esc to close