DecryptionMaterial decryptionMaterial)
throws CryptographyException, IOException
{
if(!(decryptionMaterial instanceof StandardDecryptionMaterial))
{
throw new CryptographyException("Provided decryption material is not compatible with the document");
}
StandardDecryptionMaterial material = (StandardDecryptionMaterial)decryptionMaterial;
String password = material.getPassword();
if(password == null)
{
password = "";
}
int dicPermissions = encDictionary.getPermissions();
int dicRevision = encDictionary.getRevision();
int dicLength = encDictionary.getLength()/8;
//some documents may have not document id, see
//test\encryption\encrypted_doc_no_id.pdf
byte[] documentIDBytes = null;
if( documentIDArray != null && documentIDArray.size() >= 1 )
{
COSString id = (COSString)documentIDArray.getObject( 0 );
documentIDBytes = id.getBytes();
}
else
{
documentIDBytes = new byte[0];
}
// we need to know whether the meta data was encrypted for password calculation
boolean encryptMetadata = encDictionary.isEncryptMetaData();
byte[] u = encDictionary.getUserKey();
byte[] o = encDictionary.getOwnerKey();
boolean isUserPassword =
isUserPassword(
password.getBytes("ISO-8859-1"),
u,
o,
dicPermissions,
documentIDBytes,
dicRevision,
dicLength,
encryptMetadata);
boolean isOwnerPassword =
isOwnerPassword(
password.getBytes("ISO-8859-1"),
u,
o,
dicPermissions,
documentIDBytes,
dicRevision,
dicLength,
encryptMetadata);
if( isUserPassword )
{
currentAccessPermission = new AccessPermission( dicPermissions );
encryptionKey =
computeEncryptedKey(
password.getBytes("ISO-8859-1"),
o,
dicPermissions,
documentIDBytes,
dicRevision,
dicLength,
encryptMetadata );
}
else if( isOwnerPassword )
{
currentAccessPermission = AccessPermission.getOwnerAccessPermission();
byte[] computedUserPassword = getUserPassword(password.getBytes("ISO-8859-1"),o,dicRevision,dicLength );
encryptionKey =
computeEncryptedKey(
computedUserPassword,
o,
dicPermissions,
documentIDBytes,
dicRevision,
dicLength,
encryptMetadata);
}
else
{
throw new CryptographyException(
"Error: The supplied password does not match either the owner or user password in the document." );
}
// detect whether AES encryption is used. This assumes that the encryption algo is
// stored in the PDCryptFilterDictionary