【问题标题】:Error when trying to encrypt a PDF with itext: java.lang.SecurityException: class "org.bouncycastle.asn1.ASN1Primitive"'s尝试使用 itext 加密 PDF 时出错:java.lang.SecurityException: class "org.bouncycastle.asn1.ASN1Primitive"'s
【发布时间】:2019-04-05 15:52:34
【问题描述】:

我有一个 jee 应用程序,我在其中生成 pdf 并在浏览器上将其显示给用户。我要求保护 pdf 以供打印,因此用户除了阅读之外不能对文档进行任何其他用途。我正在使用 itext 5 并尝试加密文档,但出现错误:java.lang.SecurityException:类“org.bouncycastle.asn1.ASN1Primitive”的签名者信息与同一包中其他类的签名者信息不匹配"

我查看了一些加密示例,甚至在 itext5 的书中,但我无法修复该错误。我还查看了我的问题的一些解决方案,有人说这是一个依赖问题,可能是重复的,但我仍然找不到在哪里,因为我什至没有将 bouncycastle 添加到我的 pom 中。

这是我的 web 模块的 pom.xml 的摘录。

<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);
        }
    }

依赖项应该没问题,加密应该可以工作,这样用户在打开 pdf 时就无法打印它。

【问题讨论】:

    标签: encryption jakarta-ee itext


    【解决方案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 中添加一个&lt;exclusion&gt; 部分...

    【讨论】:

    • ¡我用 maven 添加了 2.1.7 版本,它可以工作了!,坏事是我已经在使用版本 5,所以我需要弄清楚我是否必须更改我的一些代码使用这个版本 2。顺便说一下,我不明白为什么 primefaces 没有上传到版本 5,考虑到即使 5 是旧的,而 7 是实际版本。
    • 短版:这可能是由于许可问题。 iText 2.X 可以在商业环境中使用(无需付费),但 5.X 或 7.X 是 AGPL,所以它们不是。一种替代方法可能是 OpenPDF,它是 2.X 的一个分支...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多