com.itextpdf.text.exceptions.InvalidPdfException:

2019-09-11 03:41发布

问题:

I am using iTextpdf to encrypt/decrypt PDF using pkcss11 usb token certificate.

Following is my encrypt and decrypt methods

public void encryptPdf(String src, String dest)
        throws IOException, DocumentException, CertificateException {
        PdfReader reader = new PdfReader(src);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
        Certificate cert = getPublicCertificate("C:\\Users\\USER\\Documents\\NetBeansProjects\\test\\src\\lk_enc.cer");
        stamper.setEncryption(new Certificate[]{cert},
            new int[]{PdfWriter.ALLOW_PRINTING}, PdfWriter.ENCRYPTION_AES_128);
        stamper.close();
        reader.close();
    }

public void decryptPdf(String src, String dest)
        throws IOException, DocumentException, GeneralSecurityException,CMSException {
        try{
//        decrypt(getPrivateKey(), DESTINATION_FILE, DECRYPTED_FILE);
            PrivateKey pkc = getPrivateKey();
        PdfReader reader = new PdfReader(src,
            getPublicCertificate("C:\\Users\\USER\\Documents\\NetBeansProjects\\test\\src\\lk_enc.cer"), pkc, "SunPKCS11-eToken");
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
        stamper.close();
        reader.close();
        }catch(Exception ex){
            System.out.println(ex);
        }
    }

I am using itextpdf 5.2.1 and bc 1.46 versions. Decryption works fine with pdf encrypted using encryptPdf() method.

But decryptPdf() mehtod fails with error com.itextpdf.text.exceptions.InvalidPdfException: key invalid in message when using a pdf which is encrypted with Adobe dc pro (128 AES) as source file .

Source PDFs

1.PDF encrypted with encryptPdf() method

2.PDF encrypted with adobe dc pro with AES 128bit

Thanks in advance