PyPDF4 icon indicating copy to clipboard operation
PyPDF4 copied to clipboard

Potential Encrypt Issue

Open mpbunch opened this issue 4 years ago • 3 comments

        [..., canvas, bytesio(), ...]
        
        can.save()
        new_pdf = PdfFileReader(packet)

        # Open template pdf
        existing_pdf = PdfFileReader(open(input_pdf_path, "rb"), strict=False)
        output = PdfFileWriter()
        # get first page of template
        page = existing_pdf.getPage(0)
        # merge first page of template with first page of canvas pdf
        page.mergePage(new_pdf.getPage(0))
        output.addPage(page)
        
        outputStream = open(self.output_pdf_path, "wb")
        # encrypt
        output.encrypt(user_pwd='', owner_pwd="password", use_128bit=True)
        # save
        output.write(outputStream)
        outputStream.close()

File generation works, file save works, file encryption doesn't. I want to set an admin password that restricts pdf editing and copying of text, while freely allowing users to view the pdf. When I open the pdf in acrobat pro, I am able to freely edit the pdf, even though acrobat says (SECURE) after the file name.

I'm not sure if this is an issue with my implementation, or within the library, but this does seem like a problem.

mpbunch avatar Aug 10 '20 15:08 mpbunch

Hi, I'm currently preparing a new version in a fork(https://github.com/pubpub-zz/PyPDF4). I made a tried as follow: `

import pypdf w=pypdf.PdfFileWriter(None,"E:/tst02.pdf") w.write("e:/tst02a.pdf") #not crypted w.encrypt("","tst") w.write("e:/tst02a.pdf") #protected file ` I opened it successfully with acrobat reader DC Can you give me your feed back with my proposed version ?(not yet released but attached in here)

pypdf4-1.27.0PPzz_1-py2.py3-none-any.whl.zip

thanks,

pubpub-zz avatar Aug 10 '20 17:08 pubpub-zz

@pubpub-zz The test outputted a pdf which could be opened in acrobat, but it was not secure, I could edit the file. PyPDF should have similar functionality as reportlab. https://www.reportlab.com/docs/PdfEncryptIntro.pdf

image image

mpbunch avatar Aug 18 '20 18:08 mpbunch

@mpbunch , I understand : actually what you are looking for is a capability to define permissions when encrypting the file. I've upgraded the API: encrypt(user_pwd, owner_pwd=None, use_128bit=True, permits=None, can_print=True, can_modify=True, can_copy=True, can_annotate=True, can_fill=True, can_extract=True, can_assemble=True, print_fullquality=True)

I've also added 2 functions applicable to both readers and writers: get_permissions(): return the encoded permissions as in integer (negative). Returns: • (int) – permissions as defined in document. If no permissions are defined, -1(all permitted) is returned Raises: • PdfReadError – if the document is encrypted and not yet decrypted.

decode_permissions(): return the encoded permissions as a human readable string Returns: • (string) – 0 substring of “can_print_high_res,can_print_low_res,can_modify,can_copy,can_annotate,can_fill,can_extract,can_assemble” Raises: • PdfReadError – if the document is encrypted and not yet decrypted.

Can you give me your feed back? pypdf4-1.27.0PPzz_1-py2.py3-none-any.whl.zip

pubpub-zz avatar Aug 21 '20 18:08 pubpub-zz