Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PyPDF2 can't decrypt PDF files with Acrobat 6.0 or higher password security compatibility #378

Closed
apaksoy opened this issue Nov 13, 2017 · 20 comments · Fixed by #1157
Closed
Labels
is-bug From a users perspective, this is a bug - a violation of the expected behavior with a compliant PDF workflow-encryption From a users perspective, encryption is the affected feature/workflow

Comments

@apaksoy
Copy link

apaksoy commented Nov 13, 2017

Hi,

I get the "NotImplementedError: only algorithm code 1 and 2 are supported" error when I try the .decrypt() method on a PDF file saved with password security compatible with Acrobat 6.0 or higher.

Below is the code I used for decryption trials. Also below are the links to four different versions of the same file saved with the specified compatibilities in their filenames as examples. All have the same password 'password' (without the quotes).

I tried this with other PDF files and got the same result. That is, no error when saved with Acrobat 5.0 password security compatibility and always an error when saved with Acrobat 6.0 or higher password security compatibility.

I used Adobe Acrobat Pro X to save a file with different password security compatibilities. I have PyPDF2 1.26.0 installed on Python 3.6.3 on mac OS 10.13.1. Thank you.

from PyPDF2 import PdfReader

filenames = [
    "test_file_acrobat_5.pdf",
    "test_file_acrobat_6.pdf",
    "test_file_acrobat_7.pdf",
    "test_file_acrobat_10.pdf",
]

for filename in filenames:
    reader = PdfReader(filename)
    try:
        # print decryption outcome as 1 if successful and 0 otherwise
        print(filename, reader.decrypt("password"))
    except NotImplementedError as errMsg:
        print(filename, "can not be decrypted on error message", errMsg)

test_file_acrobat_5.pdf
test_file_acrobat_6.pdf
test_file_acrobat_7.pdf
test_file_acrobat_10.pdf

@apaksoy apaksoy changed the title PyPDF2 can't decrypt PDF files with Acrobat 6.0 or higher compatibility PyPDF2 can't decrypt PDF files with Acrobat 6.0 or higher password security compatibility Nov 13, 2017
@tataganesh
Copy link

tataganesh commented Nov 14, 2017

Possible duplicate #53?

@apaksoy
Copy link
Author

apaksoy commented Nov 14, 2017

I am not sure why but #53 seems to have been closed. This problem, however, still continues.

@tataganesh
Copy link

@apaksoy It seems as if no permanent fix has been made for this, but one of the solutions ( which suggests the use of the command-line utility qpdf ), worked fine for me. Could you try the same for your failed cases?

@apaksoy
Copy link
Author

apaksoy commented Nov 14, 2017

I will be away for about a week. Will try and post here when I return.

@maxiplux
Copy link

hello everybody , any news about this issue ?

@maxiplux
Copy link

maxiplux commented Nov 21, 2017

my friend add this line to your code inputpdf.decrypt('').

from PyPDF2 import PdfReader, PdfWriter

reader = PdfReader(full_file_name)
reader.decrypt('')
for index,temp_tracking_number in enumerate(self.additional_tracking_codes):
    writer = PdfWriter()
    writer.add_page(reader.pages[index])
    with open(str(temp_tracking_number) + ".pdf", "wb") as fh:
        writer.write(fh)

@apaksoy
Copy link
Author

apaksoy commented Nov 21, 2017

@ganeshtata Thank you for the suggestion but qpdf but does not work for me because I am looking for a solution within PyPDF2.

@maxiplux Thx but I am not exactly sure what you are suggesting as new. As I mentioned in my original post, the code I posted already works for PDF files with Acrobat 5.0 password security compatibility (or PDF files created via PyPDF2).

I did not paste the part of the code that copies the pages to a new PDF file and writes it to disk in the original post because it makes no difference if a file can not be decrypted at the first place. Can you pls also properly mark your code, i.e. use the grave accent ` in triple form, when you include code in your messages here?

@tataganesh
Copy link

@apaksoy The problem is, PyPDF2 doesn't support certain encryption algorithms, yet. This #226 indicates that decryption for some encryption algorithms is yet to be implemented. I know you are looking for a solution within PyPDF2, but in case you ever plan to venture out for a similar library, you can take a look at pikepdf, a python library that uses qpdf.

edwardvk added a commit to edwardvk/preview-generator that referenced this issue Mar 3, 2019
… '') using PyPDF2 py-pdf/pypdf#378 This is a workaround using external executable: qpdf
inkhey pushed a commit to algoo/preview-generator that referenced this issue Mar 12, 2019
… '') using PyPDF2 py-pdf/pypdf#378 This is a workaround using external executable: qpdf
@Fabian1337
Copy link

any update on this ?
i am not able to move with my system to qpdf so i am looking for some python based solution :/

@KrisLloyd
Copy link

I would also like updates regarding this issue

@c0nb4
Copy link

c0nb4 commented Nov 14, 2019

Work around for me was using PyMuPDF. Depending on the use case I either used it directly or saved the file again and the reopend it with the regular framework.

@Fabian1337
Copy link

@c0nb4 since PyMuPDF is based on MuPDF its under AGPL license and if you use it inside your project that you are distributing... you have to distibute as AGPL aswell or buy a license.

@dkhaitan82
Copy link

dkhaitan82 commented Sep 9, 2020

PikePDF can be used to open encrypted files (Created by Acrobat 6.0). Below is the simple code:
Output file is no longer password protected.

from pikepdf import Pdf
    with Pdf.open(v_in_file,password=v_password) as pdf:
        pdf.save(v_out_file)

@manohar9600
Copy link

Thanks bro, it worked.
I opened pdf using pikepdf and saved it. Reopened using PyPDF2.

@Sibgha360
Copy link

Sibgha360 commented Nov 2, 2021

PikePDF can be used to open encrypted files (Created by Acrobat 6.0). Below is the simple code: Output file is no longer password protected.

from pikepdf import Pdf
    with Pdf.open(v_in_file,password=v_password) as pdf:
        pdf.save(v_out_file)

Mine wasn't password-protected so removing the second argument worked.

[Edit]
For general information, I didn't need to open it using PyPDF2. I was working with Camelot API and the pdf read method threw the NonImplemented Error. It appears under the hood, Camelot is using PyPDF2. The above method worked for me.

@MartinThoma MartinThoma added is-bug From a users perspective, this is a bug - a violation of the expected behavior with a compliant PDF workflow-encryption From a users perspective, encryption is the affected feature/workflow labels Apr 7, 2022
@MartinThoma
Copy link
Member

MartinThoma commented Jun 11, 2022

Might be fixed with #749

Running the script from above:

$ python foo.py 
test_file_acrobat_5.pdf 1
test_file_acrobat_6.pdf can not be decrypted on error message only algorithm code 1 and 2 are supported. This PDF uses code 4
test_file_acrobat_7.pdf can not be decrypted on error message only algorithm code 1 and 2 are supported. This PDF uses code 4
test_file_acrobat_10.pdf can not be decrypted on error message only algorithm code 1 and 2 are supported. This PDF uses code 5

@MartinThoma
Copy link
Member

PyPDF2==2.4.0 with #1015 now supports R=6 decryption 🎉

@MartinThoma
Copy link
Member

Running the example from above, I get:

test_file_acrobat_5.pdf PasswordType.OWNER_PASSWORD
test_file_acrobat_6.pdf PasswordType.OWNER_PASSWORD
test_file_acrobat_7.pdf PasswordType.OWNER_PASSWORD
test_file_acrobat_10.pdf PasswordType.NOT_DECRYPTED

@MartinThoma
Copy link
Member

@exiledkingcc The test_file_acrobat_10.pdf still seems to fail. Do you have an idea why? I can decrypt it with "password" in my PDF reader. So it's definitely not a wrong password issue.

@exiledkingcc
Copy link
Contributor

it failed at verify_perms, which can be ignored.
i checked qpdf, it gnores if verify_perms failed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
is-bug From a users perspective, this is a bug - a violation of the expected behavior with a compliant PDF workflow-encryption From a users perspective, encryption is the affected feature/workflow
Projects
None yet
Development

Successfully merging a pull request may close this issue.