public void decryptDocument(String password) throws CryptographyException, IOException, InvalidPasswordException {
if( password == null )
{
password = "";
}
PDStandardEncryption encParameters = (PDStandardEncryption)pdDocument.getEncryptionDictionary();
int permissions = encParameters.getPermissions();
int revision = encParameters.getRevision();
int length = encParameters.getLength()/8;
COSString id = (COSString)document.getDocumentID().getObject( 0 );
byte[] u = encParameters.getUserKey();
byte[] o = encParameters.getOwnerKey();
boolean isUserPassword =
encryption.isUserPassword( password.getBytes(), u,
o, permissions, id.getBytes(), revision, length );
boolean isOwnerPassword =
encryption.isOwnerPassword( password.getBytes(), u,
o, permissions, id.getBytes(), revision, length );
if( isUserPassword )
{
encryptionKey =
encryption.computeEncryptedKey(
password.getBytes(), o,
permissions, id.getBytes(), revision, length );
}
else if( isOwnerPassword )
{
byte[] computedUserPassword =
encryption.getUserPassword(
password.getBytes(),
o,
revision,
length );
encryptionKey =
encryption.computeEncryptedKey(
computedUserPassword, o,
permissions, id.getBytes(), revision, length );
}
else
{
throw new InvalidPasswordException( "Error: The supplied password does not match " +
"either the owner or user password in the document." );
}
COSDictionary trailer = document.getTrailer();
COSArray fields = (COSArray)trailer.getObjectFromPath( "Root/AcroForm/Fields" );
//We need to collect all the signature dictionaries, for some
//reason the 'Contents' entry of signatures is not really encrypted
if( fields != null )
{
for( int i=0; i< fields.size(); i++ )
{
COSDictionary field = (COSDictionary)fields.getObject( i );
addDictionaryAndSubDictionary( potentialSignatures, field );
}
}
List allObjects = document.getObjects();
Iterator objectIter = allObjects.iterator();
while( objectIter.hasNext() )
{
decryptObject( (COSObject)objectIter.next() );
}
document.setEncryptionDictionary( null );
} Deprecated!This will decrypt the document. |