org.apache.pdfbox.pdmodel.encryption
public class: PublicKeyDecryptionMaterial [javadoc |
source]
java.lang.Object
org.apache.pdfbox.pdmodel.encryption.DecryptionMaterial
org.apache.pdfbox.pdmodel.encryption.PublicKeyDecryptionMaterial
This class holds necessary information to decrypt a PDF document
protected by the public key security handler.
To decrypt such a document, we need:
- a valid X509 certificate which correspond to one of the recipient of the document
- the private key corresponding to this certificate
- the password to decrypt the private key if necessary
Objects of this class can be used with the
openProtection
method of
PDDocument
.
The following example shows how to decrypt a document using a PKCS#12 certificate
(typically files with a pfx extension).
PDDocument doc = PDDocument.load(document_path);
KeyStore ks = KeyStore.getInstance("PKCS12");
ks.load(new FileInputStream(certificate_path), password.toCharArray());
PublicKeyDecryptionMaterial dm = new PublicKeyDecryptionMaterial(ks, null, password);
doc.openProtection(dm);
In this code sample certificate_path contains the path to the PKCS#12 certificate.
Also see:
- org.apache.pdfbox.pdmodel.PDDocument#openProtection(DecryptionMaterial)
- author:
Benoit
- Guillon (benoit.guillon@snv.jussieu.fr)
- version:
$
- Revision: 1.2 $
Constructor: |
public PublicKeyDecryptionMaterial(KeyStore keystore,
String a,
String pwd) {
keyStore = keystore;
alias = a;
password = pwd;
}
Create a new public key decryption material. Parameters:
keystore - The keystore were the private key and the certificate are
a - The alias of the private key and the certificate.
If the keystore contains only 1 entry, this parameter can be left null.
pwd - The password to extract the private key from the keystore.
|
Methods from java.lang.Object: |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Method from org.apache.pdfbox.pdmodel.encryption.PublicKeyDecryptionMaterial Detail: |
public X509Certificate getCertificate() throws KeyStoreException {
if(keyStore.size() == 1)
{
Enumeration aliases = keyStore.aliases();
String keyStoreAlias = (String)aliases.nextElement();
return (X509Certificate)keyStore.getCertificate(keyStoreAlias);
}
else
{
if(keyStore.containsAlias(alias))
{
return (X509Certificate)keyStore.getCertificate(alias);
}
throw new KeyStoreException("the keystore does not contain the given alias");
}
}
Returns the certificate contained in the keystore. |
public String getPassword() {
return password;
}
Returns the password given by the user and that will be used
to open the private key. |
public Key getPrivateKey() throws KeyStoreException {
try
{
if(keyStore.size() == 1)
{
Enumeration aliases = keyStore.aliases();
String keyStoreAlias = (String)aliases.nextElement();
return keyStore.getKey(keyStoreAlias, password.toCharArray());
}
else
{
if(keyStore.containsAlias(alias))
{
return keyStore.getKey(alias, password.toCharArray());
}
throw new KeyStoreException("the keystore does not contain the given alias");
}
}
catch(UnrecoverableKeyException ex)
{
throw new KeyStoreException("the private key is not recoverable");
}
catch(NoSuchAlgorithmException ex)
{
throw new KeyStoreException("the algorithm necessary to recover the key is not available");
}
}
returns The private key that will be used to open the document protection. |