pdfbox-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jonathan Barbero <jonathan.barb...@gmail.com>
Subject Re: Certificate a PDF
Date Mon, 17 Oct 2016 18:26:34 GMT
Thank you Tilman.

I have tried this:


                Security.addProvider(new BouncyCastleProvider());

// load the keystore
KeyStore keystore = KeyStore.getInstance("PKCS12");
char pass[] = "xxxxxx".toCharArray();

keystore.load(new FileInputStream("./keystore/cert.pfx"), pass);

Enumeration<String> aliases = keystore.aliases();
String alias;
if (aliases.hasMoreElements()) {
alias = aliases.nextElement();
} else {
throw new KeyStoreException("Keystore is empty");
}

PrivateKey pk = ((PrivateKey) keystore.getKey(alias, pass));
Certificate[] certificateChain = keystore.getCertificateChain(alias);
Certificate certificate = certificateChain[0];


PublicKeyProtectionPolicy pkpp = new PublicKeyProtectionPolicy();
PublicKeyRecipient pkr = new PublicKeyRecipient();

AccessPermission ap = new AccessPermission();
ap.setCanFillInForm(true);
ap.setCanModify(false);
ap.setCanModifyAnnotations(false);

pkr.setPermission(ap);
pkr.setX509((X509Certificate) certificate);

pkpp.setEncryptionKeyLength(128); // no support for 256
pkpp.addRecipient(pkr);

PDDocument doc = PDDocument.load(input);
doc.protect(pkpp);
doc.save(output);


but when I try to open the PDF with Adobe Reader it says that I don't have
the ID for it.

 I just want to apply a certify signature, not to encrypt.

Do you have any other hint?

Thanks.

On Wed, Oct 12, 2016 at 1:15 PM, Tilman Hausherr <THausherr@t-online.de>
wrote:

> Am 12.10.2016 um 17:17 schrieb Jonathan Barbero:
>
>> Hi,
>>
>>   I have been playing with the signing example of PDF Box 2.0.3.
>> I would like to apply a certification signature (
>> https://helpx.adobe.com/acrobat/kb/certificate-signatures.html )
>> How could I do ?  Do you have any example ?
>>
>> Thanks,
>>   Jonathan.
>>
>>
> There is no example, but the test TestPublicKeyEncryption.java from the
> source download should help too.
>
> https://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/test/j
> ava/org/apache/pdfbox/encryption/TestPublicKeyEncryption.java?view=markup
>
> There's also a segment in the Encrypt command line tool:
>
>
>                     if( certFile != null )
>                     {
>                         PublicKeyProtectionPolicy ppp = new
> PublicKeyProtectionPolicy();
>                         PublicKeyRecipient recip = new
> PublicKeyRecipient();
>                         recip.setPermission(ap);
>
>
>                         CertificateFactory cf =
> CertificateFactory.getInstance("X.509");
>
>                         InputStream inStream = null;
>                         try
>                         {
>                             inStream = new FileInputStream(certFile);
>                             X509Certificate certificate =
> (X509Certificate)cf.generateCertificate(inStream);
>                             recip.setX509(certificate);
>                         }
>                         finally
>                         {
>                             if (inStream != null)
>                             {
>                                 inStream.close();
>                             }
>                         }
>
>                         ppp.addRecipient(recip);
>
>                         ppp.setEncryptionKeyLength(keyLength);
>
>                         document.protect(ppp);
>                     }
>                     else
>                     {
>                       .....
>                     }
>                     document.save( outfile );
>                 }
>
>
> Tilman
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
>
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message