<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0 Strict Level 1//EN">
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0 Strict Level 1//EN">
<HTML>
<HEAD>
<BASE HREF="http://www.ozemail.com.au/~geoffk/pdfencrypt/">
<META name="description"
content="Modifications to GhostScript to allow it to read encrypted PDF
files.">
<META name="keywords"
content="PDF encryption PDF security GhostScript viewing printing">
<TITLE>Encrypted PDFs</TITLE>
<LINK REV=made HREF="mailto:geoffk@ozemail.com.au">
</HEAD>
<BODY>
<H1>Encrypted PDFs</H1>

<P>When you give a <B style="color:black;background-color:#ffff66">PDF</B> document an owner password, Adobe Acrobat
encrypts it so that you can't just remove the password from the file.
Unfortunately, because of
<A HREF="http://www.bxa.doc.gov/encstart.htm">stupid US regulations</A>
about exporting cryptographic software, the encryption cannot be added
into programs like
<A HREF="http://www.cs.wisc.edu/%7Eghost/">GhostScript</A> which are
distributed with source code from the US.

<P>This web page, however, is not in the US, so I can provide here the
modifications necessary to view encrypted PDFs with GhostScript.

<H2>GhostScript versions 4.01 and earlier</H2>

<P>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
<KBD>patch -l</KBD>.  If you don't have `patch' on your
machine, you can probably find a version at
<A HREF="ftp://prep.ai.mit.edu/pub/gnu/">your friendly GNU archive</A>.

<P>There are patches for versions:

<UL>
<LI><A HREF="patch-gs333-pdfsec">3.33</A>,</LI>
<LI><A HREF="patch-gs353-pdfsec">3.53</A>,</LI>
<LI><A HREF="patch-gs401-pdfsec">4.00 and 4.01</A>.</LI>
</UL>

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

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

<H2>GhostScript versions 4.02 and later</H2>

<P>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.

<P><A HREF="pdf_sec.ps"><STRONG>pdf_sec.ps</STRONG></A>

<P>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 <A
HREF="ftp://lcweb.loc.gov/pub/copyright/forms/formtxi.pdf">US
Copyright Office's Form TX</A>.  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.

<H2>Usage information</H2>

<P>If GhostScript reports
<SAMP>****This file has a user password set.</SAMP>,
you can specify a password by setting <VAR>PDFPassword</VAR> to either
the user or owner password of the <B style="color:black;background-color:#ffff66">PDF</B> file. On the command line, you
might say
<KBD>gs -sPDFPassword=apassword somefile.<B style="color:black;background-color:#ffff66">pdf</B></KBD>.
Inside GhostScript, you can say
<KBD>/PDFPassword (apassword) def</KBD>.
You can also put this in a file generated with pdf2dsc; put it just
after the line that says <SAMP>%%BeginProlog</SAMP>.

<P>I don't know how to do this with GSView, because I don't have any
hardware available that can run it.  If someone knows how, I'd
appreciate hearing about it.

<H3>Lost Passwords</H3>

<P>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 <B style="color:black;background-color:#ffff66">PDF</B> file from, and re-generate it.

<P>Alternatively, you might try contacting <A
HREF="http://www.pwcrack.com/">PWCrack</A>, who can help you unprotect
the file.

<H2>Technical Details</H2>

<P>If you're interested in how the patch works, you should look
at version 1.2 of the <B style="color:black;background-color:#ffff66">PDF</B> reference manual (available at <A
HREF="http://www.adobe.com/">Adobe's Web site</A>). My patches predate
this description, but they seem to conform to it exactly.

<H1>RC4-compatible and MD5 PostScript</H1>

<UL>
<LI><A HREF="rc4-source.ps">Source for an RC4-compatible algorithm 
implemented in PostScript</A>.</LI>
<LI><A HREF="md5-source.ps">Source for MD5 implemented in
PostScript</A>.</LI>
</UL>

<P>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.

<P><HR>
<P>Unless otherwise specified, these files are Copyright 1996-1999
<A HREF="mailto:geoffk@ozemail.com.au"><B style="color:black;background-color:#ffff66">Geoffrey</B> Keating</A>, and may be freely 
distributed with or without modifications, so long as proper attribution is 
given and copyright notices are not removed.
</BODY>
</HTML>