|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectorg.faceless.pdf2.EncryptionHandler
org.faceless.pdf2.PublicKeyEncryptionHandler
public class PublicKeyEncryptionHandler
The PublicKeyEncryptionHandler can be used to encrypt and decrypt documents using public/private
key Encryption, so documents can only be opened by certain individuals. It requires Java 1.4 or
later, as it uses the javax.crypto package. The resulting documents can be opened
in Acrobat 5 or later with the appropriate private key.
We're going to assume you're familiar with public key cryptography if you're using this class, and instead jump straight in and give a couple of examples showing how to decrypt and encrypt a document. First, some important notes:
java.lang.SecurityException: Unsupported keysize or algorithm parameters
Once these steps are done, to encrypt a document you need the X.509 certificate of the person you're sending it to. Typically you'd get this from a KeyStore, as in this example:
KeyStore keystore = KeyStore.getInstance("PKCS12");
keystore.load(new FileInputStream("keystore.p12"), "password".toCharArray());
X509Certificate cert = (X509Certificate)keystore.getCertificate("john");
PublicKeyEncryptionHandler handler = new PublicKeyEncryptionHandler(5);
handler.addRecipient(cert, StandardEncryptionHandler.PRINT_HIGHRES,
StandardEncryptionHandler.CHANGE_ALL,
StandardEncryptionHandler.EXTRACT_ALL);
pdf.setEncryptionHandler(handler);
Other ways to get a certificate include using the FormSignature.loadPKCS7KeyStore(java.io.InputStream)
method to load your X.509 certificates from a PKCS#7 object, or the CertificateFactory
class to load the certificate from .cer files exported by Acrobat:
FileInputStream fis = new FileInputStream("certificate.cer");
CertificateFactory cf = CertificateFactory.getInstance("X.509");
X509Certificate cert = (X509Certificate)cf.generateCertificate(fis);
To decrypt a document, you will need a KeyStore containing a private key that matches the public key used to encrypt the document. Typically this will be done like so:
KeyStore keystore = KeyStore.getInstance("PKCS12");
keystore.load(new FileInputStream("keystore.p12"), "storepassword".toCharArray());
EncryptionHandler handler = new PublicKeyEncryptionHandler(keystore,
null,
"keypassword".toCharArray());
PDF pdf = new PDF(new PDFReader(new File("encrypted.pdf"), handler));
FormSignature,
PDFReader.PDFReader(InputStream,EncryptionHandler),
StandardEncryptionHandler| Constructor Summary | |
|---|---|
PublicKeyEncryptionHandler()
Create a new PublicKeyEncryptionHandler for decrypting a document encrypted with the Adobe.PubSec public key encryption handler. |
|
PublicKeyEncryptionHandler(int acrobatversion)
Create a new PublicKeyEncryptionHandler for encrypting a document. |
|
PublicKeyEncryptionHandler(KeyStore keystore,
String alias,
char[] password)
Create a new PublicKeyEncryptionHandler for decrypting a document encrypted with the Adobe.PubSec public key encryption handler. |
|
| Method Summary | |
|---|---|
void |
addRecipient(X509Certificate recipient,
int print,
int extraction,
int change)
Add a recipient to the list of people that can open the document |
protected boolean |
chooseRecipient(X500Principal[] issuers,
BigInteger[] serials)
This method is called by prepareToDecrypt() to give an implementation
the chance to select an appropriate entry from the KeyStore if it hasn't
already been done. |
void |
finishedDecrypt()
This method is called after the PDF has been read. |
void |
finishedEncrypt()
This method is called after the PDF has been written. |
int |
getChange()
Return the value of the "Change" flags. |
InputStream |
getDecryptionStream(InputStream in,
int num,
int gen)
Return a FilterInputStream that will decrypt anything read
from it. |
String |
getDescription()
Return a textual description of the encryption used |
int |
getEncryptedStreamLength(int len)
Return the length that a stream of the specified length would be after encryption. |
OutputStream |
getEncryptionStream(OutputStream out,
int num,
int gen)
Return a FilterOutputStream that will encrypt anything written
to it. |
int |
getExtract()
Return the value of the "Extract" flags. |
String |
getFilterName()
Return the name of the "Filter" field in the Encryption dictionary. |
int |
getPrint()
Return the value of the "Print" flags. |
String |
getSubFilterName()
Return the name of the "Subfilter" field in the Encryption dictionary. |
boolean |
hasRight(String right)
Returns true if the EncryptionHandler wil grant the specified right to the PDF library. |
boolean |
isMetadataEncrypted()
This method returns true if XMP MetaData should be stored encrypted, or false otherwise. |
boolean |
isRequired()
This method should return true if the document needs to be encrypted. |
void |
prepareToDecrypt()
This method is called just before the PDF is read in. |
void |
prepareToEncrypt()
This method is called when the PDF is about to be written out. |
void |
setDecryptionKey(KeyStore keystore,
String alias,
char[] password)
Set the private key to use to decrypt the document |
void |
setEncryptedMetadata(boolean encrypt)
Set whether XMP Metadata is to be encrypted or not. |
| Methods inherited from class org.faceless.pdf2.EncryptionHandler |
|---|
clone, containsKey, getArrayValueSize, getBooleanValue, getDecryptedStreamLength, getDictionaryValueKeys, getFileId, getNameValue, getNumericValue, getStringValue, getTextStringValue, isEmbeddedFileEncrypted, isStreamEncrypted, isStringEncrypted, markChanged, putArrayValue, putBooleanValue, putDictionaryValue, putNameValue, putNumericValue, putStringValue, putTextStringValue, setFileId |
| Methods inherited from class java.lang.Object |
|---|
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public PublicKeyEncryptionHandler()
Adobe.PubSec public key encryption handler. This constructor must
be followed by a call to setDecryptionKey().
public PublicKeyEncryptionHandler(KeyStore keystore,
String alias,
char[] password)
throws GeneralSecurityException
Adobe.PubSec public key encryption handler.
keystore - the KeyStore containing the private key to decrypt the document withalias - the alias of the key to use, or null to use the first key that fitspassword - the password to decrypt the private key, or null if no password is required
GeneralSecurityExceptionpublic PublicKeyEncryptionHandler(int acrobatversion)
addRecipient() method. The version number specifies
the minimum release of Acrobat required to open the document - valid values are from 5 to 8,
to target Acrobat 5.0 to 8.0 respectively. Targetting Acrobat 7.0 or above will result in the
AES cipher being used if it's available. Targetting earlier version will use the RC4 cipher.
acrobatversion - the version of Acrobat that is being targeted. Must be between 5 and 8.| Method Detail |
|---|
public void setEncryptedMetadata(boolean encrypt)
encrypt - whether to encrypt the XMP Metadata when saving the file.
public void setDecryptionKey(KeyStore keystore,
String alias,
char[] password)
throws GeneralSecurityException
keystore - the KeyStore containing the private key to decrypt the document withalias - the alias of the key to use, or null to use the first key that fitspassword - the password to decrypt the private key, or null if no password is required
GeneralSecurityException
public void addRecipient(X509Certificate recipient,
int print,
int extraction,
int change)
recipient - the X.509 certificate of the recipientprint - one of StandardEncryptionHandler.PRINT_NONE StandardEncryptionHandler.PRINT_LOWRES StandardEncryptionHandler.PRINT_HIGHRESextraction - one of StandardEncryptionHandler.EXTRACT_NONE StandardEncryptionHandler.EXTRACT_ACCESSIBILITY StandardEncryptionHandler.EXTRACT_ALLchange - one of StandardEncryptionHandler.CHANGE_NONE StandardEncryptionHandler.CHANGE_LAYOUT StandardEncryptionHandler.CHANGE_FORMS StandardEncryptionHandler.CHANGE_ANNOTATIONS StandardEncryptionHandler.CHANGE_ALLpublic int getChange()
StandardEncryptionHandler.CHANGE_NONE StandardEncryptionHandler.CHANGE_LAYOUT StandardEncryptionHandler.CHANGE_FORMS StandardEncryptionHandler.CHANGE_ANNOTATIONS or StandardEncryptionHandler.CHANGE_ALLpublic int getExtract()
StandardEncryptionHandler.EXTRACT_NONE StandardEncryptionHandler.EXTRACT_ACCESSIBILITY StandardEncryptionHandler.EXTRACT_ALLpublic int getPrint()
StandardEncryptionHandler.PRINT_NONE StandardEncryptionHandler.PRINT_LOWRES StandardEncryptionHandler.PRINT_HIGHRESpublic boolean hasRight(String right)
EncryptionHandlersuper.hasRight()
if it doesn't recognise the value of "right"
hasRight in class EncryptionHandlerright - an interned() String defining the usage right the PDF library
is querying.public String getFilterName()
EncryptionHandlerStandardEncryptionHandler class returns "Standard" from this method.
getFilterName in class EncryptionHandlerpublic String getSubFilterName()
EncryptionHandlernull.
getSubFilterName in class EncryptionHandlerpublic String getDescription()
public boolean isRequired()
EncryptionHandlertrue if the document needs to be encrypted.
For example, the StandardEncryptionHandler returns false here
if and only if no passwords are set and the document is set to allow full access.
isRequired in class EncryptionHandlerpublic boolean isMetadataEncrypted()
EncryptionHandler
isMetadataEncrypted in class EncryptionHandlerpublic int getEncryptedStreamLength(int len)
EncryptionHandler
getEncryptedStreamLength in class EncryptionHandler
public OutputStream getEncryptionStream(OutputStream out,
int num,
int gen)
EncryptionHandlerFilterOutputStream that will encrypt anything written
to it. The encryption parameters are set in EncryptionHandler.prepareToEncrypt(),
which is called once at the start of the render.
getEncryptionStream in class EncryptionHandlerout - the OuptutStream that should be written tonum - the object number of the top-level objectgen - the generation number of the top-level object
public InputStream getDecryptionStream(InputStream in,
int num,
int gen)
EncryptionHandlerFilterInputStream that will decrypt anything read
from it. The decryption parameters are set in EncryptionHandler.prepareToDecrypt(),
which is called once at the start of the PDF read.
getDecryptionStream in class EncryptionHandlerin - the InputStream that should be read fromnum - the object number of the top-level objectgen - the generation number of the top-level object
public void prepareToDecrypt()
throws IOException
EncryptionHandlerEncrypt dictionary by way of the various get...
methods, and use them and the value of EncryptionHandler.getFileId() to set its internal state so that
it's ready to start decryption. It may throw an IOException if these parameters
are invalid, in which case the document cannot be read.
prepareToDecrypt in class EncryptionHandlerIOException
protected boolean chooseRecipient(X500Principal[] issuers,
BigInteger[] serials)
prepareToDecrypt() to give an implementation
the chance to select an appropriate entry from the KeyStore if it hasn't
already been done. The supplied arrays are equal length and indicate
the Issuer and SerialNumber of all the recipients that can decrypt this
document. By default this method does nothing.
issuers - an array listing all the X.509 Certificate Issuersserials - an array listing all the X.509 Certificate Serial Numbers.
public void prepareToEncrypt()
throws IOException
EncryptionHandlerEncrypt dictionary
(including the "Filter" field) by way of the various put... methods, and will use
these and the value of EncryptionHandler.getFileId() to set its internal state so that it's ready to
start encryption. It may throw an IOException if these parameters are in any
way invalid, in which case the document cannot be written.
prepareToEncrypt in class EncryptionHandlerIOExceptionpublic void finishedEncrypt()
EncryptionHandler
finishedEncrypt in class EncryptionHandlerpublic void finishedDecrypt()
EncryptionHandler
finishedDecrypt in class EncryptionHandler
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||