PDF Password Protection: Two Libraries We Actually Ran, Return Values Verbatim

Three passwords — empty, wrong, correct — went at a 3-page test deck encrypted with pypdf and with PyMuPDF (AES-256). Both libraries encrypted cleanly and both verified the password at open time. The finding both tools share: a wrong password does not raise an error — it fails silently, returning a false-ish value your script must check (0 on both, in our runs). The correct password unlocked both: pypdf's decrypt returned 2 (owner-level success) and PyMuPDF's authenticate returned 6 — a permission bitmask, not a failure. Removing the password afterward worked in both: the saved outputs read back fully unencrypted, pages and text intact. Everything below comes from a run executed today; every quoted value is a verbatim return from the script in front of us; no accuracy claims, no prices.

Day 13 of our 28-day series — the gap-list item our OCR guide pointed at without testing it: that page says OCR engines refuse encrypted files; this is the page that actually runs the encryption, authentication and removal steps. Same series rules: only executed tools get rows, not-run items carry reasons, and the exact calls are printed so you can reproduce the runs in a minute.

The test deck (reproduce in 30 seconds)

Three pages of one text line each, built with PyMuPDF and saved unencrypted. Both libraries then encrypted their own copy with the same password pair (user and owner both set to testpass123), pypdf with its default cipher and PyMuPDF with AES-256 explicitly. One caution our own series repeats: only encrypt files you own, and only remove protection with the owner's authorization — everything in this run was done on files we built ourselves for exactly that reason.

Comparison table — both libraries ran, every value verbatim

Step in the runpypdf (verbatim return)PyMuPDF (verbatim return)What it means
Open the encrypted file, ask if it is protectedis_encrypted: Trueneeds_pass: 1both flag encryption at open time before any password is tried
Try the empty passworddecrypt(""): 0authenticate(""): 0silent failure — no exception raised in either library
Try a wrong passworddecrypt("wrongpass"): 0authenticate("wrongpass"): 0the shared finding: wrong password looks identical to empty — a false value, not an error
Try the correct passworddecrypt("testpass123"): 2authenticate("testpass123"): 6both nonzero = success; PyMuPDF's 6 is a permission bitmask, not an error code
Read pages and text after unlock3 pages, first-page text Secure page 13 pages, first-page text Secure page 1unlocked content identical in both
Remove the password, save, reopenoutput is_encrypted: Falseoutput needs_pass: 0both decrypt paths produce fully unencrypted files

Why the silent failure matters (what the run supports)

In our runs, a wrong password returned 0 in both libraries and the script kept running. Nothing raised, nothing logged. The danger is one branch later: a script that tries the password, ignores the return, and reads page text gets either a crash deep inside the library or an empty result — depending on the library — instead of a clean failure at the door. Our own read of the run: check the return value explicitly on every open of a protected file. That is the entire habit this page exists to hand over, and it comes from watching both tools swallow wrong passwords without a word.

The exact calls, from the runs

# pypdf — encrypt (default cipher), check, and remove
w = PdfWriter()
for p in PdfReader("in.pdf").pages:
    w.add_page(p)
w.encrypt(user_password="testpass123", owner_password="testpass123")
with open("enc.pdf", "wb") as f:
    w.write(f)

r = PdfReader("enc.pdf")
print(r.is_encrypted)          # True
print(r.decrypt("wrongpass"))  # 0 — silent false, no exception
print(r.decrypt("testpass123"))# 2 — owner-level success

# remove: decrypt, then copy pages into a fresh writer and save
r.decrypt("testpass123")
w2 = PdfWriter()
for p in r.pages:
    w2.add_page(p)
with open("dec.pdf", "wb") as f:
    w2.write(f)   # reopens with is_encrypted: False

# PyMuPDF — encrypt AES-256, check, and remove
d = pymupdf.open("in.pdf")
d.save("enc.pdf", encryption=pymupdf.PDF_ENCRYPT_AES_256,
       owner_pw="testpass123", user_pw="testpass123")

m = pymupdf.open("enc.pdf")
print(m.needs_pass)              # 1
print(m.authenticate("wrongpass"))   # 0 — silent false
print(m.authenticate("testpass123")) # 6 — permission bitmask, nonzero = ok

# remove: authenticate, then save with encryption off
m.authenticate("testpass123")
m.save("dec.pdf", encryption=pymupdf.PDF_ENCRYPT_NONE)   # reopens with needs_pass: 0

What we could NOT test — and refuse to guess

- Owner-password-only restrictions (print/copy disabled but the file opens) — not run. Reason: we set user and owner to the same password; how the two libraries treat permission flags when the user password is empty and restrictions are set was not measured and will not be guessed.

- Commercial crackers and online unlock services — not run, and the online ones fail this site's privacy bar by design; nothing on this page scores them.

- Cipher-strength comparisons (RC4 vs AES-128 vs AES-256 across the two libraries) — not run. Reason: we ran each library's default-or-stated path once; a cipher benchmark is a different page and was not executed here.

- Whether the OCR guide's claim (engines refuse encrypted files) holds for every OCR tool — still not run. This page tested encryption and removal with two Python libraries; the OCR refusal claim stays a reasoned limit from that guide, now one step closer to tested but still not measured across tools.

Decision rule this run actually supports

- Scripting against protected PDFs: after every open, check the flag (is_encrypted / needs_pass), try the password, and check the return explicitly — both libraries fail silently on wrong passwords, so an unchecked return is a silent poison downstream.

- Removing protection you are authorized to remove: both libraries did it in two calls (unlock, save-without-encryption) and both outputs reopened clean; pick whichever is already in your pipeline.

- Confidential documents stay local: every run above executed in our sandbox with zero uploads; an upload-based unlock service breaks that bar by design.

- A 6 from PyMuPDF's authenticate is not an error: it is a permission bitmask; any nonzero value means the password worked, and our run's text extraction confirms content reads fine after it.

Quick FAQ

Can these tools crack a password I do not know?

Not in anything we ran: both libraries verify, not attack. Everything on this page used a password we set ourselves. Cracking is a different topic and was not executed here.

Why did authenticate return 6?

It is a permission bitmask in that library, not a failure code. Our run confirmed it: 6 came with full page and text access immediately after.

Does removing the password change the pages?

Not in our runs: 3 pages, identical first-page text, and both outputs reopened with no encryption flag. The removal rewrote the encryption layer, not the content.

Can I reproduce your runs?

Build any 3-page PDF, run the code block above with your own password, and compare every return value against the table. Your numbers should match ours exactly, because they are return values, not timings — and if any of them differ, that itself is worth a page.

This page tests what the OCR guide could only state: what libraries actually do with encrypted PDFs. Bookmark pdf2mdtool.com and follow along.