Silvio Langereis
Silvio Langereis

Reputation: 471

Passing a password into the pdf security handler

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 ?

Upvotes: 1

Views: 766

Answers (2)

Ryan
Ryan

Reputation: 2570

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));

Upvotes: 1

Pacific Bird
Pacific Bird

Reputation: 298

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

Upvotes: 0

Related Questions