Mostafizur
Mostafizur

Reputation: 69

How to check if a file is password protected /encrypted or not in java

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

Upvotes: 0

Views: 9279

Answers (1)

Mike Adamenko
Mike Adamenko

Reputation: 3002

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

Upvotes: 2

Related Questions