An encrypted PDF keeps its structure in the clear and encrypts the contents of its
strings and streams. The objects, the cross-reference and the dictionaries can still be
parsed; only string bytes and stream data are scrambled. The trailer’s
Encrypt names a security handler and its parameters. PDF 1.5 added crypt
filters, which let different streams and strings use different methods; PDF 1.6 added
AES-128; Adobe’s extension level 3 to PDF 1.7 added AES-256, which PDF 2.0 made standard,
with a new password hash. A file with an empty user password opens without asking: its
encryption only carries the permissions.
The encryption dictionary
| Key | Since | Meaning |
|---|---|---|
Filter | 1.1 | Required. The security handler: Standard for passwords, or a public-key handler such as Adobe.PubSec |
SubFilter | 1.3 | The format of the dictionary, so that another handler can read it |
V | 1.1 | The algorithm, below |
Length | 1.4 | For V 2 and 3: the key length in bits, a multiple of 8 from 40 to 128; default 40. 2.0 deprecated |
CF | 1.5 | For V 4 and 5: the crypt filters, by name |
StmF | 1.5 | For V 4 and 5: the crypt filter for streams; default Identity |
StrF | 1.5 | For V 4 and 5: the crypt filter for strings; default Identity |
EFF | 1.6 | For V 4 and 5: the crypt filter for embedded files without one of their own; default StmF |
V | Since | Meaning |
|---|---|---|
| 0 | – | An undocumented algorithm; not to be used |
| 1 | 1.1 | RC4 with a 40-bit key. 2.0 deprecated |
| 2 | 1.4 | RC4 with a key of Length bits. 2.0 deprecated |
| 3 | 1.4 | An unpublished algorithm; not to be used |
| 4 | 1.5 | Crypt filters with keys up to 128 bits: RC4 or AES-128. 2.0 deprecated |
| 5 | 2.0 | Crypt filters with 256-bit keys: AES-256 (earlier as Adobe’s extension level 3 to 1.7) |
The encryption dictionary must not be in an object stream. It is often a direct object in the trailer or cross-reference stream dictionary, or a plain indirect object.
Keys for each object
Every string and stream belongs to an indirect object, and is decrypted with a key made for that object. Up to V 4 (Algorithm 1):
- Take the file key, n bytes long (5 for V 1, otherwise
Length/ 8, or 16 for AESV2). - Append the low three bytes of the object number and the low two bytes of the generation, each low byte first.
- For AES, append the four bytes
73 41 6C 54, “sAlT”. - Take the MD5 hash, and use its first min(n + 5, 16) bytes as the key.
For V 5 (AES-256) there is no per-object key: the 32-byte file key is used for every object. A string or stream inside an object stream is not encrypted on its own, since the whole object stream is. Stream data is encrypted after the filters are applied, so a reader decrypts first and then decodes.
Crypt filters
From V 4, the CF dictionary names crypt filters, and StmF,
StrF and EFF choose which is the default for streams, strings
and embedded files. A stream can choose its own with a
Crypt filter.
The name Identity is built in and means no encryption. The usual name is
StdCF.
| Key | Since | Meaning |
|---|---|---|
Type | 1.5 | CryptFilter |
CFM | 1.5 | The method: None (the handler decrypts, default), V2 (RC4, 2.0 deprecated), AESV2 (AES-128, from 1.6, 2.0 deprecated), AESV3 (AES-256, 2.0; earlier as Adobe’s extension level 3) |
AuthEvent | 1.5 | When to ask for the password: DocOpen (default) or EFOpen, only when an embedded file is opened |
Length | 1.5 | The key length. The standard handler gives it in bytes (16 for AESV2, 32 for AESV3), the public-key handler in bits. 2.0 deprecated |
Recipients, EncryptMetadata | 1.5 | Public-key handler only |
<< /Filter /Standard /V 4 /R 4 /Length 128
/CF << /StdCF << /Type /CryptFilter /CFM /AESV2 /AuthEvent /DocOpen /Length 16 >> >>
/StmF /StdCF /StrF /StdCF
/O <…32 bytes…> /U <…32 bytes…> /P -1084 >>
Only embedded files can be left for later with EFOpen: a file whose
StmF and StrF are Identity and whose
EFF is a real filter has readable pages and encrypted attachments.
AES
Both AES methods use cipher block chaining (CBC) with 16-byte blocks. The first 16 bytes of each encrypted string or stream are a random initialisation vector, and the rest is the ciphertext. Before encrypting, the data is padded to a multiple of 16 with 1 to 16 bytes, each holding the number of bytes added (PKCS#5), so there is always padding, even when the data is already a multiple of 16. A reader drops it after decrypting. An empty string encrypts to 32 bytes.
The standard security handler
The Standard handler uses two passwords. The user password
opens the document. The owner password opens it with every permission. Both
may be empty; a user password that is empty opens the file without asking, and the
permissions are then the only protection, which a reader respects or not.
| Key | Meaning |
|---|---|
R | Required. The revision, below |
O | Required. 32 bytes (48 for R 5 and 6) computed from the owner password |
U | Required. 32 bytes (48 for R 5 and 6) computed from the user password |
OE, UE | R 5 and 6: 32 bytes each, the file key encrypted with a key from the owner or user password |
P | Required. The permissions, a 32-bit integer, written signed |
Perms | R 5 and 6: 16 bytes, the permissions encrypted with the file key |
EncryptMetadata | V 4 and 5 (1.5): whether the document’s XMP metadata stream is encrypted; default true |
R | With V | Since | Encryption | Password hash |
|---|---|---|---|---|
| 2 | 1 | 1.1 | RC4, 40 bits | MD5. 2.0 deprecated |
| 3 | 2 or 3 | 1.4 | RC4, 40 to 128 bits | MD5, 50 rounds. 2.0 deprecated |
| 4 | 4 | 1.5 | Crypt filters: RC4 or AES-128 | As R 3. 2.0 deprecated |
| 5 | 5 | Adobe extension level 3 | AES-256 | SHA-256 once. Weak; 2.0 deprecated |
| 6 | 5 | 2.0 (Adobe extension level 8) | AES-256 | Algorithm 2.B |
Revisions 2 to 4 are deprecated by PDF 2.0, but readers must still open them, since most encrypted files in circulation use them.
The file key, revisions 2 to 4
Algorithm 2, from a password:
- Pad or cut the password to 32 bytes: take up to 32 bytes of it and fill the rest from
the start of this padding string. An empty password is the whole string.
28 BF 4E 5E 4E 75 8A 41 64 00 4E 56 FF FA 01 08 2E 2E 00 B6 D0 68 3E 80 2F 0C A9 FE 64 53 69 7A - Start an MD5 hash of those 32 bytes.
- Add the 32 bytes of
O. - Add
Pas four bytes, low byte first. - Add the first string of the trailer’s
ID. - For R 4, if
EncryptMetadatais false, addFF FF FF FF. - Finish the hash.
- For R 3 and 4, 50 times: hash the first n bytes of the result again.
- The file key is the first n bytes: 5 for R 2,
Length/ 8 otherwise.
Passwords for these revisions are bytes in PDFDocEncoding; characters outside it cannot be used.
Checking the user password, revisions 2 to 4
- R 2 (Algorithm 4): make the file key from the password, encrypt
the 32-byte padding string with RC4 and that key. If the result equals
U, the password is right. - R 3 and 4 (Algorithm 5): make the file key; take the MD5 of the
padding string followed by the first
IDstring; encrypt those 16 bytes with RC4 and the key; then 19 more times with RC4, each time with the key’s bytes XORed with the round number 1 to 19. If the result equals the first 16 bytes ofU, the password is right. The other 16 bytes ofUare arbitrary.
To test whether a file opens without a password, try the empty password. Most encrypted PDFs open this way.
Checking the owner password, revisions 2 to 4
O is the padded user password encrypted with a key made from the owner
password (Algorithm 3). To check an owner password (Algorithm 7):
- Pad it to 32 bytes as above, take the MD5, and for R 3 and 4 hash the result 50 more times. The first n bytes are an RC4 key.
- For R 2, decrypt
Oonce with that key. For R 3 and 4, decrypt it 20 times, with the key XORed with 19, 18 and so on down to 0. - The result is the padded user password. Check it as a user password; if it is right, so is the owner password, and the file key is the one it gives.
If a file has no owner password set, writers use the user password in its place.
Revisions 5 and 6: AES-256
Here the file key is 32 random bytes, stored encrypted in UE and
OE. The passwords are Unicode: a password is prepared with SASLprep
(RFC 4013), encoded in UTF-8 and cut to 127 bytes.
Bytes of U or O | Content |
|---|---|
| 0–31 | A hash of the password and the validation salt (for O, and U) |
| 32–39 | The validation salt, 8 random bytes |
| 40–47 | The key salt, 8 random bytes |
To open the file (Algorithm 2.A):
- Prepare the password as above.
- Owner password: if hash(password,
O[32–39],U) equalsO[0–31], it is the owner password. The intermediate key is hash(password,O[40–47],U), and the file key isOEdecrypted with AES-256 in CBC mode, with a zero IV and no padding. - Otherwise, user password: if hash(password,
U[32–39]) equalsU[0–31], the intermediate key is hash(password,U[40–47]), and the file key isUEdecrypted the same way. - Decrypt
Permswith the file key, AES-256 in ECB mode. Bytes 9 to 11 must beadb; bytes 0 to 3 should matchP, and byte 8 isTorFforEncryptMetadata. A reader that finds a mismatch should treat the file as tampered with.
The hash is SHA-256 of the concatenated inputs for R 5. That proved too fast to resist guessing, and R 6 uses Algorithm 2.B instead:
- K = SHA-256(password + salt + u), where u is the
48-byte
Uwhen checking the owner password and nothing otherwise. - Repeat, counting rounds from 0:
- K1 = (password + K + u) repeated 64 times.
- E = AES-128-CBC encryption of K1, with the first 16 bytes of K as the key and the next 16 as the IV, without padding.
- Take the first 16 bytes of E as an unsigned big-endian number, modulo 3 (the same as the sum of those bytes modulo 3). 0 gives K = SHA-256(E), 1 SHA-384(E), 2 SHA-512(E).
- Stop after at least 64 rounds, at the first round whose last byte of E is not greater than the round number minus 32.
- The hash is the first 32 bytes of K.
To write a file, a program picks the file key and the four salts at random, computes
U and UE (Algorithm 8), then O and OE
using the new U (Algorithm 9), then Perms (Algorithm 10):
P as four bytes low byte first, FF FF FF FF, T or
F, a d b, and four random bytes, encrypted with the file key in
ECB mode.
Permissions
P is a set of bits, bit 1 the lowest. Bits 1 and 2 must be 0, and bits 7,
8 and 13 to 32 must be 1, so P is a negative number when written:
-4 allows everything, -3904 allows nothing (in PDF 2.0, where bit
10 is always set, the least is -3392).
| Bit | When set, allows | Revisions |
|---|---|---|
| 3 | Printing; with R 3 and later, only in low quality unless bit 12 is also set | all |
| 4 | Changing the document, other than by bits 6, 9 and 11 | all |
| 5 | Copying or extracting text and graphics (for R 2, also for accessibility) | all |
| 6 | Adding and changing annotations, and filling in form fields; with bit 4, creating and changing fields | all |
| 9 | Filling in form fields, signature fields included, even when bit 6 is clear | 3 and later |
| 10 | Extracting text and graphics for accessibility. PDF 2.0 requires the bit to be set, so such extraction is always allowed | 3 and later |
| 11 | Assembling: inserting, rotating and deleting pages, making bookmarks and thumbnails | 3 and later |
| 12 | Printing in high quality | 3 and later |
Permissions are a request to the reader, not a lock. The owner password, or a program that ignores them, gives every permission, since the user password alone decrypts the whole file.
What is not encrypted
- Cross-reference streams, which a reader needs before it can decrypt anything.
- The strings in the encryption dictionary itself.
- The strings of the trailer’s
ID, which the key depends on. - Strings inside a stream that is itself encrypted, such as text in content streams and objects in object streams.
- Streams and strings whose crypt filter is
Identity. - The document’s XMP metadata stream, when
EncryptMetadatais false.
Public-key security handlers
A public-key handler, from PDF 1.3, encrypts the file for a list of recipients by
their certificates instead of a password. Filter names the handler, often
Adobe.PubSec, and SubFilter the format:
SubFilter | Since | Meaning |
|---|---|---|
adbe.pkcs7.s3 | 1.3 | Recipients in the encryption dictionary; permissions as revision 2 |
adbe.pkcs7.s4 | 1.4 | The same, with permissions as revision 3 |
adbe.pkcs7.s5 | 1.5 | Crypt filters: recipients in each crypt filter dictionary |
Recipients is an array of PKCS#7 (CMS) enveloped-data objects, one for
each group of recipients with the same permissions. Each holds, encrypted to the
recipients’ public keys, a 20-byte seed and four bytes of permissions. The file key is
the SHA-1 hash (SHA-256 for AES-256) of the seed, the bytes of every
Recipients string, and FF FF FF FF if the metadata is not
encrypted, cut to the key length. From then on objects are decrypted as with the
standard handler.
Encryption in PDF 2.0
PDF 2.0 deprecates RC4 in every form, AES-128 (AESV2), revisions 2 to 5
of the standard handler and the Length entries; a new PDF 2.0 file should
use revision 6 with AESV3. It also requires the trailer’s
ID in every file and adds the unencrypted wrapper document, an ordinary
PDF that carries an encrypted one as an attachment. ISO/TS 32003 extends it with
AES-GCM (AESV4, V 6, R 7), and ISO/TS 32004 adds
integrity protection with a KDFSalt and a message authentication code.