IText解保受保护的PDF

科普:PDF加密有两种方式,分别为owner password和user password。它们有什么区别呢?简单来说,设置了user password,在打开PDF的时候就需要输入密码;设置了owner password,可以打开PDF,但是不能修改、拷贝里面的内容。

本文讲述的是破解owner password。

· 重写PdfReader

import com.itextpdf.text.pdf.PdfReader;

import java.io.IOException;
import java.io.InputStream;

public class MyPdfReader extends PdfReader {
    public MyPdfReader(final String filename) throws IOException {
        super(filename);
    }
    
    public MyPdfReader(final byte[] bytes) throws IOException {
        super(bytes);
    }
    
    public MyPdfReader(final InputStream is) throws IOException {
        super(is);
    }

    public void decryptOnPurpose() {
        encrypted = false;
    }
}

· 实现解保护

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.exceptions.BadPasswordException;
import com.itextpdf.text.exceptions.InvalidPdfException;
import com.itextpdf.text.pdf.PdfStamper;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class UnProtectedPDFUtil {

    private static MyPdfReader pdfReader = null;

    public static byte[] unProtected(byte[] fileBytes) throws Exception {
        ByteArrayOutputStream bos = null;
        ByteArrayInputStream bis = null;

        try {
            bos = new ByteArrayOutputStream();
            bis = new ByteArrayInputStream(fileBytes);

            initPdfReader(bis);

            if (checkProtected()) {
                unProtectedPdf(bos);
                byte[] newBytes = bos.toByteArray();
                bos.flush();
                return newBytes;
            } else {
                return fileBytes;
            }
        } catch (Exception e) {
            throw e;
        } finally {
            if (null != bos) {
                bos.close();
            }
            if (null != bis) {
                bis.close();
            }
        }
    }

    /**
     * Will check user password or owner password. If user password, will throws
     * BadPasswordException.
     * 
     * @param is
     * @throws BadPasswordException
     */
    private static void initPdfReader(final InputStream is) throws BadPasswordException {
        try {
            pdfReader = new MyPdfReader(is);
        } catch (BadPasswordException e) {
            // if catch BadPasswordException. it should be user password.
            throw new BadPasswordException("Bad password. It should be user password.");
        } catch (IOException e) {
            return;
        }
    }

    private static boolean checkProtected() throws InvalidPdfException {
        if (null == pdfReader) {
            throw new InvalidPdfException("Invalid pdf.");
        }

        // check is owner password (protected).
        if (pdfReader.isEncrypted()) {
            return true;
        } else {
            return false;
        }
    }

    private static void unProtectedPdf(OutputStream os) {
        PdfStamper stamper = null;
        try {
            MyPdfReader.unethicalreading = true;
            pdfReader.decryptOnPurpose();

            stamper = new PdfStamper(pdfReader, os);
        } catch (DocumentException e) {
            return;
        } catch (IOException e) {
            return;
        } finally {
            try {
                if (null != stamper) {
                    stamper.close();
                }
            } catch (Exception e) {
                return;
            }
        }
    }
}

· 使用样例

byte[] unProtectedBytes = UnProtectedPdfUtil.unProtected(byte[] fileBytes);
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1.ios高性能编程 (1).内层 最小的内层平均值和峰值(2).耗电量 高效的算法和数据结构(3).初始化时...
    欧辰_OSR阅读 29,789评论 8 265
  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,172评论 1 32
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,269评论 19 139
  • 1,当执行scanf函数的时候,并不是直接让我们从键盘中输入的,而是先检查缓冲区中是否有数据,如果有数据才会从缓冲...
    ITman007阅读 604评论 0 0
  • 【长篇原创】图文:风听雨夜寐荷 但行好事,莫问轮回! 笑儿走出会场,打量着整洁有序的校园。 一排修剪的恰到好处的冬...
    风听雨夜寐荷阅读 344评论 3 3