Encrypted PDFs

When you give a PDF document an owner password, Adobe Acrobat encrypts it so that you can't just remove the password from the file. This web page provides the modifications necessary to view encrypted PDFs with GhostScript.

It used to be that source code like this was not permitted to be exported from the US. Happily, the regulations have now changed, and the code is unrestricted in most countries.

GhostScript versions 4.01 and earlier

To allow versions of GhostScript from 3.33 through 4.01 to read encrypted PDFs, you need to download the appropriate patch from the list below, place it in the same directory as the files pdf_base.ps and pdf_main.ps, and pipe them into patch -l. If you don't have `patch' on your machine, you can probably find a version at your friendly GNU archive.

There are patches for versions:

The last of these patches is quite a bit better than patches for earlier versions.

It is likely that these patches will not work if GhostScript is compiled with 64-bit integers.

GhostScript versions 4.02 and later

For these versions, things became somewhat simpler. All you need to do is replace the file called pdf_sec.ps with the version provided below, as described in the documentation.

pdf_sec.ps

This file is version 1.0.4, which corrected a small bug involving user passwords. Version 1.0.3 corrected a bug visible with the US Copyright Office's Form TX. Version 1.0.2 added support for versions 5.0 and later of GhostScript. Version 1.0.1 corrected support for interpreters with 64-bit integers.

Usage information

If GhostScript reports ****This file has a user password set., you can specify a password by setting PDFPassword to either the user or owner password of the PDF file. On the command line, you might say gs -sPDFPassword=apassword somefile.pdf. Inside GhostScript, you can say /PDFPassword (apassword) def. You can also put this in a file generated with pdf2dsc; put it just after the line that says %%BeginProlog.

I'm told that to do this with GSview, you go to the Options menu, chose Configure Ghostscript, and place -sPDFPassword=apassword in the Ghostscript option.

Lost Passwords

Of course, to do this you must know the user or owner password of the file. If you got the file from someone else, you will want to ask them for the password. If it was your file and you've forgotten what password you chose, you should take whatever documents you generated the PDF file from, and re-generate it.

Alternatively, you might try contacting PWCrack, who can help you unprotect the file.

Technical Details

If you're interested in how the patch works, you should look at version 1.2 of the PDF reference manual (available at Adobe's Web site). My patches predate this description, but they seem to conform to it exactly.

RC4-compatible and MD5 PostScript

If you have your browser set to automatically run PostScript files with (say) Ghostview, you'll find doing that is not very interesting, as they don't make any marks on the page; they just run some self-tests.


Unless otherwise specified, these files are Copyright 1996-2000 Geoffrey Keating, and may be freely distributed with or without modifications, so long as proper attribution is given and copyright notices are not removed.