我正在尝试使用我自己的提供程序在java中以编程方式使用IText签署pdf文件。当我用p12文件签名时一切都很好。但是当我尝试用令牌签名时,我没有任何例外。但是当我尝试打开签名的pdf时,我收到此错误0x2726。
这是我签名的文件:https://drive.google.com/file/d/0Bz1LIekMx5F8WHFDRHpTTUU2SVU/view?usp=sharing 请帮忙!
public static void main(String[] args) throws IOException, GeneralSecurityException, DocumentException {
char[] pass = PASSWORD.toCharArray();
MyProvider provider = new MyProvider();
Security.addProvider(provider);
KeyStore ks = null;
try {
ks = KeyStoreUtil.getKeyStore(Storage.MYTOKEN, "TOEKN_PATH", pass, provider);
} catch (KeyStoreException | NoSuchProviderException | FileNotFoundException | NoSuchAlgorithmException | CertificateException | PrivilegedActionException ex) {
Logger.getLogger(PDFSIGN.class.getName()).log(Level.SEVERE, null, ex);
}
PDFSIGN app = new PDFSIGN();
PrivateKey pk = null;
Certificate[] chain = new Certificate[1];
Enumeration<String> als = ks.aliases();
String alias = (String) als.nextElement();
System.err.println(alias);
Certificate cert = ks.getCertificate(alias);
ASN1InputStream ais = new ASN1InputStream(cert.getEncoded());
X509CertificateStructure x509Struct = new X509CertificateStructure((ASN1Sequence) ais.readObject());
ais.close();
X509CertificateObject certificateObject = new X509CertificateObject(x509Struct);
chain[0] = certificateObject;
passw = "123456";
pk = (PrivateKey) ks.getKey(alias, passw.toCharArray());
app.sign(SRC, DEST, chain, pk, DigestAlgorithms.SHA1, provider.getName(), CryptoStandard.CMS, "Test", "Ghent", crlList, null, null, 0);
}
public void sign(String src, String dest, Certificate[] chain, PrivateKey pk, String digestAlgorithm, String provider, CryptoStandard subfilter, String reason, String location, Collection<CrlClient> crlList,
OcspClient ocspClient, TSAClient tsaClient, int estimatedSize)
throws GeneralSecurityException, IOException, DocumentException {
PdfReader reader = new PdfReader(src);
FileOutputStream os = new FileOutputStream(dest);
PdfStamper stamper = PdfStamper.createSignature(reader, os, '\0');
PdfSignatureAppearance appearance = stamper.getSignatureAppearance();
appearance.setReason(reason);
appearance.setLocation(location);
appearance.setVisibleSignature(new Rectangle(36, 748, 144, 780), 1, "sig");
ExternalSignature pks = new PrivateKeySignature(pk, digestAlgorithm, provider);
ExternalDigest digest = new ProviderDigest(provider);
MakeSignature.signDetached(appearance, digest, pks, chain, crlList, ocspClient, tsaClient, estimatedSize, subfilter);
}