记得以前看到pdf资料,看到感兴趣的,想复制下来。可是因为pdf安全设置的原因,不能复制。特别是看一些技术性文档,看到一些代码,想自己跑,看一下结果。结果却不能复制。但是一行一行敲代码,又是费时费力。不久前看itext源码,发现居然可以用再次加密的方式,改变pdf的权限。
源码版本:itext5.0.5
条件:有文件打开密码
关键点:PdfReader.unethicalreading
……
if (openPassword == null) {
reader = new PdfReader(fis);
} else {
reader = new PdfReader(fis, openPassword.getBytes());
}
PdfReader.unethicalreading = true;//不为true不能成功。
PdfEncryptor.encrypt(reader, fos, true, userPassword, OwnerPassword, PdfWriter.ALLOW_COPY | PdfWriter.ALLOW_PRINTING);
……
是什么原因呢,看一下itext对打开权限判断:
/**
* Checks if the document was opened with the owner password so that the end application
* can decide what level of access restrictions to apply. If the document is not encrypted
* it will return true
.
* @return true
if the document was opened with the owner password or if it's not encrypted,
* false
if the document was opened with the user password
*/
public final boolean isOpenedWithFullPermissions() {
return !encrypted || ownerPasswordUsed || unethicalreading;
}
而unethicalreading的定义又是:
public static boolean unethicalreading = false;
所以,只要unethicalreading 是true,就会被认为是有权限的!而这个unethicalreading又是开放的,这是不是itext故意留的后门呢,哈哈?
通过这个代码,我们可以设置自己想要的权限。我们也有自己设置的打开密码和所有密码。
当然,这份pdf也会有一个带锁的标志。因为它是加密了的。
通过稍微改一下代码,我们就可以完全解密。呵呵,这里就不说了。