Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Requirement is to process a batch of PDF's one at a time and on success encrypt each of them with an user password.

However, these PDF's were encrypted previously with randomly generated dynamic owner password (not know to any one) to prevent any edits.

I use iText for encryption as shown below:

byte[] userPass = "user".getBytes();
byte[] ownerPass = "owner".getBytes();
PdfReader reader = new PdfReader("Misc.pdf");

PdfStamper stamper = new PdfStamper(reader,
            new FileOutputStream("Processed_Encrypted.pdf"));
stamper.setEncryption(userPass, ownerPass,
PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128
        | PdfWriter.DO_NOT_ENCRYPT_METADATA);
stamper.close();
reader.close();

But this code throws an com.itextpdf.text.exceptions.BadPasswordException: PdfReader not opened with owner password

Can some one guide on how to resolve this error / bypass owner password?

Here I would like to make clear that we legally own these PDFs, so no crime / hacking is committed.

P.S.: Solution isn't limited to iText, can use any other Java library (Free or licensed) too.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
264 views
Welcome To Ask or Share your Answers For Others

1 Answer

PdfReader has an undocumented static boolean variable named unethicalreading. For obvious reasons, this variable is set to false by default. You could set this variable to true like this:

PdfReader.unethicalreading = true;

From now on, PdfReader will ignore the presence of an owner password. It will only throw an exception if a user password is in place.

Use this at your own risk.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...