0
我試圖使用智能卡和iText對PDF進行數字簽名。我通過documentation瞭解如何使用iText簽署文檔,並嘗試自己使用他們的一些代碼。下面是我使用的代碼:iText數字簽名PDF而無需輸入PIN兩次
String pkcs11ConfigSettings =
"name = SmartCard\nlibrary = C:\\Program Files\\ActivIdentity\\ActivClient\\acpkcs201-ns.dll";
AuthProvider p =
new SunPKCS11(new ByteArrayInputStream(pkcs11ConfigSettings.getBytes()));
Security.addProvider(p);
KeyStore.PasswordProtection pp =
new KeyStore.PasswordProtection("".toCharArray());
KeyStore.Builder builder =
KeyStore.Builder.newInstance("PKCS11",p ,pp);
KeyStore ks = builder.getKeyStore();
Certificate[] cc = ks.getCertificateChain("Digital Signature Key");
PrivateKey pk = (PrivateKey)ks.getKey("Digital Signature Key", null);
OutputStream fos = new FileOutputStream("c:\\2.pdf");
PdfReader reader = new PdfReader(new FileInputStream(new File("C:\\1.pdf")));
PdfStamper stamper = PdfStamper.createSignature(reader, fos, '\0');
PdfSignatureAppearance appearance = stamper.getSignatureAppearance();
appearance.setCrypto(pk, cc, null,PdfSignatureAppearance.SELF_SIGNED);
appearance.setVisibleSignature(new Rectangle(0, 0, 100, 100), 1,null);
stamper.close();
這種方法的問題是,當iText的關閉它確實給C_Sign()
調用它調用驅動程序的提示輸入PIN的PDFStamper
。
所以如果這是一個應用程序,它需要我在簽名之前輸入我的PIN碼,以獲得KeyStore
和PrivateKey
,以及驅動程序的PIN輸入提示時出現。無論如何都要問兩次PIN碼嗎?我對這個東西很陌生,我是否以這種錯誤的方式去做?