Error when trying to encrypt a PDF with itext: java.lang.SecurityException: class "org.bouncycastle.asn1.ASN1Primitive"'s

时间:2019-04-05 15:52:35

标签: encryption java-ee itext

I have a jee application where I generate a pdf and show it to the user on the browser. I have the request to protect the pdf for printing, so the user can´t make any other use of the document except reading. I am using itext 5 and I tried to encrypt the document but I get an error: java.lang.SecurityException: class "org.bouncycastle.asn1.ASN1Primitive"'s signer information does not match signer information of other classes in the same package"

I looked at some examples to encrypt, even in the book for itext5, but I am can´t fix that error. I also looked at some solutions to my problem, some say that is a dependencies problem, maybe duplicated but I still can´t find where, because I dont even have the bouncycastle added to my pom.

This is an extract of my pom.xml of web module.

<properties>
        <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <itext.version>5.5.13</itext.version>
    </properties>

    <dependecies>
        <dependency>
            <groupId>org.primefaces</groupId>
            <artifactId>primefaces</artifactId>
            <version>6.2</version>
        </dependency>
        <dependency>
            <groupId>javax.faces</groupId>
            <artifactId>javax.faces-api</artifactId>
            <version>2.2</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.itextpdf.tool</groupId>
            <artifactId>xmlworker</artifactId>
            <version>5.5.13</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>


        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.6</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>${itext.version}</version>

        </dependency>

        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itext-xtra</artifactId>
            <version>${itext.version}</version>

        </dependency>

    </dependencies>

The code I am trying to run is the next:

public void crearPDFconTextoHTML(String nombreArchivo, String texto) {
        try {
            String dirCarpetaTemp = directorio();//Creo el directorio temporal sin los archivos
            String dirFinalArchivo = dirCarpetaTemp + nombreArchivo;//Directorio completo del archivo
            FileOutputStream fos = new FileOutputStream(dirFinalArchivo);
            Document document = new Document(PageSize.A4);
            String USER_PASS = "Hello123";
            String OWNER_PASS = "Owner123";
            PdfWriter writer = PdfWriter.getInstance(document, fos);
            writer.setEncryption(USER_PASS.getBytes(), OWNER_PASS.getBytes(),
                    PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128);
            document.setMarginMirroring(true);
            document.open();

            Paragraph parrafo = new Paragraph();
            parrafo = convertirHtmlConFormatoAParrafoPdfOld(parrafo, texto);
//            parrafo = convertirTexto(parrafo, texto);

            parrafo.setAlignment(Element.ALIGN_JUSTIFIED);
            document.add(parrafo);

            document.close();//cierro el documento
            writer.close();//cierro la escritura

            setNombreArchivo(nombreArchivo);//Agregar el nombre archivo final a la dependencia
//            OutputStream fos = new FileOutputStream(dirCarpetaTemp + nombreArchivo);//Crear archivo final
//            PdfTempUtil.doMerge(listPdfs, fos);//Combinar pdfs que se agregaron en la lista
            generarLinksAccesoAlPdf();//Generar la url que se mostrará en la lista
        } catch (DocumentException | IOException ex) {
            System.out.println(ex);
        }
    }

The dependencies should be fine and the encryption should work so the user can´t print the pdf when he open it.

1 个答案:

答案 0 :(得分:1)

  

依赖关系应该很好[...]

没有问题是依赖关系。这可能不是直接依赖关系,而是transitive dependencies-依赖关系的依赖关系(我们在这里讨论子项,孙子项和孙子项[...]进行比较) iText 5.5.13需要Bouncy Castle版本1.49:

  <dependency>
      <groupId>org.bouncycastle</groupId>
      <artifactId>bcprov-jdk15on</artifactId>
      <version>1.49</version>
    </dependency>
    <dependency>
      <groupId>org.bouncycastle</groupId>
      <artifactId>bcpkix-jdk15on</artifactId>
      <version>1.49</version>
    </dependency>

您的Primefaces具有较旧的iText版本

    <dependency>
        <groupId>com.lowagie</groupId>
        <artifactId>itext</artifactId>
        <version>2.1.7</version>
    </dependency>

依次获取

<dependencies>
    <dependency>
        <groupId>bouncycastle</groupId>
        <artifactId>bcmail-jdk14</artifactId>
        <version>138</version>
    </dependency>
    <dependency>
        <groupId>bouncycastle</groupId>
        <artifactId>bcprov-jdk14</artifactId>
        <version>138</version>
    </dependency>
    <dependency>
        <groupId>bouncycastle</groupId>
        <artifactId>bctsp-jdk14</artifactId>
        <version>138</version>
    </dependency>
</dependencies>

检查您的依赖关系(在EAR中),并查看您是否具有正确的充气城堡版本。有时,甚至您的应用服务器也能做到这一点。如果找到了罪魁祸首,请在您的pom中添加一个<exclusion>部分...