Why do I get a BouncyCastle NoClassDefFoundError?

I want to encrypt my PDF but there seems to be an error: PdfReader reader = new PdfReader("my.pdf"); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("new.pdf")); stamper.setEncry

I want to encrypt my PDF but there seems to be an error:

PdfReader reader = new PdfReader("my.pdf");
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("new.pdf"));
stamper.setEncryption("reader_password".getBytes(), "permission_password".getBytes(),
    PdfWriter.ALLOW_COPY | PdfWriter.ALLOW_PRINTING, PdfWriter.STANDARD_ENCRYPTION_128);
stamper.close();
Here's the error I get:

Exception in thread "main" java.lang.NoClassDefFoundError: org/bouncycastle/asn1/ASN1Encodable

Posted on StackOverflow on May 19, 2014 by wizclark99

When you look at the POM file for iText, you see the following dependencies:

org.bouncycastlebcprov-jdk15on1.49jarcompiletrueorg.bouncycastlebcpkix-jdk15on1.49jarcompiletrue

This means that you need the bcprov and the bcpkix jars version 1.49 from Bouncycastle. Please check your POM file.

In iText 7 PDF encryption should be done like this:

PdfReader reader = new PdfReader("my.pdf");
WriterProperties props = new WriterProperties()
        .setStandardEncryption("reader_password".getBytes(), "permission_password".getBytes(), EncryptionConstants.ALLOW_PRINTING,
                EncryptionConstants.ENCRYPTION_AES_128 | EncryptionConstants.DO_NOT_ENCRYPT_METADATA);
PdfWriter writer = new PdfWriter(new FileOutputStream("new.pdf"), props);
PdfDocument pdfDoc = new PdfDocument(reader, writer);
pdfDoc.close();

Click this link if you want to see how to answer this question in iText 5.


Share this article

Ready to use iText?

Try our iText 7 Library and add-ons FREE for 30 days. Test your proof of concept, and see if our solution is right for you.

Get my FREE trial
Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now