如何区分两个加密/安全的 PDF 文件 [英] How to distinguish between two encrypted / secured PDF files
问题描述
我有两个安全的 pdf 文件.一个有密码,另一个是安全的,但没有密码.我正在使用 PDF 框.如何识别哪个文件有密码,哪个文件是安全的但没有密码?
I have two secured pdf files. One has a password and the other one is secured but without password. I am using PDF Box. How can I identify which file has password and which one is secured but without password?
推荐答案
PDF 有两种加密方式 -
PDF's have two type of encryption -
- 所有者密码 - PDF 所有者/创建者设置的密码以限制其使用(例如编辑、打印、复制等)
- 用户密码 - 打开/查看 PDF 的密码设置
- Owner password - Password set by PDF owner / creator to restrict its usage (e.g. edit, print, copy etc)
- User password - Password set to open / view the PDF
PDF 可以只有所有者密码或两者都有;但不仅是用户密码.在任何一种情况下,PDF 都被称为加密,并且没有直接的 API 来区分两种加密.
PDF can have only owner password or both; but not only user password. In either case the PDF is termed to be encrypted and there is no direct API to distinguish between two kind of encryption.
对于PDFBox
,您可以使用以下代码片段来确定它是否已加密;并区分它是只有所有者密码还是两者都有.
In case of PDFBox
you can use below code snippet to determine if it is encrypted or not; and distinguish whether it has only owner password or both.
PDDocument pdfDoc = PDDocument.load(new File("path/to/pdf"));
boolean hasOwnerPwd = false;
boolean hasUserPwd = false;
if(pdfDoc.isEncrypted()){
hasOwnerPwd = true;
try{
StandardDecryptionMaterial sdm = new StandardDecryptionMaterial(null);
pdfDoc.openProtection(sdm);
hasUserPwd = true;
} catch(Exception e){
// handle exception
}
}
See PDFBox
API docs here and here.
编辑 感谢蒂尔曼指出最新的代码和替代方法来确定/区分两种加密.更新了代码片段并相应地发布.
EDIT Thanks to Tilman to point out latest code and alternate way to determine / distinguish between two encryption. Updated the code snippet and post accordingly.
这篇关于如何区分两个加密/安全的 PDF 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!