1

I'm currently working on a project where pdf's can be decrypted after a successful api call that returns the password. I've browsed through SO and pdftron SDK but can't find a definitive solution on how to insert this password into the security handler.

Things I have tried:

None of the GetSecurityHandler() methods seem to handle password insertion:

 SecurityHandler handler = m_PdfDocument.GetSecurityHandler();

Takes a password string but throws error:

m_PdfDocument.InitStdSecurityHandler(pwd);

error: Message: Not a standard security handler. The custom filter needs to be registered.

Judging from the message I assumed I needed m_PdfDocument.InitSecurityHandler() instead, but that method doesn't take a string, only int.

Anyone can bump me onto the right track ?

Silvio Langereis
  • 477
  • 1
  • 11
  • 19
  • Are you able to open the PDF in a PDF reader, such as Foxit/Adobe? Does this happen for any password protected file, or only certain ones? Did you encrypt the file? How? – Ryan Mar 23 '17 at 17:55
  • In another reader, it says the file is encrypted. The files we need to open, are encrypted educational files. They are created by another firm we work with, they provide pdf's with DRM protection to students with learning disabilites, our tts software will be able to decrypt the file after a successfull apicall. If the call is successfull, the password is returned. Everything is order, I just have to put the password into the pdf for decryption. The user cannot know the password, I need to pass the password programmatically. – Silvio Langereis Mar 24 '17 at 07:56
  • Also: The encryption itself is developed with pdftron, but outsourced by the firm we work with. After some research, we concluded we need to initialize the custom security handler with an ID (the int you need to pass), but since they haven't programmed it, they have no idea either... – Silvio Langereis Mar 24 '17 at 07:58
  • The question is if the file is standard encrypted, or custom encrypted. Are you able to send one of the files to support at pdftron, or post here, along with the password. If not, there is a tool in the windows PDFNet SDK called COSEdit. You can open the file in that (skip the password entry), and check the /trailer/Encrypt/Filter key and post here. – Ryan Mar 24 '17 at 16:13
  • It's custom encrypted. I cannot open them with COSEdit, it gives an error right away. I'll send in a file with the password to pdftron support. – Silvio Langereis Mar 24 '17 at 16:30

2 Answers2

1

Thank you for sending the file. This file is encrypted using custom encryption. Your DRM. No PDF reader can open the file, but your own custom PDF reader.

To open the PDF with PDFNet, you need to find out how the file was encrypted in the first place, and essentially do the opposite. I assume the other team that did the encryption was also decrypting, for at least testing purposes?

It might as simple as following example 3 in our Encryption sample. In which case you just need to register under the filter name that the other team used. I think I know what that is, but won't post here, and will email you instead.

But for others, if the PDF was encrypted with a filter called "Frodo", then it would be

CreateDelegate frodo_del = new CreateDelegate(FrodoSecurityHandler.Create);
SecurityManagerSingleton.Instance().RegisterSecurityHandler("Frodo", new SecurityDescriptor("Frodo Security", frodo_del));
Ryan
  • 2,200
  • 1
  • 8
  • 11
0

Well according to this page, GetSecurityHandler() is used after you initialize another handler, so since InitSecurityHandler() takes an int you could do this

    string password = "9quali52ty3";

    // Convert the string into a byte[].
    byte[] asciiBytes = Encoding.ASCII.GetBytes(password);
    string compiledBytes = System.Text.Encoding.ASCII.GetString(asciiBytes);
    int convertedBytes = int.Parse(compiledBytes);
    m_PdfDocument.InitSecurityHandler(convertedBytes);
    m_PdfDocument.GetSecurityHandler();

A good rule of thumb for programming: There is always a way to get from one datatype to another. Credit to: @Brig Lamoreaux, @Zanoni and @Brandon on the following pages.
Brig Zanoni Brandon

Community
  • 1
  • 1
Sharp_
  • 234
  • 3
  • 12