A workbook that is opened with a password is not a ZIP archive on disk. It is a compound file
([MS-CFB]) that holds the real package, enciphered, in a stream. The structures that
describe the encryption are defined in [MS-OFFCRYPTO] (Office Document Cryptography
Structure, v20260217), section 2.1 (data spaces) and section 2.3.4 (ECMA-376 document encryption).
The same container is used for .xlsm, .xltx, .docx and
.pptx. The cipher is chosen by Excel, not by the file name, so nothing in the
extension tells an encrypted package from a plain one; the first bytes do (a compound file
signature instead of PK).
The compound file
An encrypted package has these parts in the compound file (2.1.1, 2.3.4):
| Name | Kind | Holds |
|---|---|---|
EncryptionInfo | stream | Which scheme is used and everything needed to turn a password into the key: salts, hash, cipher, verifier. Its first four bytes are a version (vMajor, vMinor, two bytes each) that selects the layout (2.3.4.5, 2.3.4.10). |
EncryptedPackage | stream | An 8-byte plain-text size (StreamSize, unsigned, little-endian) followed by the enciphered ZIP package. The deciphered bytes are cut to StreamSize; the enciphered data is padded to the cipher block size (2.3.4.4). |
\x06DataSpaces | storage | The data spaces structure of 2.1 (below). It is present so that other software can see what transforms were applied; the key material is not in it. |
Data spaces (2.1)
The storage \x06DataSpaces is a general mechanism for stating which transforms (encryption,
rights management, compression) were applied to which streams of a compound file. For a plain
password-encrypted package every part is fixed by 2.3.4.1 to 2.3.4.3:
| Item | Structure | Required content |
|---|---|---|
\x06DataSpaces\Version | DataSpaceVersionInfo (2.1.5) | Feature identifier Microsoft.Container.DataSpaces as a UNICODE-LP-P4 string (a 4-byte byte length, UTF-16 data, two zero bytes of padding if needed to reach a multiple of 4: 2.1.2), then reader, updater and writer versions, each a Version (2.1.4) that must be 1.0. A reader that does not understand the major version must not read the protected content. |
\x06DataSpaces\DataSpaceMap | DataSpaceMap (2.1.6) | HeaderLength 8, EntryCount 1: one DataSpaceMapEntry (2.1.6.1) whose data space name is StrongEncryptionDataSpace and whose single reference component is of type 0 (stream) named EncryptedPackage (2.1.6.2). |
\x06DataSpaces\DataSpaceInfo\StrongEncryptionDataSpace | DataSpaceDefinition (2.1.7) | HeaderLength 8 and exactly one transform reference, StrongEncryptionTransform. References are stored in the reverse of the order in which the transforms were applied. |
\x06DataSpaces\TransformInfo\StrongEncryptionTransform\x06Primary | TransformInfoHeader (2.1.8), then EncryptionTransformInfo (2.1.9) | Transform type 1, ID {FF9A3F03-56EF-4613-BDD5-5A41C1D07246}, name Microsoft.Container.EncryptionTransform, versions 1.0. The EncryptionTransformInfo that follows names the algorithm (UTF-8-LP-P4 such as AES 128), a block size of 16, cipher mode 0 and a reserved value of 4. If it disagrees with the EncryptionInfo stream, the EncryptionInfo stream is authoritative; for Agile encryption its name must be a null string (length 0). |
Strings use two padded length-prefixed forms: UNICODE-LP-P4 (2.1.2, byte length a multiple of 2, UTF-16, no terminator) and UTF-8-LP-P4 (2.1.3, a length of 0 is a null string and a length of 4 is an empty string, each taking 4 bytes).
Standard encryption (EncryptionInfo version 2.2, 3.2 or 4.2)
The oldest form, used by Office 2007. vMajor is 2, 3 or 4 and vMinor is 2
(2.3.4.5). After the version come these structures:
| Field | Size | Meaning |
|---|---|---|
EncryptionHeaderFlags | 4 | Copy of the header flags (2.3.1). fCryptoAPI and fAES are set for AES; fDocProps is set when the document properties are stored outside the encrypted data; fExternal must be clear here. |
EncryptionHeaderSize | 4 | Size of the next structure. |
EncryptionHeader (2.3.2) | variable | Flags, SizeExtra, AlgID, AlgIDHash, KeySize, ProviderType, two reserved words, then CSPName (a null-terminated UTF-16 provider name). AlgID: 0x660E AES-128, 0x660F AES-192, 0x6610 AES-256, 0x6801 RC4. AlgIDHash 0x8004 is SHA-1. ProviderType for AES is 0x18. |
EncryptionVerifier (2.3.3) | variable | SaltSize (16), Salt, EncryptedVerifier (16 bytes), VerifierHashSize (20, the size of SHA-1) and EncryptedVerifierHash (32 bytes for AES, 20 for RC4: only the first VerifierHashSize bytes are used after decrypting). |
Key derivation and decryption (2.3.4.7 to 2.3.4.9)
- The password is taken as UTF-16 little-endian.
H0 = SHA-1(salt + password). - Fifty thousand rounds:
Hn = SHA-1(iterator + Hn-1), whereiteratoris the round number as a 32-bit little-endian integer starting at 0. Hfinal = SHA-1(H50000 + 0x00000000)(the block number 0).- Two 64-byte buffers are filled with
0x36and0x5Cand XORed withHfinal, each is hashed with SHA-1 (X1 and X2), and the key is the firstKeySize / 8bytes of X1 followed by X2. - The verifier is deciphered with that key in AES-ECB mode and hashed with SHA-1; the result must equal the first 20 bytes of the deciphered
EncryptedVerifierHash. This is how a wrong password is detected. - The package is deciphered in AES-ECB mode (no IV, no segments) and cut to
StreamSize.
The structure carries no integrity value: a modified package is not detected by the scheme. RC4 is defined for the Standard form (AlgID 0x6801, verifier hash of 20 bytes) but ECMA-376 packages must not use it.
Agile encryption (EncryptionInfo version 4.4)
Used from Office 2010. The version is 4.4, followed by a 4-byte reserved value that must be
0x40, then an XML document (the encryption descriptor, 2.3.4.10) in the namespace
http://schemas.microsoft.com/office/2006/encryption. Its root encryption has three children:
| Element | Key attributes |
|---|---|
keyData | The cipher of the package itself: saltSize, blockSize, keyBits, hashSize, cipherAlgorithm, cipherChaining, hashAlgorithm and saltValue (base 64). |
dataIntegrity | encryptedHmacKey and encryptedHmacValue (base 64): an HMAC over the whole EncryptedPackage stream, with the key itself enciphered. |
keyEncryptors / keyEncryptor | One or more ways to obtain the package key. The password form (uri http://schemas.microsoft.com/office/2006/keyEncryptors/password) has an encryptedKey with spinCount, saltSize, blockSize, keyBits, hashSize, cipherAlgorithm, cipherChaining, hashAlgorithm, saltValue, encryptedVerifierHashInput, encryptedVerifierHashValue and encryptedKeyValue. A certificate form (.../keyEncryptors/certificate) holds the key enciphered with an X.509 certificate and a certVerifier, which is an HMAC of the certificate under the intermediate key. |
The attribute limits of the schema: saltSize 1 to 65536, blockSize 2 to 4096, keyBits at least 8, hashSize 1 to 65536, spinCount at most 10,000,000. Values: cipherAlgorithm is AES, RC2, RC4 (which must not be used for ECMA-376), DES, DESX, 3DES or 3DES_112; cipherChaining is ChainingModeCBC or ChainingModeCFB; hashAlgorithm is SHA1, SHA256, SHA384, SHA512, MD5, MD4, MD2, RIPEMD-128, RIPEMD-160 or WHIRLPOOL.
Key derivation (2.3.4.11 to 2.3.4.13)
H0 = H(saltValue + password), the password as UTF-16 little-endian andHthe descriptor’s hash.spinCountrounds ofHn = H(iterator + Hn-1)with a 32-bit little-endian iterator from 0.Hfinal = H(Hn + blockKey). The key is its firstkeyBits / 8bytes, padded with0x36if the hash is shorter.- The block key picks the purpose, so that one password yields independent keys:
| Block key (hex) | Used for |
|---|---|
fea7d2763b4b9e79 | Key that deciphers encryptedVerifierHashInput (the random verifier input). |
d7aa0f6d3061344e | Key that deciphers encryptedVerifierHashValue (the hash of that input). |
146e0be7abacd0d6 | Key that deciphers encryptedKeyValue, the package key itself. |
5fb2ad010cb9e1f6 | IV input for the HMAC key (encryptedHmacKey, deciphered with the package key). |
a0677f02b22c8433 | IV input for the HMAC value (encryptedHmacValue). |
The password check is: decipher the verifier input and the verifier value with their keys (IV = the key encryptor’s saltValue, cut or padded with 0x36 to the block size) and compare H(input) with the value. If equal, the password is right and encryptedKeyValue yields the package key.
The package and its integrity value (2.3.4.14, 2.3.4.15)
After the 8-byte size, the data is enciphered in segments of 4096 bytes. Segment i uses the package key, with the IV H(keyData.saltValue + i) (i as 32-bit little-endian) cut or padded to the block size. The last segment may be shorter and is padded. The integrity value is an HMAC (RFC 2104) over the entire EncryptedPackage stream, size field included. Its key is a random byte string of saltSize bytes that is stored enciphered in encryptedHmacKey; the computed HMAC is stored enciphered in encryptedHmacValue. Both are enciphered with the package key, each with an IV made from keyData.saltValue and one of the last two block keys above, and padded with zero bytes to the block size.
Extensible encryption (EncryptionInfo version 3.3 or 4.3)
With vMajor 3 or 4 and vMinor 3 (2.3.4.6) the header flag fExternal is set and the
stream holds an EncryptionData XML element with an Id (the GUID of an encryption provider), a
Url and EncryptionProviderData. The algorithm, key derivation and package layout belong to
that external provider; the specification does not define them, so a reader cannot decrypt such a file
without the provider. The EncryptionTransformInfo values (algorithm name, block size, cipher mode) are likewise
supplied by the provider in this case.
What the explorer does
The implementation is explorer/formats/shared/ooxmlcrypt.js (shared with the DOCX and PPTX
readers) and explorer/formats/xlsx/encrypted.js. The viewer reads only the two top-level
streams EncryptionInfo and EncryptedPackage; the \x06DataSpaces storage is not
interpreted, because the EncryptionInfo stream is authoritative.
| Case | Result |
|---|---|
| Agile 4.4, AES in CBC mode, SHA-1, SHA-256, SHA-384 or SHA-512, AES 128, 192 or 256, spin count up to 10,000,000 | Decrypted. The verifier is checked, the HMAC is verified and its result shown (verified, failed, not stored). |
| Standard 2.2, 3.2 or 4.2, AES-128, AES-192 or AES-256, SHA-1 | Decrypted; the verifier is checked. The scheme stores no integrity value. |
| Standard with RC4 | Described only (cipher, key size and provider are shown); an error says the cipher is not AES. |
| Agile with another cipher, with CFB chaining, or with MD5/MD4/MD2/RIPEMD/WHIRLPOOL hashes | Described only; an error names the unsupported item. (A hash name the reader maps but the platform lacks fails the same way.) |
| Agile with a certificate key encryptor only | Not decrypted: only the password key encryptor is read. |
| Extensible (3.3, 4.3) | Described only. |
| Any other version | An error names the versions that are understood. |
Without a password the window shows the scheme, cipher, hash, spin count, key size, provider and the plain package size. A password typed into the window’s Password box is used for that file and discarded; it is never stored or logged. A wrong password is reported as such, not as damaged data.
Verification
The test parser-test/test-ooxmlcrypt.mjs decrypts three real files from the
msoffcrypto-tool test corpus (MIT licence), whose
password is published in that project’s own tests, and compares the output byte for byte with that project’s
decrypted files: an Agile workbook (example_password.xlsx), an Agile SHA-1 workbook
(ecma376agile_sha1_password.xlsx) and a Standard document (ecma376standard_password.docx). For each it also checks
that a wrong password is refused, and for the two Agile files that the HMAC verifies. All checks pass. The
checksums of the files are recorded in samples/README.md.
Binary-format encryption (2.3.5 to 2.3.7)
RC4 CryptoAPI (2.3.5), RC4 (2.3.6) and XOR obfuscation (2.3.7) belong to the binary formats (.xls, .doc, .ppt), not to a .xlsx package, so a workbook in this format never carries them. The explorer implements all three for BIFF8 workbooks in explorer/formats/xls/crypt.js: the XOR method 1 verifier and key, the RC4 block keys and the CryptoAPI header, tested in parser-test/test-xls-built.mjs on three real files (an XOR file with password abc, an RC4 file and a CryptoAPI file) from the Apache POI corpus.
Write protection (2.4)
A workbook can ask for a password before it may be changed. This is not encryption: the workbook part holds a fileSharing element (ECMA-376-1 18.2.12) and the file is readable without any password. The attributes are readOnlyRecommended, userName and one of two password forms. The legacy form is reservationPassword, the 16-bit verifier of 2.3.7.1 (CreatePasswordVerifier_Method1) as four hex digits. The ISO form is algorithmName, hashValue, saltValue and spinCount (2.4.2.4): H0 is the hash of the salt followed by the UTF-16LE password, each next hash is the hash of the previous one followed by a 32-bit little-endian counter running from 0 to spinCount minus 1, and the last hash is the stored value in base 64. The spin count is at most 10,000,000.
The explorer lists these settings in Features (“Write protection (fileSharing)”) and never prints the hash. The window has no box for this password; the check exists as code (explorer/formats/xlsx/writeprot.js, MD5, SHA-1, SHA-256, SHA-384, SHA-512 and the legacy verifier, and “not checked” for another algorithm) and is run by the test.
Checked against Excel. Microsoft Excel for Mac wrote three workbooks with the write reservation password abc, with the password pässwörd☃, and with read-only recommended alone (research/xlsx/excel/writeprot.applescript). Excel wrote the ISO form with SHA-512, a 16-byte salt and a spin count of 100,000, and the code verifies both passwords from the file and refuses near misses (parser-test/test-xlsx-security.mjs). Excel for Mac wrote no legacy reservationPassword in an .xlsx; that form is tested with the verifier computed from the clause only. A further run (research/xlsx/excel/writeres-r16.applescript, password wr-r16) saved the same workbook as .xlsx (again SHA-512, 100,000 spins) and as .xls: the BIFF FileSharing record (0x005B) holds the 16-bit hash 0xC283 and the user name, which equals the legacy verifier the code computes for that password, and the ISO hash of the .xlsx verifies with the code too.
Digital signatures (2.5)
A signed package has an origin part (relationship type digital-signature/origin from the package root), and from it one relationship of type digital-signature/signature per XML-DSig signature part (OPC 12.2). The explorer reads each signature and shows: the canonicalisation and signature methods; every Reference with its digest method (2.5.2.1 requires #idPackageObject and #idOfficeObject, and the package object’s manifest lists the signed parts); the length of the signature value; each X.509 certificate with serial, issuer, subject and validity, read from the DER; the SignatureTime (OPC 12.2.4.20); the SignatureInfoV1 or V2 children (2.5.2.5, 2.5.2.6: signature text, comments, Office version, signature type); and whether XAdES properties (SigningTime, signing certificate, policy) are present. Nothing is verified: no digest is recomputed and no certificate chain is checked, so the window shows what the part says, not whether the signature is valid.
Excel cannot sign from a script (it needs a certificate and a dialog), so this is tested on a byte-built package with a certificate made by openssl, in parser-test/test-xlsx-security.mjs. The binary-format signatures of 2.5.1 and later (VBA project signatures) are not read.
Sensitivity labels (2.6)
A package root relationship of type .../2020/02/relationships/classificationlabels points to the LabelInfo part, whose root is labelList in the namespace http://schemas.microsoft.com/office/2020/mipLabelMetadata. Each label has id (a GUID, not empty), enabled, method (Standard or Privileged; empty when removed), siteId (a lower-case braced GUID, one label per site), contentBits (Header 1, Footer 2, Watermark 4, Encryption 8, added together) and removed. An extLst may hold ext elements with a uri. The explorer shows these fields as a table, names the marking bits, and reports a problem for each rule above that the part breaks. The label is only a record of the classification; the encryption bit does not mean this file is encrypted.
No label could be written by Excel without a labelling service, so the reader is tested only on byte-built parts (parser-test/test-xlsx-security.mjs), including the empty list and the four rule violations.
Divergence from spec
Divergence from spec. None is recorded for the schemes the explorer decrypts. The specification is [MS-OFFCRYPTO] (ECMA-376 Part 1 only points to it), and the output of the reader equals the decrypted files of msoffcrypto-tool byte for byte on three real files (two Agile, one Standard), with the HMAC verified on both Agile ones. Where the explorer does less than the specification (RC4 Standard encryption, Agile with another cipher, CFB chaining or the older hashes, certificate key encryptors, extensible encryption) it describes the file and reports the item as not decrypted; that is a coverage limit listed under “What the explorer does”, not a different reading of the text. No program was found that decrypts these files differently from the specification. The write-protection hash (2.4.2.4) agrees with the files Excel for Mac wrote, for an ASCII and a non-ASCII password. The signature and label readers follow 2.5.2 and 2.6 and have only byte-built tests; no divergence is recorded, and none could be checked against a program, since no Office build here can create them.
Not described on this page
Known, not yet covered
- Reference: the other encryption forms of [MS-OFFCRYPTO] that are not ECMA-376 package encryption: the information rights management data space (2.2), the binary-format signatures of 2.5.1 (VBA project signatures), the binary write protection of the
.docand.pptformats (2.4.3), MsoDataStore (2.7) and the hash streams of 2.8 to 2.10. The workbook protection attributes are on the settings page. - Viewer: RC4 Standard encryption, non-AES or non-CBC Agile ciphers, the MD2, MD4, MD5, RIPEMD and WHIRLPOOL hashes, certificate key encryptors and extensible encryption are described but not decrypted. No sample of any of them has been found, so none could be tested.
Sources
- [MS-OFFCRYPTO] v20260217, research/xlsb/MS-OFFCRYPTO.txt (and the PDF beside it): section 2.1 (lines 877 to 1234), section 2.3.1 to 2.3.4.15 (lines 1482 to 2411)
- [MS-OFFCRYPTO] 2.3.5 to 2.3.7, 2.4.1 and 2.4.2.4 (write protection), 2.5.2 (signatures) and 2.6 (sensitivity labels), same file; ECMA-376-1 18.2.12 (fileSharing); ECMA-376-2 (OPC) 12.2 (digital signatures)
- msoffcrypto-tool test inputs and outputs (MIT), as recorded in samples/README.md