Home > Software engineering >  iText7 PDF unlocker didn't work for some encrypted pdfs throwing BadPasswordException
iText7 PDF unlocker didn't work for some encrypted pdfs throwing BadPasswordException

Time:10-23

I'm using this basic code to read a pdf by decrypting it through password:

pdfReader = PdfReader(sourceFileInputStream, ReaderProperties().setPassword(ownerPassword.toByteArray()))

But it only works fine for the encrypted pdf I downloaded from iText example repo and it didn't work for other pdfs like these:

It gives me this stacktrace for above pdfs:

com.itextpdf.kernel.exceptions.BadPasswordException: Bad user password. Password is not provided or wrong password provided. Correct password should be passed to PdfReader constructor with properties. See ReaderProperties#setPassword() method.
at com.itextpdf.kernel.crypto.securityhandler.StandardHandlerUsingAes256.initKeyAndReadDictionary(StandardHandlerUsingAes256.java:253)
at com.itextpdf.kernel.crypto.securityhandler.StandardHandlerUsingAes256.<init>(StandardHandlerUsingAes256.java:89)
at com.itextpdf.kernel.pdf.PdfEncryption.<init>(PdfEncryption.java:245)
at com.itextpdf.kernel.pdf.PdfReader.readDecryptObj(PdfReader.java:1403)
at com.itextpdf.kernel.pdf.PdfReader.readPdf(PdfReader.java:762)
at com.itextpdf.kernel.pdf.PdfDocument.open(PdfDocument.java:1982)
at com.itextpdf.kernel.pdf.PdfDocument.<init>(PdfDocument.java:246)
at com.itextpdf.kernel.pdf.PdfDocument.<init>(PdfDocument.java:228)

I thought maybe there was some mistake in my implementation so I tried iText Web Based unlocker but it also resulted in "Wrong Owner Password. Try again.".

The pdf I'm using above are generated by me for testing using Adobe Acrobat Pro.

Also these pdf successfully decrypted by this online sevice.

If anybody has any idea about what wrong is going on with it then please let me know. Thank you.

CodePudding user response:

The encryption information in your files is invalid.

In the respective encryption dictionary of your files the R (revision) value is 6 and the values of the O and U entries are 127 bytes long.

According to the specification, though:

Key Type Value
O byte string (Required) A byte string, 32 bytes long if the value of R is 4 or less and 48 bytes long if the value of R is 6, [...]
U byte string (Required) A byte string, 32 bytes long if the value of R is 4 or less and 48 bytes long if the value of R is 6, [...]

(ISO 32000-2, Table 21 — Additional encryption dictionary entries for the standard security handler)

Essentially the 48 byte value is appended with zero bytes to to fill 127 bytes. This is a known Adobe Acrobat bug.

Please ask Adobe to fix their software. Or ask iText support to work around this Adobe bug. Or do the latter yourself, iText is open source after all... ;)

  • Related