我从互联网上获取了一个示例代码,我正在尝试签署一份文件。
这是代码
public class SimpleSignature {
public static final String KEYSTORE = "c:/key/sano";
public static final char[] PASSWORD = "Chennai".toCharArray();
public static final String SRC = "c:/itext/unsigned.pdf";
public static final String DEST = "c:/itext/signed.pdf";
public void sign(String src, String dest,
Certificate[] chain, PrivateKey pk, String digestAlgorithm, String provider,
CryptoStandard subfilter, String reason, String location)
throws GeneralSecurityException, IOException, DocumentException {
// Creating the reader and the stamper
PdfReader reader = new PdfReader(src);
FileOutputStream os = new FileOutputStream(dest);
PdfStamper stamper = PdfStamper.createSignature(reader, os, '\0');
// Creating the appearance
PdfSignatureAppearance appearance = stamper.getSignatureAppearance();
appearance.setReason(reason);
appearance.setLocation(location);
appearance.setVisibleSignature(new Rectangle(36, 748, 144, 780), 1, "sig");
// Creating the signature
ExternalDigest digest = new BouncyCastleDigest();
ExternalSignature signature =
new PrivateKeySignature(pk, digestAlgorithm, provider);
MakeSignature.signDetached(appearance, digest, signature, chain,
null, null, null, 0, subfilter);
}
public static void main(String[] args)
throws GeneralSecurityException, IOException, DocumentException {
BouncyCastleProvider provider = new BouncyCastleProvider();
Security.addProvider(provider);
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
ks.load(new FileInputStream(KEYSTORE), PASSWORD);
String alias = (String)ks.aliases().nextElement();
PrivateKey pk = (PrivateKey) ks.getKey(alias, PASSWORD);
Certificate[] chain = ks.getCertificateChain(alias);
SimpleSignature app = new SimpleSignature();
app.sign(SRC, String.format(DEST, 1), chain, pk, DigestAlgorithms.SHA256,
provider.getName(), CryptoStandard.CMS, "Test 1", "Ghent");
app.sign(SRC, String.format(DEST, 2), chain, pk, DigestAlgorithms.SHA512,
provider.getName(), CryptoStandard.CMS, "Test 2", "Ghent");
app.sign(SRC, String.format(DEST, 3), chain, pk, DigestAlgorithms.SHA256,
provider.getName(), CryptoStandard.CADES, "Test 3", "Ghent");
app.sign(SRC, String.format(DEST, 4), chain, pk, DigestAlgorithms.RIPEMD160,
provider.getName(), CryptoStandard.CADES, "Test 4", "Ghent");
}
}
当我运行此代码时。我收到这个错误。
Exception in thread "main" java.lang.NoSuchMethodError: org.bouncycastle.asn1.ASN1Integer.<init>(J)V
at com.itextpdf.text.pdf.security.PdfPKCS7.getEncodedPKCS7(PdfPKCS7.java:797)
at com.itextpdf.text.pdf.security.MakeSignature.signDetached(MakeSignature.java:149)
at org.allcolor.yahp.sample.SimpleSignature.sign(SimpleSignature.java:45)
at org.allcolor.yahp.sample.SimpleSignature.main(SimpleSignature.java:62)
知道为什么我会收到此错误。