Java 编译器找不到 bouncycastle 的 CMSProcessableInputStream

Java compiler can't find the CMSProcessableInputStream for bouncycastle

我从下面的 link 中提取了这个例子。我遇到了问题,因为我无法让编译器找到 CMSProcessableInputStream class.

有人有什么建议吗?

https://apache.googlesource.com/pdfbox/+/5f032354760374773f7339bbad2678d3281e90ee/examples/src/main/java/org/apache/pdfbox/examples/signature/CreateVisibleSignature.java

这是我的 pom.xml 的片段:

        <dependency>
        <groupId>org.apache.pdfbox</groupId>
        <artifactId>pdfbox</artifactId>
        <version>1.8.9</version>
    </dependency>
   <dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.6</version>
   </dependency>
    <dependency>
        <groupId>org.bouncycastle</groupId>
        <artifactId>bcpg-jdk16</artifactId>
        <version>1.46</version>
    </dependency>
    <dependency>
        <groupId>org.bouncycastle</groupId>
        <artifactId>bcprov-jdk16</artifactId>
        <version>1.46</version>
    </dependency>
    <dependency>
        <groupId>org.bouncycastle</groupId>
        <artifactId>bcmail-jdk16</artifactId>
        <version>1.46</version>
    </dependency>
    <dependency>
        <groupId>org.bouncycastle</groupId>
        <artifactId>bcprov-ext-jdk16</artifactId>
        <version>1.46</version>
    </dependency>

这是代码:

    @Override
public byte[] sign(InputStream content) throws SignatureException,
        IOException {
    CMSProcessableInputStream input = new CMSProcessableInputStream(content);
    CMSSignedDataGenerator gen = new CMSSignedDataGenerator();
    // CertificateChain
    List<Certificate> certList = Arrays.asList(cert);
    CertStore certStore = null;
    try {
        certStore = CertStore.getInstance("Collection",
                new CollectionCertStoreParameters(certList), provider);
        gen.addSigner(privKey, (X509Certificate) certList.get(0),
                CMSSignedGenerator.DIGEST_SHA256);
        gen.addCertificatesAndCRLs(certStore);
        CMSSignedData signedData = gen.generate(input, false, provider);
        return signedData.getEncoded();
    } catch (Exception e) {
        // should be handled
        System.err.println("Error while creating pkcs7 signature.");
        e.printStackTrace();
    }
    throw new RuntimeException("Problem while preparing signature");
}

1。 CMSProcessableInputStream class 是 CreateSignature class(没有 "visible" 一词)的一部分,它位于同一个包中,您可以在 PDFBox 的源代码下载中找到它。在这里获取: https://pdfbox.apache.org/downloads.html#recent 然后点击 "pdfbox-1.8.9-src.zip",然后在 zip 文件中查找 pdfbox-1.8.9\examples\src\main\java\org\apache\pdfbox\examples\signature\CreateSignature.java。

2。 1.8.9版本的PDFBox使用bc 1.44版本,as shown on the official website:

<dependency>
  <groupId>org.bouncycastle</groupId>
  <artifactId>bcprov-jdk15</artifactId>
  <version>1.44</version>
</dependency>
<dependency>
  <groupId>org.bouncycastle</groupId>
  <artifactId>bcmail-jdk15</artifactId>
  <version>1.44</version>
</dependency>

另一种解决方案是使用 pdfbox-app,其中包含 bc。

一般提示:使用通过 google 找到的源代码需要您自担风险。您不知道它是什么版本,或者它是否正确。先看看官网试试

我从这里复制了必要的 类 到我的项目(包括 CMSProcessableInputStream 和 CreateSignatureBase): https://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/signature/

添加以下 2 个依赖项:

 <dependency>
        <groupId>org.bouncycastle</groupId>
        <artifactId>bcpkix-jdk15on</artifactId>
        <version>1.60</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.bouncycastle/bcmail-jdk15 -->
    <dependency>
        <groupId>org.bouncycastle</groupId>
        <artifactId>bcmail-jdk15</artifactId>
        <version>1.46</version>
    </dependency>