Top Description Fields Constructors Methods
org.apache.pdfbox.pdmodel.encryption

public Class PDEncryption

extends Object
implements COSObjectable
Class Inheritance
All Implemented Interfaces
org.apache.pdfbox.pdmodel.common.COSObjectable
Imports
java.io.IOException, java.util.Arrays, org.apache.pdfbox.cos.COSArray, .COSDictionary, .COSName, .COSString, org.apache.pdfbox.pdmodel.common.COSObjectable

This class is a specialized view of the encryption dictionary of a PDF document. It contains a low level dictionary (COSDictionary) and provides the methods to manage its fields. The available fields are the ones who are involved by standard security handler and public key security handler.
Authors
Ben Litchfield, Benoit Guillon

Field Summary

Modifier and TypeField and Description
public static final int
DEFAULT_LENGTH

The default length for the encryption key.

public static final String
DEFAULT_NAME

The default security handler.

public static final int
DEFAULT_VERSION

The default version, according to the PDF Reference.

private final COSDictionary
private SecurityHandler<ProtectionPolicy>
public static final int
VERSION0_UNDOCUMENTED_UNSUPPORTED

See PDF Reference 1.4 Table 3.13.

public static final int
VERSION1_40_BIT_ALGORITHM

See PDF Reference 1.4 Table 3.13.

public static final int
VERSION2_VARIABLE_LENGTH_ALGORITHM

See PDF Reference 1.4 Table 3.13.

public static final int
VERSION3_UNPUBLISHED_ALGORITHM

See PDF Reference 1.4 Table 3.13.

public static final int
VERSION4_SECURITY_HANDLER

See PDF Reference 1.4 Table 3.13.

Constructor Summary

AccessConstructor and Description
public
PDEncryption()

creates a new empty encryption dictionary.

public
PDEncryption(COSDictionary
a COS encryption dictionary
dictionary
)

creates a new encryption dictionary from the low level dictionary provided.

Method Summary

Modifier and TypeMethod and Description
public COSDictionary

Returns:

The COS dictionary that this object wraps.
getCOSObject
()

Implements org.apache.pdfbox.pdmodel.common.COSObjectable.getCOSObject.

This will get the dictionary associated with this encryption dictionary.

public PDCryptFilterDictionary

Returns:

the crypt filter with the given name if available
getCryptFilterDictionary
(COSName
the name of the crypt filter
cryptFilterName
)

Returns the crypt filter with the given name.

public PDCryptFilterDictionary

Returns:

the default crypt filter if available.
getDefaultCryptFilterDictionary
()

Returns the default crypt filter (for public-key security handler).

public final String

Returns:

The filter name contained in this encryption dictionary.
getFilter
()

Get the name of the filter.

public int

Returns:

The length in bits for the encryption algorithm
getLength
()

This will return the Length entry of the encryption dictionary.

public byte[]

Returns:

A 32 byte array or null if there is no owner encryption key.
getOwnerEncryptionKey
()

This will get the OE entry in the standard encryption dictionary.

public byte[]

Returns:

A 32 byte array or null if there is no owner key.
getOwnerKey
()

This will get the O entry in the standard encryption dictionary.

public int

Returns:

The permissions bit mask.
getPermissions
()

This will get the permissions bit mask.

public byte[]

Returns:

A 16 byte array or null if there is no Perms entry.
getPerms
()

Get the Perms entry in the encryption dictionary.

public int

Returns:

the number of recipients contained in the Recipients field.
getRecipientsLength
()

Returns the number of recipients contained in the Recipients field of the dictionary.

public COSString

Returns:

a COSString object containing information about the recipient number i.
getRecipientStringAt
(int
the position in the Recipients field array.
i
)

returns the COSString contained in the Recipients field at position i.

public int

Returns:

The encryption revision to use.
getRevision
()

This will return the R entry of the encryption dictionary.

public SecurityHandler<ProtectionPolicy>

Returns:

a security handler instance
getSecurityHandler
()

Returns the security handler specified in the dictionary's Filter entry.

public PDCryptFilterDictionary

Returns:

the standard crypt filter if available.
getStdCryptFilterDictionary
()

Returns the standard crypt filter.

public COSName

Returns:

the name of the filter
getStreamFilterName
()

Returns the name of the filter which is used for de/encrypting streams.

public COSName

Returns:

the name of the filter
getStringFilterName
()

Returns the name of the filter which is used for de/encrypting strings.

public String

Returns:

The subfilter name contained in this encryption dictionary.
getSubFilter
()

Get the name of the subfilter.

public byte[]

Returns:

A 32 byte array or null if there is no user encryption key.
getUserEncryptionKey
()

This will get the UE entry in the standard encryption dictionary.

public byte[]

Returns:

A 32 byte array or null if there is no user key.
getUserKey
()

This will get the U entry in the standard encryption dictionary.

public int

Returns:

The encryption version to use.
getVersion
()

This will return the V entry of the encryption dictionary.

public boolean

Returns:

true if the security handler is available
hasSecurityHandler
()

Returns true if the security handler specified in the dictionary's Filter is available.

public boolean

Returns:

true if EncryptMetaData is explicitly set to false (the default is true)
isEncryptMetaData
()

Will get the EncryptMetaData dictionary info.

public void
removeV45filters()

remove CF, StmF, and StrF entries.

public void
setCryptFilterDictionary(COSName
the name of the crypt filter
cryptFilterName
,
PDCryptFilterDictionary
the crypt filter to set
cryptFilterDictionary
)

Sets the crypt filter with the given name.

public void
setDefaultCryptFilterDictionary(PDCryptFilterDictionary
the standard crypt filter to set
defaultFilterDictionary
)

Sets the default crypt filter (for public-key security handler).

public void
setFilter(String
The filter name.
filter
)

Sets the filter entry of the encryption dictionary.

public void
setLength(int
The new key length.
length
)

This will set the number of bits to use for the encryption algorithm.

public void
setOwnerEncryptionKey(byte[]
A 32 byte array or null if there is no owner encryption key.
oe
)

This will set the OE entry in the standard encryption dictionary.

public void
setOwnerKey(byte[]
A 32 byte array or null if there is no owner key.
o
)

This will set the O entry in the standard encryption dictionary.

public void
setPermissions(int
The new permissions bit mask
permissions
)

This will set the permissions bit mask.

public void
setPerms(byte[]
A 16 byte array.
perms
)

Set the Perms entry in the encryption dictionary.

public void
setRecipients(byte[][]
the array of bytes arrays to put in the Recipients field.
recipients
)

This will set the Recipients field of the dictionary.

public void
setRevision(int
The new encryption version.
revision
)

This will set the R entry of the encryption dictionary.

public void
setSecurityHandler(SecurityHandler<ProtectionPolicy>
new security handler
securityHandler
)

Sets the security handler used in this encryption dictionary

public void
setStdCryptFilterDictionary(PDCryptFilterDictionary
the standard crypt filter to set
cryptFilterDictionary
)

Sets the standard crypt filter.

public void
setStreamFilterName(COSName
the name of the filter
streamFilterName
)

Sets the name of the filter which is used for de/encrypting streams.

public void
setStringFilterName(COSName
the name of the filter
stringFilterName
)

Sets the name of the filter which is used for de/encrypting strings.

public void
setSubFilter(String
The value of the subfilter field.
subfilter
)

Set the subfilter entry of the encryption dictionary.

public void
setUserEncryptionKey(byte[]
A 32 byte array or null if there is no user encryption key.
ue
)

This will set the UE entry in the standard encryption dictionary.

public void
setUserKey(byte[]
A 32 byte array.
u
)

This will set the U entry in the standard encryption dictionary.

public void
setVersion(int
The new encryption version.
version
)

This will set the V entry of the encryption dictionary.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

DEFAULT_LENGTHback to summary
public static final int DEFAULT_LENGTH

The default length for the encryption key.

DEFAULT_NAMEback to summary
public static final String DEFAULT_NAME

The default security handler.

DEFAULT_VERSIONback to summary
public static final int DEFAULT_VERSION

The default version, according to the PDF Reference.

dictionaryback to summary
private final COSDictionary dictionary
securityHandlerback to summary
private SecurityHandler<ProtectionPolicy> securityHandler
VERSION0_UNDOCUMENTED_UNSUPPORTEDback to summary
public static final int VERSION0_UNDOCUMENTED_UNSUPPORTED

See PDF Reference 1.4 Table 3.13.

VERSION1_40_BIT_ALGORITHMback to summary
public static final int VERSION1_40_BIT_ALGORITHM

See PDF Reference 1.4 Table 3.13.

VERSION2_VARIABLE_LENGTH_ALGORITHMback to summary
public static final int VERSION2_VARIABLE_LENGTH_ALGORITHM

See PDF Reference 1.4 Table 3.13.

VERSION3_UNPUBLISHED_ALGORITHMback to summary
public static final int VERSION3_UNPUBLISHED_ALGORITHM

See PDF Reference 1.4 Table 3.13.

VERSION4_SECURITY_HANDLERback to summary
public static final int VERSION4_SECURITY_HANDLER

See PDF Reference 1.4 Table 3.13.

Constructor Detail

PDEncryptionback to summary
public PDEncryption()

creates a new empty encryption dictionary.

PDEncryptionback to summary
public PDEncryption(COSDictionary dictionary)

creates a new encryption dictionary from the low level dictionary provided.

Parameters
dictionary:COSDictionary

a COS encryption dictionary

Method Detail

getCOSObjectback to summary
public COSDictionary getCOSObject()

Implements org.apache.pdfbox.pdmodel.common.COSObjectable.getCOSObject.

This will get the dictionary associated with this encryption dictionary.

Returns:COSDictionary

The COS dictionary that this object wraps.

Annotations
@Override
getCryptFilterDictionaryback to summary
public PDCryptFilterDictionary getCryptFilterDictionary(COSName cryptFilterName)

Returns the crypt filter with the given name.

Parameters
cryptFilterName:COSName

the name of the crypt filter

Returns:PDCryptFilterDictionary

the crypt filter with the given name if available

getDefaultCryptFilterDictionaryback to summary
public PDCryptFilterDictionary getDefaultCryptFilterDictionary()

Returns the default crypt filter (for public-key security handler).

Returns:PDCryptFilterDictionary

the default crypt filter if available.

getFilterback to summary
public final String getFilter()

Get the name of the filter.

Returns:String

The filter name contained in this encryption dictionary.

getLengthback to summary
public int getLength()

This will return the Length entry of the encryption dictionary.

The length in bits for the encryption algorithm. This will return a multiple of 8.

Returns:int

The length in bits for the encryption algorithm

getOwnerEncryptionKeyback to summary
public byte[] getOwnerEncryptionKey() throws IOException

This will get the OE entry in the standard encryption dictionary.

Returns:byte[]

A 32 byte array or null if there is no owner encryption key.

Exceptions
IOException:
If there is an error accessing the data.
getOwnerKeyback to summary
public byte[] getOwnerKey() throws IOException

This will get the O entry in the standard encryption dictionary.

Returns:byte[]

A 32 byte array or null if there is no owner key.

Exceptions
IOException:
If there is an error accessing the data.
getPermissionsback to summary
public int getPermissions()

This will get the permissions bit mask.

Returns:int

The permissions bit mask.

getPermsback to summary
public byte[] getPerms() throws IOException

Get the Perms entry in the encryption dictionary.

Returns:byte[]

A 16 byte array or null if there is no Perms entry.

Exceptions
IOException:
If there is an error accessing the data.
getRecipientsLengthback to summary
public int getRecipientsLength()

Returns the number of recipients contained in the Recipients field of the dictionary.

Returns:int

the number of recipients contained in the Recipients field.

getRecipientStringAtback to summary
public COSString getRecipientStringAt(int i)

returns the COSString contained in the Recipients field at position i.

Parameters
i:int

the position in the Recipients field array.

Returns:COSString

a COSString object containing information about the recipient number i.

getRevisionback to summary
public int getRevision()

This will return the R entry of the encryption dictionary.

See PDF Reference 1.4 Table 3.14.

Returns:int

The encryption revision to use.

getSecurityHandlerback to summary
public SecurityHandler<ProtectionPolicy> getSecurityHandler() throws IOException

Returns the security handler specified in the dictionary's Filter entry.

Returns:SecurityHandler<ProtectionPolicy>

a security handler instance

Exceptions
IOException:
if there is no security handler available which matches the Filter
getStdCryptFilterDictionaryback to summary
public PDCryptFilterDictionary getStdCryptFilterDictionary()

Returns the standard crypt filter.

Returns:PDCryptFilterDictionary

the standard crypt filter if available.

getStreamFilterNameback to summary
public COSName getStreamFilterName()

Returns the name of the filter which is used for de/encrypting streams. Default value is "Identity".

Returns:COSName

the name of the filter

getStringFilterNameback to summary
public COSName getStringFilterName()

Returns the name of the filter which is used for de/encrypting strings. Default value is "Identity".

Returns:COSName

the name of the filter

getSubFilterback to summary
public String getSubFilter()

Get the name of the subfilter.

Returns:String

The subfilter name contained in this encryption dictionary.

getUserEncryptionKeyback to summary
public byte[] getUserEncryptionKey() throws IOException

This will get the UE entry in the standard encryption dictionary.

Returns:byte[]

A 32 byte array or null if there is no user encryption key.

Exceptions
IOException:
If there is an error accessing the data.
getUserKeyback to summary
public byte[] getUserKey() throws IOException

This will get the U entry in the standard encryption dictionary.

Returns:byte[]

A 32 byte array or null if there is no user key.

Exceptions
IOException:
If there is an error accessing the data.
getVersionback to summary
public int getVersion()

This will return the V entry of the encryption dictionary.

See PDF Reference 1.4 Table 3.13.

Returns:int

The encryption version to use.

hasSecurityHandlerback to summary
public boolean hasSecurityHandler()

Returns true if the security handler specified in the dictionary's Filter is available.

Returns:boolean

true if the security handler is available

isEncryptMetaDataback to summary
public boolean isEncryptMetaData()

Will get the EncryptMetaData dictionary info.

Returns:boolean

true if EncryptMetaData is explicitly set to false (the default is true)

removeV45filtersback to summary
public void removeV45filters()

remove CF, StmF, and StrF entries. This is to be called if V is not 4 or 5.

setCryptFilterDictionaryback to summary
public void setCryptFilterDictionary(COSName cryptFilterName, PDCryptFilterDictionary cryptFilterDictionary)

Sets the crypt filter with the given name.

Parameters
cryptFilterName:COSName

the name of the crypt filter

cryptFilterDictionary:PDCryptFilterDictionary

the crypt filter to set

setDefaultCryptFilterDictionaryback to summary
public void setDefaultCryptFilterDictionary(PDCryptFilterDictionary defaultFilterDictionary)

Sets the default crypt filter (for public-key security handler).

Parameters
defaultFilterDictionary:PDCryptFilterDictionary

the standard crypt filter to set

setFilterback to summary
public void setFilter(String filter)

Sets the filter entry of the encryption dictionary.

Parameters
filter:String

The filter name.

setLengthback to summary
public void setLength(int length)

This will set the number of bits to use for the encryption algorithm.

Parameters
length:int

The new key length.

setOwnerEncryptionKeyback to summary
public void setOwnerEncryptionKey(byte[] oe) throws IOException

This will set the OE entry in the standard encryption dictionary.

Parameters
oe:byte[]

A 32 byte array or null if there is no owner encryption key.

Exceptions
IOException:
If there is an error setting the data.
setOwnerKeyback to summary
public void setOwnerKey(byte[] o) throws IOException

This will set the O entry in the standard encryption dictionary.

Parameters
o:byte[]

A 32 byte array or null if there is no owner key.

Exceptions
IOException:
If there is an error setting the data.
setPermissionsback to summary
public void setPermissions(int permissions)

This will set the permissions bit mask.

Parameters
permissions:int

The new permissions bit mask

setPermsback to summary
public void setPerms(byte[] perms) throws IOException

Set the Perms entry in the encryption dictionary.

Parameters
perms:byte[]

A 16 byte array.

Exceptions
IOException:
If there is an error setting the data.
setRecipientsback to summary
public void setRecipients(byte[][] recipients) throws IOException

This will set the Recipients field of the dictionary. This field contains an array of string.

Parameters
recipients:byte[][]

the array of bytes arrays to put in the Recipients field.

Exceptions
IOException:
If there is an error setting the data.
setRevisionback to summary
public void setRevision(int revision)

This will set the R entry of the encryption dictionary.

See PDF Reference 1.4 Table 3.14.

Note

This value is used to decrypt the pdf document. If you change this when the document is encrypted then decryption will fail!.

Parameters
revision:int

The new encryption version.

setSecurityHandlerback to summary
public void setSecurityHandler(SecurityHandler<ProtectionPolicy> securityHandler)

Sets the security handler used in this encryption dictionary

Parameters
securityHandler:SecurityHandler<ProtectionPolicy>

new security handler

setStdCryptFilterDictionaryback to summary
public void setStdCryptFilterDictionary(PDCryptFilterDictionary cryptFilterDictionary)

Sets the standard crypt filter.

Parameters
cryptFilterDictionary:PDCryptFilterDictionary

the standard crypt filter to set

setStreamFilterNameback to summary
public void setStreamFilterName(COSName streamFilterName)

Sets the name of the filter which is used for de/encrypting streams.

Parameters
streamFilterName:COSName

the name of the filter

setStringFilterNameback to summary
public void setStringFilterName(COSName stringFilterName)

Sets the name of the filter which is used for de/encrypting strings.

Parameters
stringFilterName:COSName

the name of the filter

setSubFilterback to summary
public void setSubFilter(String subfilter)

Set the subfilter entry of the encryption dictionary.

Parameters
subfilter:String

The value of the subfilter field.

setUserEncryptionKeyback to summary
public void setUserEncryptionKey(byte[] ue) throws IOException

This will set the UE entry in the standard encryption dictionary.

Parameters
ue:byte[]

A 32 byte array or null if there is no user encryption key.

Exceptions
IOException:
If there is an error setting the data.
setUserKeyback to summary
public void setUserKey(byte[] u) throws IOException

This will set the U entry in the standard encryption dictionary.

Parameters
u:byte[]

A 32 byte array.

Exceptions
IOException:
If there is an error setting the data.
setVersionback to summary
public void setVersion(int version)

This will set the V entry of the encryption dictionary.

See PDF Reference 1.4 Table 3.13.

Note

This value is used to decrypt the pdf document. If you change this when the document is encrypted then decryption will fail!.

Parameters
version:int

The new encryption version.