PDF encryption
PDF encryption protects the contents of a file, not its structure. Strings and streams are encrypted; object numbers, dictionary keys, page trees and the cross-reference table stay in the clear, so a reader can still find its way around a document it cannot yet read. Two passwords can be set, and the relationship between them is the single most misunderstood thing about the feature: both unlock the same key, and the permissions that the "owner" password is supposed to guard are a request to the viewer, not a cryptographic barrier.
This page follows the standard security handler as ISO 32000-1 defines it, shows the dictionaries qpdf writes, and ends with the tools.
What encryption covers#
ISO 32000-1 §7.6.1 states the scope exactly. Encryption "applies to all strings and streams in the document's PDF file", with three exceptions: the /ID strings in the trailer, the strings inside the encryption dictionary itself, and strings that sit inside already-encrypted streams. Integers, booleans, names, arrays and dictionaries are never encrypted, "which are used primarily to convey information about the document's structure rather than its contents". The design goal was random access: a viewer should be able to open page 400 without decrypting pages 1 to 399.
The presence of an /Encrypt entry in the trailer is what makes a file encrypted. Its value is the encryption dictionary, whose common entries (Table 20) are:
/Filter- The security handler:
/Standardfor password-based encryption, which every reader must support;/Adobe.PubSecfor certificate-based encryption. /V- Algorithm code. 1: RC4 with a 40-bit key. 2 (PDF 1.4): RC4 with longer keys. 3: an unpublished algorithm that shall not appear in conforming files. 4 (PDF 1.5): the handler defines the method through crypt filters. 5: AES-256 (defined outside ISO 32000-1, see below).
/Length- Key length in bits for V 2 and 3, a multiple of 8 from 40 to 128. Default 40.
/CF,/StmF,/StrF- With V 4 or 5: the crypt filter dictionaries, and which one applies by default to streams and to strings. The method is given by each filter's
/CFM:/V2(RC4),/AESV2(AES-128),/AESV3(AES-256).
The standard handler adds its own entries (Table 21): /R, the revision that says how to interpret the rest; /O and /U, 32-byte strings derived from the owner and user passwords; /P, the permission flags; and /EncryptMetadata, which lets the XMP metadata stream stay readable in an otherwise encrypted file so that search tools can index it.
Here is the dictionary qpdf 12 wrote when asked for AES-256 with a user password and an owner password, as printed by qpdf --json=2:
"obj:6 0 R": {
"/Filter": "/Standard",
"/V": 5,
"/R": 6,
"/Length": 256,
"/CF": { "/StdCF": { "/AuthEvent": "/DocOpen", "/CFM": "/AESV3", "/Length": 32 } },
"/StmF": "/StdCF",
"/StrF": "/StdCF",
"/P": -3376,
"/O": "b:6e50e52d272d65f9fe…", "/U": "b:158b21b3409a8e7433…",
"/OE": "b:8322c623f9a4fd162a…", "/UE": "b:ec4b0ada28fd6ae5f0…",
"/Perms": "b:5324b5765921384a9c…"
}And the trailer that points at it, with the /ID array that feeds the key derivation:
trailer << /Encrypt 6 0 R /ID [<a60cc34fbe1d68a51c18caa59f5985e7> <a60cc34fbe1d68a51c18caa59f5985e7>] /Root 1 0 R /Size 7 >>The page's content stream, which in the unencrypted original reads BT /F1 24 Tf 72 770 Td (Hello, PDF.) Tj ET, now begins f234f2e21ebd8a748938140a1401a9e7 …: the first 16 bytes are the AES initialisation vector, the rest is ciphertext. The dictionary around it is untouched.
Two passwords, one key#
The standard handler (§7.6.3.1) allows "an owner password and a user password". Opening a document, a reader first tries the empty user password; if that fails it asks for one, and either password opens the file. The difference is what the reader should allow afterwards: the owner password "should allow full (owner) access", including changing passwords and permissions; the user password (or no password) should allow only the operations listed in /P.
The key derivation makes the limitation plain. For revisions 2 to 4, Algorithm 2 computes the file key from the padded user password, the /O string, the /P value and the first /ID string, through MD5 (iterated 50 times for revision 3 and later). The owner password is used only to compute /O, which is an RC4-encrypted copy of the padded user password: an owner who types the owner password recovers the user password from /O and proceeds as a user. Revision 6 replaces the arithmetic with SHA-256 based hashing and two intermediate keys (/UE, /OE) wrapped with AES, but the structure is the same: both passwords unwrap one file key.
ISO 32000-1 says so itself, in §7.6.3.1: "Once the document has been opened and decrypted successfully, a conforming reader technically has access to the entire contents of the document. There is nothing inherent in PDF encryption that enforces the document permissions specified in the encryption dictionary. Conforming readers shall respect the intent of the document creator by restricting user access". Respecting intent is a rule for well-behaved software; qpdf, which is well behaved, will nonetheless remove restrictions from any file it can open, because that is what its author considers the honest implementation.
The permission flags#
/P is a 32-bit integer whose bits are listed in Table 22. Unused bits must be 1, which is why the value prints as a negative number (two's complement). Readers "shall ignore all flags other than those at bit positions 3, 4, 5, 6, 9, 10, 11, and 12".
| Bit | When set, the user may | Since |
|---|---|---|
| 3 | Print the document (at reduced quality if bit 12 is clear, for revision 3 and later) | R2 |
| 4 | Modify the contents, other than the operations covered by bits 6, 9 and 11 | R2 |
| 5 | Copy or otherwise extract text and graphics (revision 3 and later: other than for accessibility, bit 10) | R2 |
| 6 | Add or modify annotations and fill in form fields; with bit 4, create or modify form fields | R2 |
| 9 | Fill in existing form fields and sign, even if bit 6 is clear | R3 |
| 10 | Extract text and graphics for accessibility (screen readers) | R3 |
| 11 | Assemble the document: insert, rotate, delete pages, create bookmarks and thumbnails, even if bit 4 is clear | R3 |
| 12 | Print at full quality (a representation from which a faithful copy could be made) | R3 |
The file qpdf produced above has /P -3376, which it decodes for you:
R = 6
P = -3376
User password = reader
Supplied password is user password
extract for accessibility: allowed
extract for any purpose: allowed
print low resolution: not allowed
print high resolution: not allowed
modify document assembly: not allowed
modify forms: not allowed
modify annotations: not allowed
modify other: not allowed
modify anything: not allowed
stream encryption method: AESv3
string encryption method: AESv3
file encryption method: AESv3Bit 10 deserves a sentence of its own. Clearing it tells screen readers not to extract text, which blocks blind users and nobody else. Accessibility standards such as PDF/UA require it to be set, and there is no legitimate reason to clear it.
The owner-only case#
The common "protected" PDF, the one that opens without a prompt but refuses to print or copy, has an empty user password and an owner password. Every reader can compute the key, because the user password is the empty string and the padding constant is public. The restrictions survive only as long as the software chooses to honour them. qpdf demonstrates this in two commands:
# Encrypt with an owner password only: opens without a prompt, printing and copying disallowed
qpdf --encrypt --owner-password=owner-secret --bits=256 --print=none --extract=n --modify=none -- in.pdf locked.pdf
# Remove the restrictions. No password is supplied; none is needed.
qpdf --decrypt locked.pdf unlocked.pdf
qpdf --show-encryption unlocked.pdf
# File is not encryptedThis is not a flaw in qpdf. It is what the standard describes, and it is why "owner password" restrictions should be understood as a notice ("the author would prefer you did not print this") rather than as protection. If content must not be copied, do not send the content. If a document must not be opened by the wrong person, set a user password, or use certificate encryption, so that the key depends on a secret.
Revisions, from RC4-40 to AES-256#
The /R entry has moved through six revisions. ISO 32000-1 defines 2, 3 and 4; the AES-256 revisions came from Adobe's extension levels and then from ISO 32000-2. The qpdf manual's account of the history is the clearest public one and is followed here.
| R | V | Method | Key | Defined in | Status |
|---|---|---|---|---|---|
| 2 | 1 | RC4 | 40 bits | PDF 1.1; ISO 32000-1 | Breakable by exhaustive search in hours on ordinary hardware. Deprecated in PDF 2.0. |
| 3 | 2 | RC4 | 40 to 128 bits | PDF 1.4; ISO 32000-1 | RC4 itself is weak; the password hashing is MD5-based. Deprecated in PDF 2.0. |
| 4 | 4 | RC4 (/V2) or AES-128 CBC (/AESV2) via crypt filters | 128 bits | PDF 1.5 (crypt filters), PDF 1.6 (AES); ISO 32000-1 | Still common. Deprecated in PDF 2.0. |
| 5 | 5 | AES-256 CBC (/AESV3) | 256 bits | Adobe Extension Level 3 (Acrobat 9) | Never fully specified; a weak password hash (a single SHA-256). qpdf writes it only for compatibility testing. Deprecated. |
| 6 | 5 | AES-256 CBC (/AESV3) | 256 bits | Adobe Extension Level 8; ISO 32000-2 | The current method, with a hardened iterated hash (ISO 32000-2 "Algorithm 2.B"). The only revision PDF 2.0 does not deprecate. |
What "deprecated" buys you is interoperability advice: new files should use R6 unless a specific old reader must open them. Encryption of a file written for Acrobat 5 (R3) or 7 (R4) still works in every current viewer; it is the security that is gone, not the compatibility. A 40-bit RC4 key is small enough to brute-force without the password, and the MD5-based password hashing of R3 and R4 lets an attacker test billions of password candidates per second on a GPU. R6 with a strong password is a different matter: the qpdf manual puts AES-256 with a good password on a par with encrypting the file with gpg or openssl, with the advantage that any PDF viewer can open it.
Crypt filters and selective encryption#
Since PDF 1.5 (§7.6.5), a file can name several crypt filters in /CF and choose per stream. Two uses are standard. /EncryptMetadata false leaves the XMP stream in the clear by giving it the /Identity crypt filter, so that desktop search can index a locked document; and attachments can be encrypted in an otherwise open document (/AuthEvent /EFOpen), so that a cover page is readable but the payload needs a password. Table 26 defines exactly one standard crypt filter name, /Identity, which passes data through unchanged.
The flexibility has a cost. Because the format allows encrypted and unencrypted objects to coexist, a document can be constructed in which attacker-controlled plaintext objects wrap the victim's encrypted content. That is the first of the two PDFex attack classes.
Known weaknesses#
Short keys and weak hashing in R2 to R4, covered above.
Partial encryption (PDFex, 2019). Researchers at Ruhr University Bochum and Münster University of Applied Sciences showed that a PDF can be modified without the password so that, once a victim opens and decrypts it, the plaintext is sent to an attacker: unencrypted objects added to the file use forms, links or JavaScript to exfiltrate the decrypted content. Of 27 viewers tested, 23 were vulnerable. The second class, CBC gadgets, exploits the fact that PDF's AES-CBC has no integrity check: ciphertext can be manipulated to produce chosen plaintext fragments, and all 27 viewers were affected. Fixes landed in most viewers; the structural lesson is that PDF encryption provides confidentiality against a passive reader, not integrity against an active one. A digital signature is the integrity mechanism, and it is a separate feature (see the end of this page).
Removable restrictions, by design, as shown above.
The /ID dependence. Revisions 2 to 4 mix the first /ID string into the key. Tools that regenerate /ID when rewriting a file must therefore re-encrypt; the specification notes that security handlers "are encouraged not to rely on the ID". R6 does not use it.
Certificate-based encryption#
The public-key security handler (§7.6.4, /Filter /Adobe.PubSec, /SubFilter values adbe.pkcs7.s3, adbe.pkcs7.s4 and, with crypt filters, adbe.pkcs7.s5) replaces passwords with recipients. The file key is wrapped for each recipient's X.509 certificate inside a PKCS#7 envelope stored in the /Recipients array, and each envelope also carries that recipient's permission flags. It is the right tool when the set of readers is known and has certificates, and it is rare outside enterprises and government, because few consumer viewers manage certificates well. The same advisory nature of permissions applies.
Encrypting and decrypting with tools#
qpdf (12.4.2 checked for this page). The modern syntax uses flags; the older positional form --encrypt user owner 256 -- is still accepted.
# AES-256 (R6) with a user password and an owner password, no printing or modification
qpdf --encrypt --user-password=reader --owner-password=owner-secret --bits=256 \
--print=none --modify=none -- in.pdf out.pdf
# Inspect
qpdf --show-encryption --password=reader out.pdf
# Decrypt (any password that opens the file suffices)
qpdf --decrypt --password=reader out.pdf plain.pdf
# Legacy methods need an explicit opt-in because they are insecure
qpdf --allow-weak-crypto --encrypt --user-password=reader --owner-password=o --bits=40 -- in.pdf rc4-40.pdfThe permission flags map to options: --print=none|low|full, --modify=none|assembly|form|annotate|all, --extract=n, --accessibility=n (which qpdf ignores for 256-bit files, with a warning, because clearing bit 10 serves no purpose), --annotate=n, --form=n, --assemble=n.
pypdf (Python; checked against the pypdf documentation, not executed here). AES needs the cryptography package installed. Omitting algorithm falls back to RC4, so always state it.
from pypdf import PdfReader, PdfWriter
reader = PdfReader("in.pdf")
writer = PdfWriter(clone_from=reader)
writer.encrypt(user_password="reader", owner_password="owner-secret", algorithm="AES-256")
writer.write("out.pdf")
# Decrypting
reader = PdfReader("out.pdf")
if reader.is_encrypted:
reader.decrypt("reader")
print(reader.pages[0].extract_text())pdfcpu (Go, command line) defaults to AES-256 with all permissions withheld, and requires an owner password:
# AES-256, owner password required, user password recommended; --mode rc4 and --key 40|128 exist for legacy readers
pdfcpu encrypt --upw reader --opw owner-secret in.pdf out.pdf
pdfcpu decrypt --upw reader out.pdf plain.pdfpdf-lib (JavaScript) has no encryption support in version 1.17.1: it can neither produce an encrypted file nor decrypt one. Its ignoreEncryption load option only suppresses the error; the streams it then hands you are still ciphertext. For JavaScript, use pdf.js to read encrypted files and a server-side tool such as qpdf to write them.
Signatures are a different mechanism#
A digital signature does not encrypt anything. It stores a cryptographic hash of a byte range of the file, signed with the signer's private key, inside a signature dictionary, and relies on incremental updates so that later changes append after the signed range instead of invalidating it. Encryption answers "who can read this"; a signature answers "who produced this and has it changed". Documents that need both are encrypted and signed, in that order of concern, and ISO 32000-1 covers signatures in §12.8. This site will cover them on their own page.
Inspecting encryption from the command line#
qpdf --show-encryption file.pdf # revision, method, permissions (prompts for nothing if user password is empty)
qpdf --json=2 --password=... file.pdf | grep -A20 '"/Filter": "/Standard"' # the raw encryption dictionary
qpdf --is-encrypted file.pdf; echo $? # exit code 0 if encryptedThe PDF inspector on this site reports whether a file is encrypted, the revision and method, and the permission flags, without uploading the file.
Sources
- ISO 32000-1:2008, §7.6 Encryption (§7.6.1 General and Table 20, §7.6.2 General encryption algorithm, §7.6.3 Standard security handler with Tables 21 and 22 and Algorithm 2, §7.6.4 Public-key security handlers, §7.6.5 Crypt filters with Tables 25 and 26) PDF 1.7, distributed free of charge by Adobe
- PDF Association, ISO 32000-2:2020 errata and issues, clause 7 (revision 6, AESV3, deprecation of revisions 1 to 5) the ISO 32000-2 text itself is paywalled
- qpdf manual, "PDF Encryption" (revision history, R5 status, why restrictions cannot be enforced)
- qpdf manual, command-line reference (--encrypt, --decrypt, --show-encryption)
- Müller, Ising, Mladenov, Mainka, Schinzel, Schwenk, "How to break PDF Encryption" (PDFex), 2019
- pypdf documentation, "Encryption and Decryption of PDFs"
- pdfcpu documentation, encrypt