org.pdfbox.pdmodel.encryption

Class PublicKeyProtectionPolicy


public class PublicKeyProtectionPolicy
extends ProtectionPolicy

This class represents the protection policy to use to protect a document with the public key security handler as described in the PDF specification 1.6 p104. 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);
 
Version:
$Revision: 1.2 $
Author:
Benoit Guillon (benoit.guillon@snv.jussieu.fr)
See Also:
PDDocument.protect(ProtectionPolicy), AccessPermission, PublicKeyRecipient

Constructor Summary

PublicKeyProtectionPolicy()
Constructor for encryption.

Method Summary

void
addRecipient(PublicKeyRecipient r)
Adds a new recipient to the recipients list.
X509Certificate
getDecryptionCertificate()
Getter of the property decryptionCertificate.
Iterator
getRecipientsIterator()
Returns an iterator to browse the list of recipients.
int
getRecipientsNumber()
Returns the number of recipients.
boolean
removeRecipient(PublicKeyRecipient r)
Removes a recipient from the recipients list.
void
setDecryptionCertificate(X509Certificate aDecryptionCertificate)
Setter of the property decryptionCertificate.

Methods inherited from class org.pdfbox.pdmodel.encryption.ProtectionPolicy

getEncryptionKeyLength, setEncryptionKeyLength

Constructor Details

PublicKeyProtectionPolicy

public PublicKeyProtectionPolicy()
Constructor for encryption. Just creates an empty recipients list.

Method Details

addRecipient

public void addRecipient(PublicKeyRecipient r)
Adds a new recipient to the recipients list.
Parameters:
r - A new recipient.

getDecryptionCertificate

public X509Certificate getDecryptionCertificate()
Getter of the property decryptionCertificate.
Returns:
Returns the decryptionCertificate.

getRecipientsIterator

public Iterator getRecipientsIterator()
Returns an iterator to browse the list of recipients. Object found in this iterator are PublicKeyRecipient.
Returns:
The recipients list iterator.

getRecipientsNumber

public int getRecipientsNumber()
Returns the number of recipients.
Returns:
The number of recipients.

removeRecipient

public boolean removeRecipient(PublicKeyRecipient r)
Removes a recipient from the recipients list.
Parameters:
r - The recipient to remove.
Returns:
true If a recipient was found and removed.

setDecryptionCertificate

public void setDecryptionCertificate(X509Certificate aDecryptionCertificate)
Setter of the property decryptionCertificate.
Parameters:
aDecryptionCertificate - The decryption certificate to set.