2015-07-19 67 views
1

我从下面的链接中找到了这个例子。我有问题,因为我无法让编译器找到CMSProcessableInputStream类。Java编译器找不到bouncycastle的CMSProcessableInputStream

有没有人有任何建议?

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

1. 的CMSProcessableInputStream类是CreateSignature类的一部分(没有“可见”一词),它位于相同的包中,您可以在PDFBox的源代码下载中找到它。在这里获取: https://pdfbox.apache.org/downloads.html#recent 并点击“pdfbox-1.8.9-src.zip”,然后查找pdfbox-1.8.9 \ examples \ src \ main \ java \ org \ apache \ pdfbox \ examples \ signature \在zip文件中创建Signature.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应用内,其具有内BC。

一般提示:使用您在谷歌上找到的源代码需要您自担风险。你不知道它是什么版本,或者它是否正确。先试着看官方网站。

+0

嗨蒂尔曼,我刚试过,但我仍然得到同样的编译错误。 – DenisMP

+0

@DenisMP对不起,我意识到我的答案毫无价值。我已经做了更多的研究,希望这会有所帮助。 –

+0

三倍赞成你的*一般提示*。 – mkl