-3

I have a password protected PDF file and I need to Decrypt it using PDFKit or some other way, When I use unlock with password, it is unlocked but it is still encrypted could anyone help me with this code:

func handlePDF(url: URL, password: String) {                      
    print(pdfDocument.isEncrypted, pdfDocument.isLocked) // true, true
                            
    if let pdfDocument = PDFDocument(url: url) {
        if pdfDocument.isEncrypted {
            if pdfDocument.unlock(withPassword: password) {
                print(pdfDocument.isEncrypted, pdfDocument.isLocked) //true , false
            }
        }
    }
}
koen
  • 5,383
  • 7
  • 50
  • 89
Dinesh
  • 167
  • 2
  • 11

1 Answers1

1

As the documentation explains, your result is expected and correct:

 isEncrypted: Bool { get }

true if the document is encrypted, whether it is locked or unlocked; false otherwise.

(My italics.) So don't worry, be happy.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • yes but when is pass the PDF data to the server it asks for password. so how can I get the Decrypted PDF data – Dinesh Mar 01 '22 at 11:34
  • My question is "How to Decrypt a PDF" – Dinesh Mar 01 '22 at 11:45
  • You have decrypted the _data_. That is all you need to do. – matt Mar 02 '22 at 14:44
  • All I want is to make the "isEncrypted" to be false – Dinesh Mar 02 '22 at 14:46
  • You cannot do that. The file _is_ an encrypted file. If you want an unencrypted file, that would be a _different_ file. Do you see? – matt Mar 02 '22 at 14:47
  • Yes that is what I need, I want an unencrypted file of the encrypted file – Dinesh Mar 02 '22 at 14:48
  • So go ahead and make that. – matt Mar 02 '22 at 14:49
  • 1
    Think about it like this. Start with an encrypted PDF on your desktop. Try to open it. You can't until you give the password. You give the password. You can now read the PDF! OK, close the PDF. Now try to open it. You still can't until you give the password again! That's because the PDF is _still encrypted_. You can't change that. You can read it because you know the password, but you can't claim that an encrypted PDF is not encrypted; it _is_ encrypted. – matt Mar 02 '22 at 14:54
  • I want to do that through my app, so any idea how could I convert encrypted file data to unencrypted file data? – Dinesh Mar 02 '22 at 14:54
  • Just write the data out as new file that isn't encrypted. – matt Mar 02 '22 at 14:54
  • 1
    Again, think about how you would do this on the desktop. You'd open the encrypted PDF by giving the password. You'd select all the pages. You'd copy them. You'd create a new PDF document. You'd paste the pages into it. You'd save. Try it yourself! – matt Mar 02 '22 at 14:57