Why do I get a BouncyCastle NoClassDefFoundError? - iText 5 PDF Library Explained
I want to encrypt my PDF but there seems to be an error.
Here is the code:
PdfReader reader = new PdfReader("my.pdf");
PdfStamper stamper = new PdfStamper(reader, newFileOutputStream("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:
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.49</version>
<type>jar</type>
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcpkix-jdk15on</artifactId>
<version>1.49</version>
<type>jar</type>
<scope>compile</scope>
<optional>true</optional>
</dependency>
This means that you need the bcprov and the bcpkix jars version 1.49 from Bouncy Castle.
Since this FAQ was written, CVEs have been disclosed detailing vulnerabilities in older versions of Bouncy Castle. To resolve this, ensure you update to the latest security release of iText 5.5.x. See CVEs - iText 5 Documentation for more information.
If you are not using iText 5.5.x, please check the POM file as older versions of iText might require older versions of Bouncy Castle.