Class PDFPassword


  • public class PDFPassword
    extends java.lang.Object

    Identifies a PDF Password, expressible either as a string or a byte sequence.

    In revisions up to version 1.e Expansion 3, the mapping between a string and the bytes corresponding to the password was poorly specified, meaning that the safest manner in which to specify a password was via a byte array. With 1.7 expansion 3, a still slightly problematic mapping was given for the Standard encryption algorithms through to version 4, and a very well specified mapping for the new version 5 encryption.

    So, for passwords specified in versions up to and including 4, a byte[] representation is the most accurate, but not necessarily the most convenient manner to provide passwords. For version 5, allowing passwords to be specified as Strings will be the preferred mechanism. Rather than specify two interfaces whenever a password can be provided - one for byte[] and one for String - we express the password as a class. This class can also offer a best guess at a String representation for a password for encryption versions up to and including 4.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      private static class  PDFPassword.CharsetEncoderGenerator
      Converts strings to byte by employing a CharsetEncoder and a configurable mechanism to replace or ignore characters that are unrepresentable according to the encoder.
      private static class  PDFPassword.IdentityEncodingByteGenerator
      Generate byte[] representations based on a Unicode code point identity encoding; characters over 255 in value are considered unrepresentable
      private static interface  PDFPassword.PasswordByteGenerator
      Converts a string password to a byte[] representation
      private static class  PDFPassword.PDFDocEncodingByteGenerator
      Generate byte[] representations based on the PDFDocEncoding
    • Constructor Summary

      Constructors 
      Constructor Description
      PDFPassword​(byte[] passwordBytes)
      Construct a byte-based password
      PDFPassword​(java.lang.String passwordString)
      Construct a string-based password
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private static java.util.List<byte[]> generatePossiblePasswordBytes​(java.lang.String passwordString)
      Generate some possible byte representations of a string password
      (package private) java.util.List<byte[]> getPasswordBytes​(boolean unicodeConversion)
      Get the password bytes.
      private boolean isAlphaNum7BitString​(java.lang.String string)  
      static PDFPassword nonNullPassword​(PDFPassword password)
      Ensure a non-null PDFPassword by substituting the empty password for a null password
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • EMPTY_PASSWORD

        public static final PDFPassword EMPTY_PASSWORD
        The empty password
      • passwordBytes

        private byte[] passwordBytes
        the password in bytes, if specified as such
      • passwordString

        private java.lang.String passwordString
        the passwird as a string, if specified as such
      • PASSWORD_BYTE_GENERATORS

        private static final PDFPassword.PasswordByteGenerator[] PASSWORD_BYTE_GENERATORS
        An array of password byte generators that attempts to enumerate the possible strategies that an encrypting application might take to convert a string to an array of bytes
    • Constructor Detail

      • PDFPassword

        public PDFPassword​(byte[] passwordBytes)
        Construct a byte-based password
        Parameters:
        passwordBytes - the password bytes
      • PDFPassword

        public PDFPassword​(java.lang.String passwordString)
        Construct a string-based password
        Parameters:
        passwordString - the password
    • Method Detail

      • nonNullPassword

        public static PDFPassword nonNullPassword​(PDFPassword password)
        Ensure a non-null PDFPassword by substituting the empty password for a null password
        Parameters:
        password - the password, may be null
        Returns:
        a non-null password
      • getPasswordBytes

        java.util.List<byte[]> getPasswordBytes​(boolean unicodeConversion)
        Get the password bytes.
        Parameters:
        unicodeConversion - whether the specific conversion from a unicode String, as present for version 5 encryption, should be used
        Returns:
        a list of possible password bytes
      • generatePossiblePasswordBytes

        private static java.util.List<byte[]> generatePossiblePasswordBytes​(java.lang.String passwordString)
        Generate some possible byte representations of a string password
        Parameters:
        passwordString - the string password
        Returns:
        a list of unique possible byte representations
      • isAlphaNum7BitString

        private boolean isAlphaNum7BitString​(java.lang.String string)