【问题标题】:Java compiler can't find the CMSProcessableInputStream for bouncycastleJava 编译器找不到 bouncycastle 的 CMSProcessableInputStream
【发布时间】:2015-07-19 14:58:56
【问题描述】:

我从以下链接中提取了这个示例。我遇到了问题,因为我无法让编译器找到 CMSProcessableInputStream 类。

有人有什么建议吗?

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

这是我的 pom.xml 的 sn-p:

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

【问题讨论】:

    标签: bouncycastle pdfbox


    【解决方案1】:

    1。 CMSProcessableInputStream 类是同一包中的 CreateSignature 类(没有“可见”一词)的一部分,您可以在 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。 PDFBox 1.8.9版本使用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 找到的源代码,风险自负。你不知道它是什么版本,或者它是否正确。试试先看官网吧。

    【讨论】:

    • 您好 Tilman,我刚刚尝试过,但仍然遇到相同的编译错误。
    • @DenisMP 抱歉,我意识到我的回答毫无价值。我做了更多的研究,希望这会有所帮助。
    • 为您的一般提示点赞。
    • 我很抱歉太密集了,但是我按照你的建议从link 复制了代码,我让我的 pom 看起来像你建议的那样,但我得到“CMSProcessableInputStream 无法解析为一个类型”。您还有其他建议吗?
    • @DenisMP 但 CMSProcessableInputStream 是在该文件中定义的?!此处解释了新错误的可能原因:stackoverflow.com/questions/5579558/…
    【解决方案2】:

    添加以下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>
    

    【讨论】:

    • 在一个项目中拥有不同版本的充气城堡工件可能最终会导致问题。
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 2016-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-16
      • 1970-01-01
      • 1970-01-01
      • 2015-06-27
      相关资源
      最近更新 更多