我从以下链接中提取了此示例。我遇到了问题,因为我无法让编译器找到CMSProcessableInputStream类。
有人有任何建议吗?
这是我的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");
}
答案 0 :(得分:2)
1。 CMSProcessableInputStream类是CreateSignature类的一部分(没有单词“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。
一般提示:使用谷歌发现的源代码需要您自担风险。您不知道它是什么版本,或者它是否正确。请先试试官方网站。
答案 1 :(得分:1)
添加以下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>
答案 2 :(得分:0)
我从这里将必要的类复制到我的项目中(包括CMSProcessableInputStream和CreateSignatureBase): https://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/signature/