<p>Although the <a href="http://www.adobe.com/devnet/pdf/pdf_reference.html" rel="noreferrer">PDF specification</a> is available from Adobe, it's not exactly the simplest document to read through.  PDF allows documents to be encrypted so that either a user password and/or an owner password is required to do various things with the document (display, print, etc).  A common use is to lock a PDF so that end users can read it without entering any password, but a password is required to do anything else.</p>

<p>I'm trying to parse PDFs that are locked in this way (to get the same privileges as you would get opening them in any reader).  Using an empty string as the user password doesn't work, but it seems (section 3.5.2 of the spec) that there has to be a user password to create the hash for the admin password.</p>

<p>What I would like is either an explanation of how to do this, or any code that I can read (ideally Python, C, or C++, but anything readable will do) that does this so that I can understand what I'm meant to be doing.  Standalone code, rather than reading through (e.g.) the gsview source, would be best.</p>

[{'answer_id': 49485, 'author': 'DMC', 'author_id': 3148, 'author_profile': 'https://Stackoverflow.com/users/3148', 'pm_score': 1, 'selected': False, 'text': '<p>A plugin for GSview for viewing encrypted PDFs is <a href="http://www.cs.cmu.edu/~dst/Adobe/Gallery/Keating/" rel="nofollow noreferrer">here</a>.</p>\n\n<p>If this works for you, you may be able to look at the source.</p>\n'}, {'answer_id': 107838, 'author': 'Pieter', 'author_id': 5822, 'author_profile': 'https://Stackoverflow.com/users/5822', 'pm_score': 1, 'selected': False, 'text': "<p>If I remember correctly, there is a fixed padding string of 32 (?) bytes to apply to any password. All passwords need to be 32 bytes at the start of computing the encryption key, either by truncating or adding some of those padding bytes.</p>\n\n<p>If no user password was set you simply have to pad with all 32 bytes of the string, i.e. use the 32 padding bytes as the starting point for computing the encryption key.</p>\n\n<p>I have to admit it's been a while since I've done this, I do remember that the encryption part of the PDF is an absolute mess as it got changed significantly in nearly every revision, requiring you to cope with a lot of cases to handle all PDF's.</p>\n\n<p>Good luck.</p>\n"}, {'answer_id': 110110, 'author': 'tfinniga', 'author_id': 9042, 'author_profile': 'https://Stackoverflow.com/users/9042', 'pm_score': 0, 'selected': False, 'text': '<p>xpdf is probably a good reference implementation for this sort of problem.  I have successfully used them to open encrypted pdfs before.</p>\n'}]
['https://Stackoverflow.com/questions/49455', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4966/']