You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by RJC 2009 <rj...@gmail.com> on 2019/05/17 07:47:07 UTC

Need help to protect PDF with owner password

Hi All,

I am using PDFBox version 2.0.5 and looking for help to protect a PDF with
owner password.

Here is my test program which is trying to disable the printing option and
protect the document with an owner password.

However, if I use the resultant PDF and run the program again with a
DIFFERENT owner password then it ENABLE the printing option.



Basically I need to disable the printing option in PDF with an owner
password and the printing option cannot be enabled back without using the
original owner password.


Please advise if I am missing anything in my below testing programme.


*import* java.io.File;

*import* org.apache.pdfbox.pdmodel.PDDocument;

*import* org.apache.pdfbox.pdmodel.encryption.AccessPermission;

*import* org.apache.pdfbox.pdmodel.encryption.StandardProtectionPolicy;



*public* *class* TestEncryptPDF {



       *public* *static* *void* main(String[] args) {



              String inputPdfFileName = "D:\\temp\\test-input.pdf";

              String cannotPrintPdfFileName = inputPdfFileName.replace(
".pdf", "-cannotPrint.pdf");

              String canPrintPdfFileName = inputPdfFileName.replace(".pdf",
"-cannPrint.pdf");

              *setPdfPassword*(inputPdfFileName, cannotPrintPdfFileName,
"OwnerPassword1234", *false*);



              *setPdfPassword*(cannotPrintPdfFileName, canPrintPdfFileName,
"BreakingOwnerPassworAndEnablingPrinting", *true*);

       }



       *private* *static* *void* setPdfPassword(String inputPdfFileName,

                     String outputPdfFileName, String ownerPassword,
*boolean* canPrint) {

              *try* {

                     PDDocument pdfDoc = PDDocument.*load*(*new* File(
inputPdfFileName));

                     AccessPermission ap = *new* AccessPermission();

                     ap.setCanPrint(canPrint);

                     ap.setCanAssembleDocument(*false*);

                     ap.setCanExtractContent(*false*);

                     ap.setCanExtractForAccessibility(*false*);

                     ap.setCanFillInForm(*false*);

                     ap.setCanModify(*false*);

                     ap.setCanModifyAnnotations(*false*);

                     ap.setReadOnly();

                     // setting only owner *pwd*

                     StandardProtectionPolicy spp = *new*
StandardProtectionPolicy(

                                  ownerPassword, *null*, ap);

                     spp.setPreferAES(*true*);

                     spp.setEncryptionKeyLength(128);

                     spp.setPermissions(ap);

                     pdfDoc.protect(spp);

                     pdfDoc.save(outputPdfFileName);

                     pdfDoc.close();

              } *catch* (Exception e) {

                     e.printStackTrace();

              }

       }



}

regards

RJC

Re: Need help to protect PDF with owner password

Posted by Tilman Hausherr <TH...@t-online.de>.
Hi,

One could still use the content by transferring the page content. The 
command line applications do respect the settings (almost... I'll fix a 
few issues soon). The PDFBox library is a toolkit to build an 
application, so your application should respect these settings.

http://gitlab.itextsupport.com/itext/sandbox/raw/master/resources/pdfs/hello_encrypted2.pdf
can be decrypted with pdfbox, so it can be reencrypted too.

Tilman

Am 22.05.2019 um 03:28 schrieb RJC 2009:
> Thanks Tilman Hausherr and Marc Kaufman for your time & help.
>
> I came across following post at iText tech support faq talking about
> similar topic.
> https://itextpdf.com/en/resources/faq/technical-support/itext-7/how-decrypt-pdf-document-owner-password
>
> Seems the following pdf file which they have shared is encrypted and does
> not allow to overwrite the owner password using PDFBox (2.0.15).
> http://gitlab.itextsupport.com/itext/sandbox/raw/master/resources/pdfs/hello_encrypted2.pdf
>
> I'm just wondering how does this works and are there any similar
> implementation in PDFBox.
>
> On Wed, May 22, 2019 at 7:45 AM Marc Kaufman <ma...@eeph.com> wrote:
>
>> Which is, of course, a violation of the "agreement" between PDF and
>> Readers (ignored by everyone, it seems, except Adobe). In
>> Acrobat/Reader, you need to open with the Owner password in order to
>> change permissions. Opening with the User password should honor
>> permissions and not allow changing of permissions.
>>
>> Marc
>>
>> On 5/21/2019 9:42 AM, Tilman Hausherr wrote:
>>> Am 17.05.2019 um 09:47 schrieb RJC 2009:
>>>> Here is my test program which is trying to disable the printing
>>>> option and
>>>> protect the document with an owner password.
>>>>
>>>> However, if I use the resultant PDF and run the program again with a
>>>> DIFFERENT owner password then it ENABLE the printing option.
>>>>
>>>>
>>>>
>>>> Basically I need to disable the printing option in PDF with an owner
>>>> password and the printing option cannot be enabled back without using
>>>> the
>>>> original owner password.
>>>
>>> I should have read the text more carefully (and continue reading after
>>> "2.0.5"). This is by design. When you open with the user password then
>>> the file is decrypted and can be manipulated, reencrypted, everything.
>>> The owner password is just an extra so that a software can decide that
>>> you're the owner. That is why there are so many tools on the internet
>>> that allow to change the permissions of PDF files.
>>>
>>> Read also this:
>>>
>>> https://www.cs.cmu.edu/~dst/Adobe/Gallery/anon21jul01-pdf-encryption.txt
>>>
>>>
>>> Tilman
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
>>> For additional commands, e-mail: users-help@pdfbox.apache.org
>>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
>> For additional commands, e-mail: users-help@pdfbox.apache.org
>>
>>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


Re: Need help to protect PDF with owner password

Posted by Tilman Hausherr <TH...@t-online.de>.
Answered on 22.05.2019, 18:57. If you didn't get it, then look in the 
mailing list archives.

Tilman

Am 19.06.2019 um 09:36 schrieb rjc2009@gmail.com:
> Appreciate any help on this.
> I came across following post at iText tech support faq talking about similar topic.
> https://itextpdf.com/en/resources/faq/technical-support/itext-7/how-decrypt-pdf-document-owner-password
>
> Seems the following PDF file which they have shared is encrypted and does not allow to overwrite the owner password using PDFBox (2.0.15).
> http://gitlab.itextsupport.com/itext/sandbox/raw/master/resources/pdfs/hello_encrypted2.pdf
>
> How does this works and are there any similar implementation in PDFBox.
>
> Thanks
>
> On 2019/05/22 01:28:36, RJC 2009 <rj...@gmail.com> wrote:
>> Thanks Tilman Hausherr and Marc Kaufman for your time & help.
>>
>> I came across following post at iText tech support faq talking about
>> similar topic.
>> https://itextpdf.com/en/resources/faq/technical-support/itext-7/how-decrypt-pdf-document-owner-password
>>
>> Seems the following pdf file which they have shared is encrypted and does
>> not allow to overwrite the owner password using PDFBox (2.0.15).
>> http://gitlab.itextsupport.com/itext/sandbox/raw/master/resources/pdfs/hello_encrypted2.pdf
>>
>> I'm just wondering how does this works and are there any similar
>> implementation in PDFBox.
>>
>> On Wed, May 22, 2019 at 7:45 AM Marc Kaufman <ma...@eeph.com> wrote:
>>
>>> Which is, of course, a violation of the "agreement" between PDF and
>>> Readers (ignored by everyone, it seems, except Adobe). In
>>> Acrobat/Reader, you need to open with the Owner password in order to
>>> change permissions. Opening with the User password should honor
>>> permissions and not allow changing of permissions.
>>>
>>> Marc
>>>
>>> On 5/21/2019 9:42 AM, Tilman Hausherr wrote:
>>>> Am 17.05.2019 um 09:47 schrieb RJC 2009:
>>>>> Here is my test program which is trying to disable the printing
>>>>> option and
>>>>> protect the document with an owner password.
>>>>>
>>>>> However, if I use the resultant PDF and run the program again with a
>>>>> DIFFERENT owner password then it ENABLE the printing option.
>>>>>
>>>>>
>>>>>
>>>>> Basically I need to disable the printing option in PDF with an owner
>>>>> password and the printing option cannot be enabled back without using
>>>>> the
>>>>> original owner password.
>>>>
>>>> I should have read the text more carefully (and continue reading after
>>>> "2.0.5"). This is by design. When you open with the user password then
>>>> the file is decrypted and can be manipulated, reencrypted, everything.
>>>> The owner password is just an extra so that a software can decide that
>>>> you're the owner. That is why there are so many tools on the internet
>>>> that allow to change the permissions of PDF files.
>>>>
>>>> Read also this:
>>>>
>>>> https://www.cs.cmu.edu/~dst/Adobe/Gallery/anon21jul01-pdf-encryption.txt
>>>>
>>>>
>>>> Tilman
>>>>
>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
>>>> For additional commands, e-mail: users-help@pdfbox.apache.org
>>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
>>> For additional commands, e-mail: users-help@pdfbox.apache.org
>>>
>>>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


Re: Need help to protect PDF with owner password

Posted by .
Appreciate any help on this. 
I came across following post at iText tech support faq talking about similar topic.
https://itextpdf.com/en/resources/faq/technical-support/itext-7/how-decrypt-pdf-document-owner-password

Seems the following PDF file which they have shared is encrypted and does not allow to overwrite the owner password using PDFBox (2.0.15).
http://gitlab.itextsupport.com/itext/sandbox/raw/master/resources/pdfs/hello_encrypted2.pdf

How does this works and are there any similar implementation in PDFBox.

Thanks 

On 2019/05/22 01:28:36, RJC 2009 <rj...@gmail.com> wrote: 
> Thanks Tilman Hausherr and Marc Kaufman for your time & help.
> 
> I came across following post at iText tech support faq talking about
> similar topic.
> https://itextpdf.com/en/resources/faq/technical-support/itext-7/how-decrypt-pdf-document-owner-password
> 
> Seems the following pdf file which they have shared is encrypted and does
> not allow to overwrite the owner password using PDFBox (2.0.15).
> http://gitlab.itextsupport.com/itext/sandbox/raw/master/resources/pdfs/hello_encrypted2.pdf
> 
> I'm just wondering how does this works and are there any similar
> implementation in PDFBox.
> 
> On Wed, May 22, 2019 at 7:45 AM Marc Kaufman <ma...@eeph.com> wrote:
> 
> > Which is, of course, a violation of the "agreement" between PDF and
> > Readers (ignored by everyone, it seems, except Adobe). In
> > Acrobat/Reader, you need to open with the Owner password in order to
> > change permissions. Opening with the User password should honor
> > permissions and not allow changing of permissions.
> >
> > Marc
> >
> > On 5/21/2019 9:42 AM, Tilman Hausherr wrote:
> > > Am 17.05.2019 um 09:47 schrieb RJC 2009:
> > >> Here is my test program which is trying to disable the printing
> > >> option and
> > >> protect the document with an owner password.
> > >>
> > >> However, if I use the resultant PDF and run the program again with a
> > >> DIFFERENT owner password then it ENABLE the printing option.
> > >>
> > >>
> > >>
> > >> Basically I need to disable the printing option in PDF with an owner
> > >> password and the printing option cannot be enabled back without using
> > >> the
> > >> original owner password.
> > >
> > >
> > > I should have read the text more carefully (and continue reading after
> > > "2.0.5"). This is by design. When you open with the user password then
> > > the file is decrypted and can be manipulated, reencrypted, everything.
> > > The owner password is just an extra so that a software can decide that
> > > you're the owner. That is why there are so many tools on the internet
> > > that allow to change the permissions of PDF files.
> > >
> > > Read also this:
> > >
> > > https://www.cs.cmu.edu/~dst/Adobe/Gallery/anon21jul01-pdf-encryption.txt
> > >
> > >
> > > Tilman
> > >
> > >
> > > ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> > > For additional commands, e-mail: users-help@pdfbox.apache.org
> > >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> > For additional commands, e-mail: users-help@pdfbox.apache.org
> >
> >
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


Re: Need help to protect PDF with owner password

Posted by RJC 2009 <rj...@gmail.com>.
Thanks Tilman Hausherr and Marc Kaufman for your time & help.

I came across following post at iText tech support faq talking about
similar topic.
https://itextpdf.com/en/resources/faq/technical-support/itext-7/how-decrypt-pdf-document-owner-password

Seems the following pdf file which they have shared is encrypted and does
not allow to overwrite the owner password using PDFBox (2.0.15).
http://gitlab.itextsupport.com/itext/sandbox/raw/master/resources/pdfs/hello_encrypted2.pdf

I'm just wondering how does this works and are there any similar
implementation in PDFBox.

On Wed, May 22, 2019 at 7:45 AM Marc Kaufman <ma...@eeph.com> wrote:

> Which is, of course, a violation of the "agreement" between PDF and
> Readers (ignored by everyone, it seems, except Adobe). In
> Acrobat/Reader, you need to open with the Owner password in order to
> change permissions. Opening with the User password should honor
> permissions and not allow changing of permissions.
>
> Marc
>
> On 5/21/2019 9:42 AM, Tilman Hausherr wrote:
> > Am 17.05.2019 um 09:47 schrieb RJC 2009:
> >> Here is my test program which is trying to disable the printing
> >> option and
> >> protect the document with an owner password.
> >>
> >> However, if I use the resultant PDF and run the program again with a
> >> DIFFERENT owner password then it ENABLE the printing option.
> >>
> >>
> >>
> >> Basically I need to disable the printing option in PDF with an owner
> >> password and the printing option cannot be enabled back without using
> >> the
> >> original owner password.
> >
> >
> > I should have read the text more carefully (and continue reading after
> > "2.0.5"). This is by design. When you open with the user password then
> > the file is decrypted and can be manipulated, reencrypted, everything.
> > The owner password is just an extra so that a software can decide that
> > you're the owner. That is why there are so many tools on the internet
> > that allow to change the permissions of PDF files.
> >
> > Read also this:
> >
> > https://www.cs.cmu.edu/~dst/Adobe/Gallery/anon21jul01-pdf-encryption.txt
> >
> >
> > Tilman
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> > For additional commands, e-mail: users-help@pdfbox.apache.org
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
>
>

Re: Need help to protect PDF with owner password

Posted by Marc Kaufman <ma...@eeph.com>.
Which is, of course, a violation of the "agreement" between PDF and 
Readers (ignored by everyone, it seems, except Adobe). In 
Acrobat/Reader, you need to open with the Owner password in order to 
change permissions. Opening with the User password should honor 
permissions and not allow changing of permissions.

Marc

On 5/21/2019 9:42 AM, Tilman Hausherr wrote:
> Am 17.05.2019 um 09:47 schrieb RJC 2009:
>> Here is my test program which is trying to disable the printing 
>> option and
>> protect the document with an owner password.
>>
>> However, if I use the resultant PDF and run the program again with a
>> DIFFERENT owner password then it ENABLE the printing option.
>>
>>
>>
>> Basically I need to disable the printing option in PDF with an owner
>> password and the printing option cannot be enabled back without using 
>> the
>> original owner password.
>
>
> I should have read the text more carefully (and continue reading after 
> "2.0.5"). This is by design. When you open with the user password then 
> the file is decrypted and can be manipulated, reencrypted, everything. 
> The owner password is just an extra so that a software can decide that 
> you're the owner. That is why there are so many tools on the internet 
> that allow to change the permissions of PDF files.
>
> Read also this:
>
> https://www.cs.cmu.edu/~dst/Adobe/Gallery/anon21jul01-pdf-encryption.txt
>
>
> Tilman
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


Re: Need help to protect PDF with owner password

Posted by Tilman Hausherr <TH...@t-online.de>.
Am 17.05.2019 um 09:47 schrieb RJC 2009:
> Here is my test program which is trying to disable the printing option and
> protect the document with an owner password.
>
> However, if I use the resultant PDF and run the program again with a
> DIFFERENT owner password then it ENABLE the printing option.
>
>
>
> Basically I need to disable the printing option in PDF with an owner
> password and the printing option cannot be enabled back without using the
> original owner password.


I should have read the text more carefully (and continue reading after 
"2.0.5"). This is by design. When you open with the user password then 
the file is decrypted and can be manipulated, reencrypted, everything. 
The owner password is just an extra so that a software can decide that 
you're the owner. That is why there are so many tools on the internet 
that allow to change the permissions of PDF files.

Read also this:

https://www.cs.cmu.edu/~dst/Adobe/Gallery/anon21jul01-pdf-encryption.txt


Tilman


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


Re: Need help to protect PDF with owner password

Posted by Gilad Denneboom <gi...@gmail.com>.
Any reason not to use the latest version of PDFBox (2.0.15)? It might solve
this issue...

On Fri, May 17, 2019 at 9:47 AM RJC 2009 <rj...@gmail.com> wrote:

> Hi All,
>
> I am using PDFBox version 2.0.5 and looking for help to protect a PDF with
> owner password.
>
> Here is my test program which is trying to disable the printing option and
> protect the document with an owner password.
>
> However, if I use the resultant PDF and run the program again with a
> DIFFERENT owner password then it ENABLE the printing option.
>
>
>
> Basically I need to disable the printing option in PDF with an owner
> password and the printing option cannot be enabled back without using the
> original owner password.
>
>
> Please advise if I am missing anything in my below testing programme.
>
>
> *import* java.io.File;
>
> *import* org.apache.pdfbox.pdmodel.PDDocument;
>
> *import* org.apache.pdfbox.pdmodel.encryption.AccessPermission;
>
> *import* org.apache.pdfbox.pdmodel.encryption.StandardProtectionPolicy;
>
>
>
> *public* *class* TestEncryptPDF {
>
>
>
>        *public* *static* *void* main(String[] args) {
>
>
>
>               String inputPdfFileName = "D:\\temp\\test-input.pdf";
>
>               String cannotPrintPdfFileName = inputPdfFileName.replace(
> ".pdf", "-cannotPrint.pdf");
>
>               String canPrintPdfFileName = inputPdfFileName.replace(".pdf",
> "-cannPrint.pdf");
>
>               *setPdfPassword*(inputPdfFileName, cannotPrintPdfFileName,
> "OwnerPassword1234", *false*);
>
>
>
>               *setPdfPassword*(cannotPrintPdfFileName, canPrintPdfFileName,
> "BreakingOwnerPassworAndEnablingPrinting", *true*);
>
>        }
>
>
>
>        *private* *static* *void* setPdfPassword(String inputPdfFileName,
>
>                      String outputPdfFileName, String ownerPassword,
> *boolean* canPrint) {
>
>               *try* {
>
>                      PDDocument pdfDoc = PDDocument.*load*(*new* File(
> inputPdfFileName));
>
>                      AccessPermission ap = *new* AccessPermission();
>
>                      ap.setCanPrint(canPrint);
>
>                      ap.setCanAssembleDocument(*false*);
>
>                      ap.setCanExtractContent(*false*);
>
>                      ap.setCanExtractForAccessibility(*false*);
>
>                      ap.setCanFillInForm(*false*);
>
>                      ap.setCanModify(*false*);
>
>                      ap.setCanModifyAnnotations(*false*);
>
>                      ap.setReadOnly();
>
>                      // setting only owner *pwd*
>
>                      StandardProtectionPolicy spp = *new*
> StandardProtectionPolicy(
>
>                                   ownerPassword, *null*, ap);
>
>                      spp.setPreferAES(*true*);
>
>                      spp.setEncryptionKeyLength(128);
>
>                      spp.setPermissions(ap);
>
>                      pdfDoc.protect(spp);
>
>                      pdfDoc.save(outputPdfFileName);
>
>                      pdfDoc.close();
>
>               } *catch* (Exception e) {
>
>                      e.printStackTrace();
>
>               }
>
>        }
>
>
>
> }
>
> regards
>
> RJC
>

Re: Need help to protect PDF with owner password

Posted by RJC 2009 <rj...@gmail.com>.
Thanks Gilad Denneboom & Tilman Hausherr for your prompt response.
I have tried with the version PDFBox (2.0.15) but the issue still persists.
I have yet to try the "Encrypt" command line utility but at the end I have
to achieve this using this programmatically.
I've shared my testing java code and the sample input & output pdf files at
the following sharehoster url, please take a look.

https://drive.google.com/open?id=1rYRXFLi7NuzkkvZrm8Er3Lgqn97u2ePs

Appreciate any help on this. Thanks

On Sat, May 18, 2019 at 1:30 AM Tilman Hausherr <TH...@t-online.de>
wrote:

> Am 17.05.2019 um 09:47 schrieb RJC 2009:
> > I am using PDFBox version 2.0.5 and looking for help to protect a PDF
> with
> > owner password.
>
>
> Please retry with the current version. If it doesn't work, please upload
> code, source and modified file to a sharehoster. (The code here can't be
> used, it  has lots of "*").
>
> Try also the "Encrypt" command line utility.
>
> Tilman
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
>
>

Re: Need help to protect PDF with owner password

Posted by Tilman Hausherr <TH...@t-online.de>.
Am 17.05.2019 um 09:47 schrieb RJC 2009:
> I am using PDFBox version 2.0.5 and looking for help to protect a PDF with
> owner password.


Please retry with the current version. If it doesn't work, please upload 
code, source and modified file to a sharehoster. (The code here can't be 
used, it  has lots of "*").

Try also the "Encrypt" command line utility.

Tilman


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org