Encrypt PDF with aes_256 (cfpdf)

Hi,
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.

Thanks

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

1 Like

Hi,
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

https://luceeserver.atlassian.net/browse/LDEV-3885

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

<cfscript>
    // 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");
        cfdocumentitem(type="pagebreak");
        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")#");
        writeDump(expand=false,var=PdfReader.init(expandPath("./testnormal.pdf")));
        n = PdfReader.getNumberOfPages();

        // create OutputStream
        FileOutputStream = createObject("java", "java.io.FileOutputStream");
        writeDump(expand=false,var=FileOutputStream.init(expandPath("./test-AES-encrypted.pdf")));

        // http://javadox.com/com.lowagie/itext/4.2.0/com/lowagie/text/Document.html
        Document = createObject("java", "com.lowagie.text.Document", "#expandPath("./openPDF-lib")#");
        writeDump(expand=false,var=Document.init(PdfReader.getPageSizeWithRotation(1)));
        
        // 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

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

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

</cfscript>