Skip to main content
Skip table of contents

Why do I get a BouncyCastle NoClassDefFoundError?

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

Here is the code:

JAVA
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:

XML
<dependency>
	<groupId>org.bouncycastle</groupId>
	<artifactId>bcprov-jdk18on</artifactId>
	<version>${bouncycastle.version}</version>
</dependency>

This means that you need the bcprov jar from Bouncycastle. Please check your POM file.

In iText 7 PDF encryption should be done like this:

CODE
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();

 

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.