You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by Hesham Gneady <he...@gmail.com> on 2018/01/03 08:29:18 UTC

Decrypt PDF to edit it then encrypt it

Hello,

 

I have a PDF file that has an owner password & user password. I'd like to
decrypt it using the owner password, make some modifications to it, then
encrypt it back and save it. I did the first part successfully, but how can
I encrypt it again using the same encryption settings it had, which I don't
actually know? . For example, I know only the owner password, but not the
user password.

 

 

Best regards,

Hesham 

 

 



---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus

Re: Decrypt PDF to edit it then encrypt it

Posted by Tilman Hausherr <TH...@t-online.de>.
Am 03.01.2018 um 12:34 schrieb Hesham Gneady:
> I am asking if there is a way to copy the encryption dictionary(Or whatever you call it) just as it is to re-encrypt the PDF file using it once again. I don't want to reveal any password.

Oh. I haven't tried it - what you might try would be to change 
COSWriter.java to skip this line:


securityHandler.prepareDocumentForEncryption(pdDocument);


But I don't know if this is good practice. The encryption keys (derived 
from your keys) have a random component that is different for each PDF 
even if your keys are the same. So maybe you're weakening encryption 
this way.

Tilman


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


RE: Decrypt PDF to edit it then encrypt it

Posted by Hesham Gneady <he...@gmail.com>.
I am asking if there is a way to copy the encryption dictionary(Or whatever you call it) just as it is to re-encrypt the PDF file using it once again. I don't want to reveal any password.


Best regards,
Hesham

--------------------------------------------------------------------------------------------------
Included Message:

You can't do that. The library is obviously not going to reveal to you what the password was...

On Wed, Jan 3, 2018 at 9:29 AM, Hesham Gneady <he...@gmail.com>
wrote:

> Hello,
>
>
>
> I have a PDF file that has an owner password & user password. I'd like 
> to decrypt it using the owner password, make some modifications to it, 
> then encrypt it back and save it. I did the first part successfully, 
> but how can I encrypt it again using the same encryption settings it 
> had, which I don't actually know? . For example, I know only the owner 
> password, but not the user password.
>
>
>
>
>
> Best regards,
>
> Hesham
>
>
>
>
>
>
>
> ---
> This email has been checked for viruses by Avast antivirus software.
> https://www.avast.com/antivirus
>


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


Re: Decrypt PDF to edit it then encrypt it

Posted by Gilad Denneboom <gi...@gmail.com>.
You can't do that. The library is obviously not going to reveal to you what
the password was...

On Wed, Jan 3, 2018 at 9:29 AM, Hesham Gneady <he...@gmail.com>
wrote:

> Hello,
>
>
>
> I have a PDF file that has an owner password & user password. I'd like to
> decrypt it using the owner password, make some modifications to it, then
> encrypt it back and save it. I did the first part successfully, but how can
> I encrypt it again using the same encryption settings it had, which I don't
> actually know? . For example, I know only the owner password, but not the
> user password.
>
>
>
>
>
> Best regards,
>
> Hesham
>
>
>
>
>
>
>
> ---
> This email has been checked for viruses by Avast antivirus software.
> https://www.avast.com/antivirus
>

RE: Decrypt PDF to edit it then encrypt it

Posted by Hesham Gneady <he...@gmail.com>.
Thanks Tilman!

I have tested your code & it works fine 😊

 

 

Best regards,

Hesham 

 

--------------------------------------------------------------------------------------------------

Included Message:

 

here's some code... I'm also adding it to our tests as some extra fun.

 

 

        PDEncryption encryption = doc.getEncryption();

        int revision = encryption.getRevision();

        if (revision < 5)

        {

            StandardSecurityHandler standardSecurityHandler = new StandardSecurityHandler();

            int keyLengthInBytes = encryption.getVersion() == 1 ? 5 : 

encryption.getLength() / 8;

            byte[] computedUserPassword = standardSecurityHandler.getUserPassword(

                    ownerpassword.getBytes(Charsets.ISO_8859_1),

                    encryption.getOwnerKey(),

                    revision,

                    keyLengthInBytes);

 

        }

 

You could also try to use incremental saving. But it is tricky (you need to set a "breadcrumb trail" to your updated element(s)) and buggy (if there is a signature and you don't sign yourself).

 

Tilman

 

Am 04.01.2018 um 08:26 schrieb Hesham Gneady:

> Thanks a lot Tilman .. I will try that 😊

> 

> 

> 

> Best regards,

> Hesham

> 

> ----------------------------------------------------------------------

> ----------------------------

> Included Message:

> 

> It is possible for older encryption (not AES256): get the source code and have a look at StandardSecurityHandler.java. There is a segment where "isOwnerPassword()" is called, and a bit later, "computedPassword"

> is calculated by calling getUserPassword(). I tried this by debugging with out test files and yes I got the user password.

> 

> So you could do it by 1) get length, revision and owner key from the PDEncryption object you get by calling document.getEncryption() 2) check that revision is smaller then 5, and 3) call getUserPassword().

> 

> Tilman

> 

> Am 03.01.2018 um 09:29 schrieb Hesham Gneady:

>> Hello,

>> 

>>    

>> 

>> I have a PDF file that has an owner password & user password. I'd 

>> like to decrypt it using the owner password, make some modifications 

>> to it, then encrypt it back and save it. I did the first part 

>> successfully, but how can I encrypt it again using the same 

>> encryption settings it had, which I don't actually know? . For 

>> example, I know only the owner password, but not the user password.

>> 

>>    

>> 

>>    

>> 

>> Best regards,

>> 

>> Hesham

>> 

>>    

>> 

>>    

>> 

>> 

>> 

>> ---

>> This email has been checked for viruses by Avast antivirus software.

>>  <https://www.avast.com/antivirus> https://www.avast.com/antivirus

>> 

> 

> ---------------------------------------------------------------------

> To unsubscribe, e-mail:  <ma...@pdfbox.apache.org> users-unsubscribe@pdfbox.apache.org

> For additional commands, e-mail:  <ma...@pdfbox.apache.org> users-help@pdfbox.apache.org

> 

> 

> 

> ---------------------------------------------------------------------

> To unsubscribe, e-mail:  <ma...@pdfbox.apache.org> users-unsubscribe@pdfbox.apache.org

> For additional commands, e-mail:  <ma...@pdfbox.apache.org> users-help@pdfbox.apache.org

> 

 

 

---------------------------------------------------------------------

To unsubscribe, e-mail:  <ma...@pdfbox.apache.org> users-unsubscribe@pdfbox.apache.org

For additional commands, e-mail:  <ma...@pdfbox.apache.org> users-help@pdfbox.apache.org

 


Re: Decrypt PDF to edit it then encrypt it

Posted by Tilman Hausherr <TH...@t-online.de>.
here's some code... I'm also adding it to our tests as some extra fun.


         PDEncryption encryption = doc.getEncryption();
         int revision = encryption.getRevision();
         if (revision < 5)
         {
             StandardSecurityHandler standardSecurityHandler = new 
StandardSecurityHandler();
             int keyLengthInBytes = encryption.getVersion() == 1 ? 5 : 
encryption.getLength() / 8;
             byte[] computedUserPassword = 
standardSecurityHandler.getUserPassword(
                     ownerpassword.getBytes(Charsets.ISO_8859_1),
                     encryption.getOwnerKey(),
                     revision,
                     keyLengthInBytes);

         }

You could also try to use incremental saving. But it is tricky (you need 
to set a "breadcrumb trail" to your updated element(s)) and buggy (if 
there is a signature and you don't sign yourself).

Tilman

Am 04.01.2018 um 08:26 schrieb Hesham Gneady:
> Thanks a lot Tilman .. I will try that 😊
>
>
>
> Best regards,
> Hesham
>
> --------------------------------------------------------------------------------------------------
> Included Message:
>
> It is possible for older encryption (not AES256): get the source code and have a look at StandardSecurityHandler.java. There is a segment where "isOwnerPassword()" is called, and a bit later, "computedPassword"
> is calculated by calling getUserPassword(). I tried this by debugging with out test files and yes I got the user password.
>
> So you could do it by 1) get length, revision and owner key from the PDEncryption object you get by calling document.getEncryption() 2) check that revision is smaller then 5, and 3) call getUserPassword().
>
> Tilman
>
> Am 03.01.2018 um 09:29 schrieb Hesham Gneady:
>> Hello,
>>
>>    
>>
>> I have a PDF file that has an owner password & user password. I'd like
>> to decrypt it using the owner password, make some modifications to it,
>> then encrypt it back and save it. I did the first part successfully,
>> but how can I encrypt it again using the same encryption settings it
>> had, which I don't actually know? . For example, I know only the owner
>> password, but not the user password.
>>
>>    
>>
>>    
>>
>> Best regards,
>>
>> Hesham
>>
>>    
>>
>>    
>>
>>
>>
>> ---
>> This email has been checked for viruses by Avast antivirus software.
>> https://www.avast.com/antivirus
>>
>
> ---------------------------------------------------------------------
> 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: Decrypt PDF to edit it then encrypt it

Posted by Hesham Gneady <he...@gmail.com>.
Thanks a lot Tilman .. I will try that 😊



Best regards,
Hesham

--------------------------------------------------------------------------------------------------
Included Message:

It is possible for older encryption (not AES256): get the source code and have a look at StandardSecurityHandler.java. There is a segment where "isOwnerPassword()" is called, and a bit later, "computedPassword" 
is calculated by calling getUserPassword(). I tried this by debugging with out test files and yes I got the user password.

So you could do it by 1) get length, revision and owner key from the PDEncryption object you get by calling document.getEncryption() 2) check that revision is smaller then 5, and 3) call getUserPassword().

Tilman

Am 03.01.2018 um 09:29 schrieb Hesham Gneady:
> Hello,
>
>   
>
> I have a PDF file that has an owner password & user password. I'd like 
> to decrypt it using the owner password, make some modifications to it, 
> then encrypt it back and save it. I did the first part successfully, 
> but how can I encrypt it again using the same encryption settings it 
> had, which I don't actually know? . For example, I know only the owner 
> password, but not the user password.
>
>   
>
>   
>
> Best regards,
>
> Hesham
>
>   
>
>   
>
>
>
> ---
> This email has been checked for viruses by Avast antivirus software.
> https://www.avast.com/antivirus
>


---------------------------------------------------------------------
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: Decrypt PDF to edit it then encrypt it

Posted by Tilman Hausherr <TH...@t-online.de>.
It is possible for older encryption (not AES256): get the source code 
and have a look at StandardSecurityHandler.java. There is a segment 
where "isOwnerPassword()" is called, and a bit later, "computedPassword" 
is calculated by calling getUserPassword(). I tried this by debugging 
with out test files and yes I got the user password.

So you could do it by 1) get length, revision and owner key from the 
PDEncryption object you get by calling document.getEncryption() 2) check 
that revision is smaller then 5, and 3) call getUserPassword().

Tilman

Am 03.01.2018 um 09:29 schrieb Hesham Gneady:
> Hello,
>
>   
>
> I have a PDF file that has an owner password & user password. I'd like to
> decrypt it using the owner password, make some modifications to it, then
> encrypt it back and save it. I did the first part successfully, but how can
> I encrypt it again using the same encryption settings it had, which I don't
> actually know? . For example, I know only the owner password, but not the
> user password.
>
>   
>
>   
>
> Best regards,
>
> Hesham
>
>   
>
>   
>
>
>
> ---
> This email has been checked for viruses by Avast antivirus software.
> https://www.avast.com/antivirus
>


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