0

Is there any Java API that can check PDF and Office file formats - return list of files those are Password Protected / encrypted?

Mostafizur
  • 69
  • 2
  • 9
  • Have you tried any coding on your own? – ItamarG3 May 31 '17 at 09:17
  • yes, i tried with Pdfbox to handle pdf file but i need an API which can handle both PDF and office file format too. – Mostafizur May 31 '17 at 09:20
  • Re PDFBox see https://stackoverflow.com/questions/14806868/how-to-check-pdf-file-is-password-protected-i and https://stackoverflow.com/questions/15896691/how-to-check-if-a-pdf-is-password-protected-or-not . Note that some files are encrypted but have an empty user password, this is for restricted rights. – Tilman Hausherr May 31 '17 at 11:12

1 Answers1

2

You can use POI for office documents encryption.

The below example for Office XML-based formats (.xlsx, .pptx, .docx, ...).

XML-based formats are stored in OLE-package stream "EncryptedPackage". Use org.apache.poi.poifs.crypt.Decryptor to decode file:

EncryptionInfo info = new EncryptionInfo(filesystem);
Decryptor d = Decryptor.getInstance(info);

try {
    if (!d.verifyPassword(password)) {
        throw new RuntimeException("Unable to process: document is encrypted");
    }

    InputStream dataStream = d.getDataStream(filesystem);

    // parse dataStream

} catch (GeneralSecurityException ex) {
    throw new RuntimeException("Unable to process encrypted document", ex);
}

If you want to read file encrypted with build-in password, use Decryptor.DEFAULT_PASSWORD.

And you can use iText pdf API to identify the password protected PDF.

Example :

try {
        new PdfReader("C:\\Password_protected.pdf");            
    } catch (BadPasswordException e) {
        System.out.println("PDF is password protected..");
    } catch (Exception e) {
        e.printStackTrace();
    }
Mike Adamenko
  • 2,944
  • 1
  • 15
  • 28
  • The first Part of the answer is working for .docx file format but how to handle for .doc file and open office file format.. – Mostafizur Jun 05 '17 at 17:51
  • @Mostafizur: check [my answer](https://stackoverflow.com/questions/45238486#45337641) for doc/docx – kiwiwings Jul 31 '17 at 12:39