Top Description Fields Constructors Methods
org.apache.pdfbox.pdmodel.encryption

public final Class PublicKeyProtectionPolicy

extends ProtectionPolicy
Class Inheritance
Imports
java.security.cert.X509Certificate, java.util.ArrayList, .Iterator, .List

The protection policy to use to protect a document with the public key security handler. PDF documents are encrypted so that they can be decrypted by one or more recipients. Each recipient have its own access permission. The following code sample shows how to protect a document using the public key security handler. In this code sample, doc is a PDDocument object.
PublicKeyProtectionPolicy policy = new PublicKeyProtectionPolicy();
PublicKeyRecipient recip = new PublicKeyRecipient();
AccessPermission ap = new AccessPermission();
ap.setCanModify(false);
recip.setPermission(ap);

// load the recipient's certificate
InputStream inStream = new FileInputStream(certificate_path);
CertificateFactory cf = CertificateFactory.getInstance("X.509");
X509Certificate certificate = (X509Certificate)cf.generateCertificate(inStream);
inStream.close();

recip.setX509(certificate); // set the recipient's certificate
policy.addRecipient(recip);
policy.setEncryptionKeyLength(128); // the document will be encrypted with 128 bits secret key
doc.protect(policy);
doc.save(out);
Author
Benoit Guillon
See Also
org.apache.pdfbox.pdmodel.PDDocument#protect(ProtectionPolicy), AccessPermission, PublicKeyRecipient

Field Summary

Modifier and TypeField and Description
private X509Certificate
private final List<PublicKeyRecipient>

Constructor Summary

AccessConstructor and Description
public
PublicKeyProtectionPolicy()

Creates a new PublicKeyProtectionPolicy with an empty recipients list.

Method Summary

Modifier and TypeMethod and Description
public void
addRecipient(PublicKeyRecipient
A new recipient.
recipient
)

Adds a new recipient to the recipients list.

public X509Certificate

Returns:

the decryption certificate
getDecryptionCertificate
()

Returns the decryption certificate.

public int

Returns:

the number of recipients
getNumberOfRecipients
()

Returns the number of recipients

public Iterator<PublicKeyRecipient>

Returns:

The recipients list iterator.
getRecipientsIterator
()

Returns an iterator to browse the list of recipients.

public boolean

Returns:

true If a recipient was found and removed.
removeRecipient
(PublicKeyRecipient
The recipient to remove.
recipient
)

Removes a recipient from the recipients list.

public void
setDecryptionCertificate(X509Certificate
the new decryption certificate.
decryptionCertificate
)

Sets the decryption certificate

Inherited from org.apache.pdfbox.pdmodel.encryption.ProtectionPolicy:
getEncryptionKeyLengthisPreferAESsetEncryptionKeyLengthsetPreferAES

Field Detail

decryptionCertificateback to summary
private X509Certificate decryptionCertificate
recipientsback to summary
private final List<PublicKeyRecipient> recipients

Constructor Detail

PublicKeyProtectionPolicyback to summary
public PublicKeyProtectionPolicy()

Creates a new PublicKeyProtectionPolicy with an empty recipients list.

Method Detail

addRecipientback to summary
public void addRecipient(PublicKeyRecipient recipient)

Adds a new recipient to the recipients list.

Parameters
recipient:PublicKeyRecipient

A new recipient.

getDecryptionCertificateback to summary
public X509Certificate getDecryptionCertificate()

Returns the decryption certificate.

Returns:X509Certificate

the decryption certificate

getNumberOfRecipientsback to summary
public int getNumberOfRecipients()

Returns the number of recipients

Returns:int

the number of recipients

getRecipientsIteratorback to summary
public Iterator<PublicKeyRecipient> getRecipientsIterator()

Returns an iterator to browse the list of recipients. Object found in this iterator are PublicKeyRecipient.

Returns:Iterator<PublicKeyRecipient>

The recipients list iterator.

removeRecipientback to summary
public boolean removeRecipient(PublicKeyRecipient recipient)

Removes a recipient from the recipients list.

Parameters
recipient:PublicKeyRecipient

The recipient to remove.

Returns:boolean

true If a recipient was found and removed.

setDecryptionCertificateback to summary
public void setDecryptionCertificate(X509Certificate decryptionCertificate)

Sets the decryption certificate

Parameters
decryptionCertificate:X509Certificate

the new decryption certificate.