Class PDEncryption

  • All Implemented Interfaces:
    com.pd4ml.pdf.parser.obj.Obj

    public class PDEncryption
    extends com.pd4ml.pdf.parser.obj.DictObject
    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.
    Author:
    Ben Litchfield, Benoit Guillon
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int DEFAULT_LENGTH
      The default length for the encryption key.
      static java.lang.String DEFAULT_NAME
      The default security handler.
      static int DEFAULT_VERSION
      The default version, according to the PDF Reference.
      static int VERSION0_UNDOCUMENTED_UNSUPPORTED
      See PDF Reference 1.4 Table 3.13.
      static int VERSION1_40_BIT_ALGORITHM
      See PDF Reference 1.4 Table 3.13.
      static int VERSION2_VARIABLE_LENGTH_ALGORITHM
      See PDF Reference 1.4 Table 3.13.
      static int VERSION3_UNPUBLISHED_ALGORITHM
      See PDF Reference 1.4 Table 3.13.
      static int VERSION4_SECURITY_HANDLER
      See PDF Reference 1.4 Table 3.13.
      • Fields inherited from class com.pd4ml.pdf.parser.obj.DictObject

        entries
      • Fields inherited from class com.pd4ml.pdf.parser.obj.PdfObject

        id, touched, xref
      • Fields inherited from interface com.pd4ml.pdf.parser.obj.Obj

        ARRAY, BOOL, CMD, DICT, EOF, ID, INDIRECT, INT, NAME, NEWSTREAM, NULL, REAL, REF, STREAM, STRING
    • Constructor Summary

      Constructors 
      Constructor Description
      PDEncryption​(com.pd4ml.pdf.parser.obj.DictObject dict)
      wraps given DictObject
      PDEncryption​(com.pd4ml.pdf.parser.Objects xref)
      creates a new empty encryption dictionary.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      PDCryptFilterDictionary getCryptFilterDictionary​(java.lang.String cryptFilterName)
      Returns the crypt filter with the given name.
      PDCryptFilterDictionary getDefaultCryptFilterDictionary()
      Returns the default crypt filter (for public-key security handler).
      com.pd4ml.pdf.parser.obj.Name getFilter()
      Get the name of the filter.
      int getLength()
      This will return the Length entry of the encryption dictionary.

      The length in bits for the encryption algorithm.
      byte[] getOwnerEncryptionKey()
      This will get the OE entry in the standard encryption dictionary.
      byte[] getOwnerKey()
      This will get the O entry in the standard encryption dictionary.
      int getPermissions()
      This will get the permissions bit mask.
      byte[] getPerms()
      Get the Perms entry in the encryption dictionary.
      int getRecipientsLength()
      Returns the number of recipients contained in the Recipients field of the dictionary.
      com.pd4ml.pdf.parser.obj.Str getRecipientStringAt​(int i)
      returns the COSString contained in the Recipients field at position i.
      int getRevision()
      This will return the R entry of the encryption dictionary.

      See PDF Reference 1.4 Table 3.14.
      PDCryptFilterDictionary getStdCryptFilterDictionary()
      Returns the standard crypt filter.
      java.lang.String getStreamFilterName()
      Returns the name of the filter which is used for de/encrypting streams.
      java.lang.String getStringFilterName()
      Returns the name of the filter which is used for de/encrypting strings.
      com.pd4ml.pdf.parser.obj.Name getSubFilter()
      Get the name of the subfilter.
      byte[] getUserEncryptionKey()
      This will get the UE entry in the standard encryption dictionary.
      byte[] getUserKey()
      This will get the U entry in the standard encryption dictionary.
      int getVersion()
      This will return the V entry of the encryption dictionary.

      See PDF Reference 1.4 Table 3.13.
      boolean isEncryptMetaData()
      Will get the EncryptMetaData dictionary info.
      void removeV45filters()
      remove CF, StmF, and StrF entries.
      void setCryptFilterDictionary​(java.lang.String cryptFilterName, PDCryptFilterDictionary cryptFilterDictionary)
      Sets the crypt filter with the given name.
      void setDefaultCryptFilterDictionary​(PDCryptFilterDictionary defaultFilterDictionary)
      Sets the default crypt filter (for public-key security handler).
      void setFilter​(java.lang.String filter)
      Sets the filter entry of the encryption dictionary.
      void setLength​(int length)
      This will set the number of bits to use for the encryption algorithm.
      void setOwnerEncryptionKey​(byte[] oe)
      This will set the OE entry in the standard encryption dictionary.
      void setOwnerKey​(byte[] o)
      This will set the O entry in the standard encryption dictionary.
      void setPermissions​(int permissions)
      This will set the permissions bit mask.
      void setPerms​(byte[] perms)
      Set the Perms entry in the encryption dictionary.
      void setRecipients​(byte[][] recipients)
      This will set the Recipients field of the dictionary.
      void setRevision​(int revision)
      This will set the R entry of the encryption dictionary.

      See PDF Reference 1.4 Table 3.14.
      void setStdCryptFilterDictionary​(PDCryptFilterDictionary cryptFilterDictionary)
      Sets the standard crypt filter.
      void setStreamFilterName​(java.lang.String streamFilterName)
      Sets the name of the filter which is used for de/encrypting streams.
      void setStringFilterName​(java.lang.String stringFilterName)
      Sets the name of the filter which is used for de/encrypting strings.
      void setSubFilter​(java.lang.String subfilter)
      Set the subfilter entry of the encryption dictionary.
      void setUserEncryptionKey​(byte[] ue)
      This will set the UE entry in the standard encryption dictionary.
      void setUserKey​(byte[] u)
      This will set the U entry in the standard encryption dictionary.
      void setVersion​(int version)
      This will set the V entry of the encryption dictionary.

      See PDF Reference 1.4 Table 3.13.
      • Methods inherited from class com.pd4ml.pdf.parser.obj.DictObject

        addAll, addEntry, assignXref, equals, get, get, get, getInt, getInt, getObject, getPageObjects, getType, has, isEmpty, remove, set, setToBeWritten, toString, touch, values, write
      • Methods inherited from class com.pd4ml.pdf.parser.obj.Dict

        getDict, hasStream, reassignParents, setStream
      • Methods inherited from class com.pd4ml.pdf.parser.obj.PdfObject

        resolveReferences, storeOffset
      • Methods inherited from class java.lang.Object

        getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • VERSION0_UNDOCUMENTED_UNSUPPORTED

        public static final int VERSION0_UNDOCUMENTED_UNSUPPORTED
        See PDF Reference 1.4 Table 3.13.
        See Also:
        Constant Field Values
      • VERSION1_40_BIT_ALGORITHM

        public static final int VERSION1_40_BIT_ALGORITHM
        See PDF Reference 1.4 Table 3.13.
        See Also:
        Constant Field Values
      • VERSION2_VARIABLE_LENGTH_ALGORITHM

        public static final int VERSION2_VARIABLE_LENGTH_ALGORITHM
        See PDF Reference 1.4 Table 3.13.
        See Also:
        Constant Field Values
      • VERSION3_UNPUBLISHED_ALGORITHM

        public static final int VERSION3_UNPUBLISHED_ALGORITHM
        See PDF Reference 1.4 Table 3.13.
        See Also:
        Constant Field Values
      • VERSION4_SECURITY_HANDLER

        public static final int VERSION4_SECURITY_HANDLER
        See PDF Reference 1.4 Table 3.13.
        See Also:
        Constant Field Values
      • DEFAULT_NAME

        public static final java.lang.String DEFAULT_NAME
        The default security handler.
        See Also:
        Constant Field Values
      • DEFAULT_LENGTH

        public static final int DEFAULT_LENGTH
        The default length for the encryption key.
        See Also:
        Constant Field Values
      • DEFAULT_VERSION

        public static final int DEFAULT_VERSION
        The default version, according to the PDF Reference.
        See Also:
        Constant Field Values
    • Constructor Detail

      • PDEncryption

        public PDEncryption​(com.pd4ml.pdf.parser.Objects xref)
        creates a new empty encryption dictionary.
        Parameters:
        xref - xreference
      • PDEncryption

        public PDEncryption​(com.pd4ml.pdf.parser.obj.DictObject dict)
        wraps given DictObject
        Parameters:
        dict - encryption dictionary object
    • Method Detail

      • setFilter

        public void setFilter​(java.lang.String filter)
        Sets the filter entry of the encryption dictionary.
        Parameters:
        filter - The filter name.
      • getFilter

        public final com.pd4ml.pdf.parser.obj.Name getFilter()
        Get the name of the filter.
        Returns:
        The filter name contained in this encryption dictionary.
      • getSubFilter

        public com.pd4ml.pdf.parser.obj.Name getSubFilter()
        Get the name of the subfilter.
        Returns:
        The subfilter name contained in this encryption dictionary.
      • setSubFilter

        public void setSubFilter​(java.lang.String subfilter)
        Set the subfilter entry of the encryption dictionary.
        Parameters:
        subfilter - The value of the subfilter field.
      • setVersion

        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 - The new encryption version.
      • getVersion

        public int getVersion()
        This will return the V entry of the encryption dictionary.

        See PDF Reference 1.4 Table 3.13.
        Returns:
        The encryption version to use.
      • setLength

        public void setLength​(int length)
        This will set the number of bits to use for the encryption algorithm.
        Parameters:
        length - The new key length.
      • getLength

        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:
        The length in bits for the encryption algorithm
      • setRevision

        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 - The new encryption version.
      • getRevision

        public int getRevision()
        This will return the R entry of the encryption dictionary.

        See PDF Reference 1.4 Table 3.14.
        Returns:
        The encryption revision to use.
      • setOwnerKey

        public void setOwnerKey​(byte[] o)
                         throws java.io.IOException
        This will set the O entry in the standard encryption dictionary.
        Parameters:
        o - A 32 byte array or null if there is no owner key.
        Throws:
        java.io.IOException - If there is an error setting the data.
      • getOwnerKey

        public byte[] getOwnerKey()
                           throws java.io.IOException
        This will get the O entry in the standard encryption dictionary.
        Returns:
        A 32 byte array or null if there is no owner key.
        Throws:
        java.io.IOException - If there is an error accessing the data.
      • setUserKey

        public void setUserKey​(byte[] u)
                        throws java.io.IOException
        This will set the U entry in the standard encryption dictionary.
        Parameters:
        u - A 32 byte array.
        Throws:
        java.io.IOException - If there is an error setting the data.
      • getUserKey

        public byte[] getUserKey()
                          throws java.io.IOException
        This will get the U entry in the standard encryption dictionary.
        Returns:
        A 32 byte array or null if there is no user key.
        Throws:
        java.io.IOException - If there is an error accessing the data.
      • setOwnerEncryptionKey

        public void setOwnerEncryptionKey​(byte[] oe)
                                   throws java.io.IOException
        This will set the OE entry in the standard encryption dictionary.
        Parameters:
        oe - A 32 byte array or null if there is no owner encryption key.
        Throws:
        java.io.IOException - If there is an error setting the data.
      • getOwnerEncryptionKey

        public byte[] getOwnerEncryptionKey()
                                     throws java.io.IOException
        This will get the OE entry in the standard encryption dictionary.
        Returns:
        A 32 byte array or null if there is no owner encryption key.
        Throws:
        java.io.IOException - If there is an error accessing the data.
      • setUserEncryptionKey

        public void setUserEncryptionKey​(byte[] ue)
                                  throws java.io.IOException
        This will set the UE entry in the standard encryption dictionary.
        Parameters:
        ue - A 32 byte array or null if there is no user encryption key.
        Throws:
        java.io.IOException - If there is an error setting the data.
      • getUserEncryptionKey

        public byte[] getUserEncryptionKey()
                                    throws java.io.IOException
        This will get the UE entry in the standard encryption dictionary.
        Returns:
        A 32 byte array or null if there is no user encryption key.
        Throws:
        java.io.IOException - If there is an error accessing the data.
      • setPermissions

        public void setPermissions​(int permissions)
        This will set the permissions bit mask.
        Parameters:
        permissions - The new permissions bit mask
      • getPermissions

        public int getPermissions()
        This will get the permissions bit mask.
        Returns:
        The permissions bit mask.
      • isEncryptMetaData

        public boolean isEncryptMetaData()
        Will get the EncryptMetaData dictionary info.
        Returns:
        true if EncryptMetaData is explicitly set to false (the default is true)
      • setRecipients

        public void setRecipients​(byte[][] recipients)
                           throws java.io.IOException
        This will set the Recipients field of the dictionary. This field contains an array of string.
        Parameters:
        recipients - the array of bytes arrays to put in the Recipients field.
        Throws:
        java.io.IOException - If there is an error setting the data.
      • getRecipientsLength

        public int getRecipientsLength()
        Returns the number of recipients contained in the Recipients field of the dictionary.
        Returns:
        the number of recipients contained in the Recipients field.
      • getRecipientStringAt

        public com.pd4ml.pdf.parser.obj.Str getRecipientStringAt​(int i)
        returns the COSString contained in the Recipients field at position i.
        Parameters:
        i - the position in the Recipients field array.
        Returns:
        a COSString object containing information about the recipient number i.
      • getStdCryptFilterDictionary

        public PDCryptFilterDictionary getStdCryptFilterDictionary()
        Returns the standard crypt filter.
        Returns:
        the standard crypt filter if available.
      • getDefaultCryptFilterDictionary

        public PDCryptFilterDictionary getDefaultCryptFilterDictionary()
        Returns the default crypt filter (for public-key security handler).
        Returns:
        the default crypt filter if available.
      • getCryptFilterDictionary

        public PDCryptFilterDictionary getCryptFilterDictionary​(java.lang.String cryptFilterName)
        Returns the crypt filter with the given name.
        Parameters:
        cryptFilterName - the name of the crypt filter
        Returns:
        the crypt filter with the given name if available
      • setCryptFilterDictionary

        public void setCryptFilterDictionary​(java.lang.String cryptFilterName,
                                             PDCryptFilterDictionary cryptFilterDictionary)
        Sets the crypt filter with the given name.
        Parameters:
        cryptFilterName - the name of the crypt filter
        cryptFilterDictionary - the crypt filter to set
      • setStdCryptFilterDictionary

        public void setStdCryptFilterDictionary​(PDCryptFilterDictionary cryptFilterDictionary)
        Sets the standard crypt filter.
        Parameters:
        cryptFilterDictionary - the standard crypt filter to set
      • setDefaultCryptFilterDictionary

        public void setDefaultCryptFilterDictionary​(PDCryptFilterDictionary defaultFilterDictionary)
        Sets the default crypt filter (for public-key security handler).
        Parameters:
        defaultFilterDictionary - the standard crypt filter to set
      • getStreamFilterName

        public java.lang.String getStreamFilterName()
        Returns the name of the filter which is used for de/encrypting streams. Default value is "Identity".
        Returns:
        the name of the filter
      • setStreamFilterName

        public void setStreamFilterName​(java.lang.String streamFilterName)
        Sets the name of the filter which is used for de/encrypting streams.
        Parameters:
        streamFilterName - the name of the filter
      • getStringFilterName

        public java.lang.String getStringFilterName()
        Returns the name of the filter which is used for de/encrypting strings. Default value is "Identity".
        Returns:
        the name of the filter
      • setStringFilterName

        public void setStringFilterName​(java.lang.String stringFilterName)
        Sets the name of the filter which is used for de/encrypting strings.
        Parameters:
        stringFilterName - the name of the filter
      • setPerms

        public void setPerms​(byte[] perms)
                      throws java.io.IOException
        Set the Perms entry in the encryption dictionary.
        Parameters:
        perms - A 16 byte array.
        Throws:
        java.io.IOException - If there is an error setting the data.
      • getPerms

        public byte[] getPerms()
                        throws java.io.IOException
        Get the Perms entry in the encryption dictionary.
        Returns:
        A 16 byte array or null if there is no Perms entry.
        Throws:
        java.io.IOException - If there is an error accessing the data.
      • removeV45filters

        public void removeV45filters()
        remove CF, StmF, and StrF entries. This is to be called if V is not 4 or 5.