简体   繁体   English

如何在Java中检查文件是否受密码保护/加密

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

是否有任何Java API可以检查PDF和Office文件格式-返回受密码保护/加密的文件列表?

You can use POI for office documents encryption. 您可以将POI用于Office文档加密。

The below example for Office XML-based formats (.xlsx, .pptx, .docx, ...). 以下示例基于Office XML的格式(.xlsx,.pptx,.docx等)。

XML-based formats are stored in OLE-package stream "EncryptedPackage". 基于XML的格式存储在OLE包流“ EncryptedPackage”中。 Use org.apache.poi.poifs.crypt.Decryptor to decode file: 使用org.apache.poi.poifs.crypt.Decryptor解码文件:

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. 如果要读取使用内置密码加密的文件,请使用Decryptor.DEFAULT_PASSWORD。

And you can use iText pdf API to identify the password protected PDF. 而且,您可以使用iText pdf API来识别受密码保护的PDF。

Example : 范例:

try {
        new PdfReader("C:\\Password_protected.pdf");            
    } catch (BadPasswordException e) {
        System.out.println("PDF is password protected..");
    } catch (Exception e) {
        e.printStackTrace();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM