Encrypt PDF with aes_256 (cfpdf)

I need to encrypt a PDF file with aes_256. I have read that Adobe supports AES_256R5 and AES_256R6. This does not work properly with Lucee.
Is there an alternative solution for this?

I use the latest PDF snapshot and latest Lucee Version.


Related ticket for this issue in Jira: LDEV-4041 - CFPDF cannot read AES-256 encrypted files

1 Like

thank you for your response.
So what i can see it is an open issue / ticket.

Pretty sure it’s due to the old com.lowagie.text 2.17 aka itext library

there is this fork of iText 4, which probably resolves this issue


1 Like

Great thanks!
Is there any documentation how to use it?

the extension would need to updated to use that lib and handle any API changes between 2.17 and 4.x

@Vuish For now, I can set the AES-256 encryption to the pdf using below the workaround. hope maybe it will help you.

I downloaded the openPDF jar and bouncyCastle jar from the below links.

Put those jars in the directory and use that directory to create Objects
create an object using the createObject() with jar files path defined in the arguments otherwise the classes are created from the PDF extension itext-jar.

Note: we can’t perform any actions using the cfpdf tag on the AES-256 encrypted pdf created by using this workaround due to the [LDEV-4041] - Lucee

    // com.lowagie.text.pdf.PdfCopy
    // com.lowagie.text.pdf.PdfWriter
    // com.lowagie.text.Document
    // com.lowagie.text.pdf.PdfReader
    // java.io.FileOutputStream

    cfdocument(filename="#expandPath("./testnormal.pdf")#", overwrite="true") {
        writeoutput("test page 1");
        writeoutput("test page 2");

    try {
        // http://javadox.com/com.lowagie/itext/4.2.0/com/lowagie/text/pdf/PdfReader.html
        PdfReader = createObject("java", "com.lowagie.text.pdf.PdfReader", "#expandPath("./openPDF-lib")#");
        n = PdfReader.getNumberOfPages();

        // create OutputStream
        FileOutputStream = createObject("java", "java.io.FileOutputStream");

        // http://javadox.com/com.lowagie/itext/4.2.0/com/lowagie/text/Document.html
        Document = createObject("java", "com.lowagie.text.Document", "#expandPath("./openPDF-lib")#");
        // http://javadox.com/com.lowagie/itext/4.2.0/com/lowagie/text/pdf/PdfCopy.html
        PdfCopy = createObject("java", "com.lowagie.text.pdf.PdfCopy", "#expandPath("./openPDF-lib")#");
        writeDump(expand=false,var=PdfCopy.init(Document, FileOutputStream));

        // http://javadox.com/com.lowagie/itext/4.2.0/com/lowagie/text/pdf/PdfWriter.html#setEncryption(byte[],%20byte[],%20int,%20int)
        // setEncryption(byte[] userPassword, byte[] ownerPassword, int permissions, int encryptionType)
        // Sets the encryption options for this document.
        PdfCopy.setEncryption("userPassword".getBytes(), "ownerPassword".getBytes(), PdfCopy.ALLOW_COPY , PdfCopy.ENCRYPTION_AES_256_V3);

        // if you don't want to set password for the pdf then set the password as null value 
        // if you want to add more permissions then add them like PdfCopy.ALLOW_COPY + PdfCopy.ALLOW_PRINTING in the third arguments

        for (i = 1; i <= n; i++) {
			PdfCopy.addPage(PdfCopy.getImportedPage(PdfReader, i));

        f = PdfReader.getAcroForm();
		if (!isNull(f)) writer.copyAcroForm(PdfReader);
    finally {
        // FileOutputStream.close();