gpt4 book ai didi

java - Itext 签名空签名字段

转载 作者:行者123 更新时间:2023-11-30 04:07:16 27 4
gpt4 key购买 nike

在我的应用程序中,我有一个用于签署 pdf 文件的选项。 pdf 签名有两种选项,一种是创建新签名,第二种是我需要签署空签名字段。我完成了创建新签名字段的部分,它工作正常,现在我在签署空签名字段时遇到问题。这是我的代码

 KeyStore ks = KeyStore.getInstance("Windows-MY");
ks.load(null, null) ;
//ovo smo ubacili
Enumeration en = ks.aliases();
// String alias = (String)en.nextElement();
PrivateKey key = (PrivateKey)ks.getKey(alias, "password".toCharArray());
java.security.cert.Certificate[] chain = ks.getCertificateChain(alias);
//location of pdf document to sign
PdfReader reader = new PdfReader(jTextField1.getText());

String [] delovi=jTextField1.getText().split("\\\\");
String potisaniFajl=delovi[delovi.length-1];

new File(System.getProperty("user.home") + "\\Desktop\\Potpisani Fajlovi\\").mkdirs();
//signed pdf location
FileOutputStream fout = new FileOutputStream(System.getProperty("user.home") + "\\Desktop\\Potpisani Fajlovi\\"+potisaniFajl);
PdfStamper stp = PdfStamper.createSignature(reader, fout, '\0',null,true);

PdfSignatureAppearance appearance = stp.getSignatureAppearance();
appearance.setCrypto(null, chain, null, PdfSignatureAppearance.SELF_SIGNED);
//appearance.setCrypto(key, chain, null,PdfSignatureAppearance.WINCER_SIGNED);
//appearance.setCrypto(null, chain, null, PdfSignatureAppearance.WINCER_SIGNED);
appearance.setReason("Potpis kompenzacije");
appearance.setLocation("Foobar");
//lokacija potpisa
appearance.setVisibleSignature(new Rectangle(100, 100, 200, 200), 1, "dva");

appearance.setExternalDigest(new byte[128], null, "RSA");
appearance.preClose();
Signature signature = Signature.getInstance("SHA1withRSA");
signature.initSign(key);
byte buf[] = new byte[8192];
int n;
InputStream inp = appearance.getRangeStream();
while ((n = inp.read(buf)) > 0) {
signature.update(buf, 0, n);
}
PdfPKCS7 sig = appearance.getSigStandard().getSigner();
sig.setExternalDigest(signature.sign(), null, "RSA");
PdfDictionary dic = new PdfDictionary();
dic.put(PdfName.CONTENTS,new PdfString(sig.getEncodedPKCS1()).setHexWriting(true));

appearance.close(dic);

此代码添加了一个新签名,我需要进行哪些更改才能签署空签名字段名称“GoodSignature”

最佳答案

what changes I need to do to sign empty signature field name "GoodSignature"

在当前代码中,您调用 PdfSignatureAppearance.setVisibleSignature 的重载来创建新的签名字段:

appearance.setVisibleSignature(new Rectangle(100, 100, 200, 200), 1, "dva");

它被记录(JavaDocs)为:

/**
* Sets the signature to be visible. It creates a new visible signature field.
* @param pageRect the position and dimension of the field in the page
* @param page the page to place the field. The fist page is 1
* @param fieldName the field name or <CODE>null</CODE> to generate automatically a new field name
*/
public void setVisibleSignature(Rectangle pageRect, int page, String fieldName)

对于您的新任务,您必须使用此重载:

/**
* Sets the signature to be visible. An empty signature field with the same name must already exist.
* @param fieldName the existing empty signature field name
*/
public void setVisibleSignature(String fieldName)

在您的情况下,调用将是:

appearance.setVisibleSignature("GoodSignature");

话虽如此,我建议您阅读 Digital Signatures for PDF documentsBruno Lowagie(iText Software)的白皮书。您的代码似乎使用了许多过时的技术,您应该更新。

关于java - Itext 签名空签名字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20443009/

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