Java利用iTEXT函式庫進行簽章

public void signPdfFirstTime(String src, String dest) throws IOException, DocumentException, GeneralSecurityException {
String ksPassword = “密碼";
String alias = “ca.test.com.tw";
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
ks.load(ITEXTpdfSignatures.class.getResourceAsStream(“/ca.private.keystore"), ksPassword.toCharArray());
KeyStore.PrivateKeyEntry privateKeyEntry = (KeyStore.PrivateKeyEntry) ks.getEntry(alias, new KeyStore.PasswordProtection(ksPassword.toCharArray()));
PrivateKey pk = privateKeyEntry.getPrivateKey();
Certificate[] chain = ks.getCertificateChain(alias);

// reader and stamper
PdfStamper stamper = PdfStamper.createSignature(new PdfReader(src), new FileOutputStream(dest), “);
// appearance
PdfSignatureAppearance appearance = stamper .getSignatureAppearance();
// appearance.setImage(Image.getInstance(RESOURCE));
appearance.setReason(“This is writed");
appearance.setLocation(“footbar");
appearance.setVisibleSignature(new Rectangle(72, 732, 144, 780), 1, “sign1″);
// digital signature
MakeSignature.signDetached(appearance, new BouncyCastleDigest(), new PrivateKeySignature(pk, “SHA-256″, “BC"), chain, null, null, null, 0, CryptoStandard.CMS);
}

public void verifySignatures() throws GeneralSecurityException, IOException {
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
ks.load(null, null);
CertificateFactory cf = CertificateFactory.getInstance(“X509″);
FileInputStream is1 = new FileInputStream(“D:/Temp/HCA.cer");
X509Certificate cert1 = (X509Certificate) cf.generateCertificate(is1);
ks.setCertificateEntry(“cacert", cert1);
FileInputStream is2 = new FileInputStream(“D:/Temp/0100DC7D000001438E709C5800099C9D.cer");
X509Certificate cert2 = (X509Certificate) cf.generateCertificate(is2);
ks.setCertificateEntry(“foobar", cert2);

PrintWriter out = new PrintWriter(new FileOutputStream(VERIFICATION));
PdfReader reader = new PdfReader(SIGNED2);
AcroFields af = reader.getAcroFields();
ArrayList names = af.getSignatureNames();
for (String name : names) {
out.println(“Signature name: " + name);
out.println(“Signature covers whole document: " + af.signatureCoversWholeDocument(name));
out.println(“Document revision: " + af.getRevision(name) + " of " + af.getTotalRevisions());
PdfPKCS7 pk = af.verifySignature(name);
Calendar cal = pk.getSignDate();
Certificate[] pkc = pk.getCertificates();
out.println(“Subject: " + CertificateInfo.getSubjectFields(pk.getSigningCertificate()));
out.println(“Revision modified: " + !pk.verify());
List errors = CertificateVerification.verifyCertificates(pkc, ks, null, cal);
if (errors.size() == 0)
out.println(“Certificates verified against the KeyStore");
else
out.println(errors);
}
out.flush();
out.close();
}

public void extractFirstRevision() throws IOException {
PdfReader reader = new PdfReader(SIGNED2);
AcroFields af = reader.getAcroFields();
FileOutputStream os = new FileOutputStream(REVISION);
byte bb[] = new byte[1028];
InputStream ip = af.extractRevision(“sign1″);
int n = 0;
while ((n = ip.read(bb)) > 0)
os.write(bb, 0, n);
os.close();
ip.close();
}

廣告

About fenjj

Perfect !!??...
本篇發表於 Uncategorized。將永久鏈結加入書籤。

發表迴響

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com Logo

您的留言將使用 WordPress.com 帳號。 登出 / 變更 )

Twitter picture

您的留言將使用 Twitter 帳號。 登出 / 變更 )

Facebook照片

您的留言將使用 Facebook 帳號。 登出 / 變更 )

Google+ photo

您的留言將使用 Google+ 帳號。 登出 / 變更 )

連結到 %s