我在线程"主要"中获得了异常。 java.lang.NoSuchMethodError:org.bouncycastle.asn1.ASN1InputStream.readObject()Lorg / bouncycastle / asn1 / ASN1Primitive;
这是我的TestSign.java
package com.test.sign;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.PrivateKey;
import java.security.Security;
import java.security.cert.Certificate;
import java.util.Properties;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfAnnotation;
import com.itextpdf.text.pdf.PdfAppearance;
import com.itextpdf.text.pdf.PdfFormField;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfSignatureAppearance;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.security.ExternalSignature;
import com.itextpdf.text.pdf.security.MakeSignature.CryptoStandard;
import com.itextpdf.text.pdf.security.BouncyCastleDigest;
import com.itextpdf.text.pdf.security.ExternalDigest;
import com.itextpdf.text.pdf.security.PrivateKeySignature;
import com.itextpdf.text.pdf.security.MakeSignature;
public class TestSign {
/** The resulting PDF */
public static String ORIGINAL = "C://results/xmlworker/test3.pdf";
/** The resulting PDF */
public static String SIGNED1 = "results/part3/chapter12/signed_1.pdf";
/** The resulting PDF */
public static String SIGNED2 = "C://results/xmlworker/test3yfguhik.pdf";
/** One of the resources. */
public static final String RESOURCE
= "resources/img/1t3xt.gif";
/**
* A properties file that is PRIVATE.
* You should make your own properties file and adapt this line.
*/
//public static String PATH = "c:/home/blowagie/key.properties";
/** Some properties used when signing. */
//public static Properties properties = new Properties();
/**
* Creates a PDF document.
* @param filename the path to the new PDF document
* @throws DocumentException
* @throws IOException
*/
public void createPdf(String filename) throws IOException, DocumentException {
// step 1
Document document = new Document();
// step 2
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
// step 3
document.open();
// step 4
document.add(new Paragraph("Hello World!"));
PdfFormField field = PdfFormField.createSignature(writer);
field.setWidget(new Rectangle(72, 732, 144, 780), PdfAnnotation.HIGHLIGHT_INVERT);
field.setFieldName("mySig");
field.setFlags(PdfAnnotation.FLAGS_PRINT);
field.setPage();
field.setMKBorderColor(BaseColor.BLACK);
field.setMKBackgroundColor(BaseColor.WHITE);
PdfAppearance tp = PdfAppearance.createAppearance(writer, 72, 48);
tp.rectangle(0.5f, 0.5f, 71.5f, 47.5f);
tp.stroke();
field.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, tp);
writer.addAnnotation(field);
// step 5
document.close();
}
/**
* Manipulates a PDF file src with the file dest as result
* @param src the original PDF
* @param dest the resulting PDF
* @throws GeneralSecurityException
* @throws IOException
* @throws DocumentException
* @throws FileNotFoundException
* @throws KeyStoreException
* @throws Exception
*/
public void signPdf(String src, String dest, boolean certified, boolean graphic) throws GeneralSecurityException, IOException, DocumentException {
// private key and certificate
KeyStore ks = KeyStore.getInstance("pkcs12");
ks.load(new FileInputStream("C://results/xmlworker/Test.pfx"), "10111995".toCharArray());
String alias = (String)ks.aliases().nextElement();
PrivateKey pk = (PrivateKey)ks.getKey(alias, "10111995".toCharArray());
Certificate[] chain = ks.getCertificateChain(alias);
// reader and stamper
PdfReader reader = new PdfReader(ORIGINAL);
PdfStamper stamper = PdfStamper.createSignature(reader, new FileOutputStream(dest), '\0');
// appearance
PdfSignatureAppearance appearance = stamper.getSignatureAppearance();
appearance.setVisibleSignature("mySig");
appearance.setReason("It's personal.");
appearance.setLocation("Foobar");
if (certified)
appearance.setCertificationLevel(PdfSignatureAppearance.CERTIFIED_NO_CHANGES_ALLOWED);
if (graphic) {
appearance.setSignatureGraphic(Image.getInstance(RESOURCE));
appearance.setRenderingMode(PdfSignatureAppearance.RenderingMode.GRAPHIC);
}
// signature
ExternalSignature es = new PrivateKeySignature(pk, "SHA-256", "BC");
ExternalDigest digest = new BouncyCastleDigest();
MakeSignature.signDetached(appearance, digest, es, chain, null, null, null, 0, CryptoStandard.CMS);
}
/**
* Main method.
*
* @param args no arguments needed
*/
public static void main(String[] args)
throws Exception {
//Security.addProvider(new BouncyCastleProvider());
TestSign signatures = new TestSign();
signatures.createPdf(ORIGINAL);
//signatures.signPdf(ORIGINAL, SIGNED1, false, false);
signatures.signPdf(ORIGINAL, SIGNED2, true, false);
}
}
的pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>exemple</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>exemple Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.4.0</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.56</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcmail-jdk15on</artifactId>
<version>1.56</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
我已经尝试了所有版本的bcprov-jdk15on和bcmail-jdk15on,你能帮助我吗,我在这个问题上被困了一个星期?
我已经复制了此Exemple
中的代码答案 0 :(得分:2)
您在此版本的iText中使用了错误版本的BouncyCastle。
我建议您使用iText 5.5.11和BouncyCastle 1.49的组合,如iText POM中所示:https://github.com/itext/itextpdf/blob/5.5.11/itext/pom.xml
<artifactId>itextpdf</artifactId>
<version>5.5.11</version>
<dependencies>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.49</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcpkix-jdk15on</artifactId>
<version>1.49</version>
<optional>true</optional>
</dependency>
</dependencies>
另外请确保您只有一个版本的BouncyCastle。