Signing does incremental saving so what you do can't work because the
existing PDF wouldn't be signed.
So first sign and then reload your encrypted PDF, then sign it.
However I must admit I have never heard of anybody who signed a
certificate encrypted PDF. I hope it will work :-)
Tilman
Am 25.09.2017 um 15:21 schrieb brona xxxx:
Hello,
I really need your help.
I'm able to sign document using example for signing:
PDDocument doc = PDDocument.load(new File(src));
// sign PDF
KeyStore keystore = KeyStore.getInstance("PKCS12");
keystore.load(new FileInputStream(cert2_sign), keystore_password.
toCharArray());
SignatureBase signing = new SignatureBase(keystore,keystore_
password.toCharArray());
signing.setExternalSigning(true);
File outFile = new File(dest);
FileOutputStream fos = new FileOutputStream(outFile);
signing.signDetached(doc, fos, null);
fos.close();
doc.close();
I'm also able to encrypt document using certificate:
PDDocument doc = PDDocument.load(new File(src));
int keyLength = 128;
AccessPermission ap = new AccessPermission();
ap.setCanFillInForm(false);
X509Certificate cert = getPublicCertificate(cert_encr);
PublicKeyProtectionPolicy ppp = new PublicKeyProtectionPolicy();
PublicKeyRecipient recip = new PublicKeyRecipient();
recip.setPermission(ap);
recip.setX509(cert);
ppp.addRecipient(recip);
ppp.setEncryptionKeyLength(keyLength);
doc.protect(ppp);
doc.save(dest);
doc.close();
I've tried to combine these two actions into one, but I haven't succeeded.
PDDocument doc = PDDocument.load(new File(src));
int keyLength = 128;
AccessPermission ap = new AccessPermission();
ap.setCanFillInForm(false);
X509Certificate cert = getPublicCertificate(cert_encr);
PublicKeyProtectionPolicy ppp = new PublicKeyProtectionPolicy();
PublicKeyRecipient recip = new PublicKeyRecipient();
recip.setPermission(ap);
recip.setX509(cert);
ppp.addRecipient(recip);
ppp.setEncryptionKeyLength(keyLength);
doc.protect(ppp);
// sign PDF
KeyStore keystore = KeyStore.getInstance("PKCS12");
keystore.load(new FileInputStream(cert2_sign), keystore_password.
toCharArray());
SignatureBase signing = new SignatureBase(keystore,keystore_
password.toCharArray());
signing.setExternalSigning(true);
File outFile = new File(dest);
FileOutputStream fos = new FileOutputStream(outFile);
signing.signDetached(doc, fos, null);
fos.close();
doc.close();
I get java.lang.NullPointerException
at org.apache.pdfbox.pdmodel.encryption.SecurityHandler.calcFinalKey
(SecurityHandler.java:172)
at org.apache.pdfbox.pdmodel.encryption.SecurityHandler.encryptData
(SecurityHandler.java:149)
at org.apache.pdfbox.pdmodel.encryption.SecurityHandler.encryptString
(SecurityHandler.java:560)
at org.apache.pdfbox.pdfwriter.COSWriter.visitFromString(COSWriter.java:
1225)
at org.apache.pdfbox.cos.COSString.accept(COSString.java:243)
at org.apache.pdfbox.pdfwriter.COSWriter.visitFromDictionary(COSWriter.
java:1064)
at org.apache.pdfbox.pdfwriter.COSWriter.visitFromDictionary(COSWriter.
java:1023)
at org.apache.pdfbox.cos.COSDictionary.accept(COSDictionary.java:1329)
at org.apache.pdfbox.pdfwriter.COSWriter.doWriteObject(COSWriter.java:
522)
at org.apache.pdfbox.pdfwriter.COSWriter.doWriteObjects(COSWriter.java:
460)
at org.apache.pdfbox.pdfwriter.COSWriter.doWriteBody(COSWriter.java:444)
at org.apache.pdfbox.pdfwriter.COSWriter.visitFromDocument(COSWriter.
java:1099)
at org.apache.pdfbox.cos.COSDocument.accept(COSDocument.java:419)
at org.apache.pdfbox.pdfwriter.COSWriter.write(COSWriter.java:1370)
at org.apache.pdfbox.pdfwriter.COSWriter.write(COSWriter.java:1257)
at org.apache.pdfbox.pdmodel.PDDocument.
saveIncrementalForExternalSigning(PDDocument.java:1365)
at SignatureBase.signDetached(SignatureBase.java:385)
at EncryptPDF2.signPDF(EncryptPDF2.java:125)
at EncryptPDF2.main(EncryptPDF2.java:199)
Thanks for your help.
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]