{
password = "";
}
PDDocument document = null;
try
{
document = PDDocument.load( infile );
if( document.isEncrypted() )
{
DecryptionMaterial decryptionMaterial = null;
if( keyStore != null )
{
KeyStore ks = KeyStore.getInstance("PKCS12");
ks.load(new FileInputStream(keyStore), password.toCharArray());
decryptionMaterial = new PublicKeyDecryptionMaterial(ks, alias, password);
}
else
{
decryptionMaterial = new StandardDecryptionMaterial(password);
}
document.openProtection(decryptionMaterial);
AccessPermission ap = document.getCurrentAccessPermission();
if(ap.isOwnerPermission())
{
document.save( outfile );
}
else
{
throw new IOException(
"Error: You are only allowed to decrypt a document with the owner password." );
}
}
else
{
System.err.println( "Error: Document is not encrypted." );
}
}
finally
{
if( document != null )
{
document.close();
}
}
}
}