gpt4 book ai didi

java - itext 数字签名的这种实现有什么问题?

转载 作者:行者123 更新时间:2023-12-02 03:28:24 28 4
gpt4 key购买 nike

我从互联网上获取了示例代码,并尝试签署一份文档。

这是代码

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)

知道为什么我会收到此错误吗?

最佳答案

我能够通过使用 itext5.3.4 和 bcprov-jdk15on-1.47.jar 成功运行示例

关于java - itext 数字签名的这种实现有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14956356/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com