Class EncryptionToolsRevision4


  • public class EncryptionToolsRevision4
    extends Object
    Contains methods for encryption and decryption of PDF files.
    Author:
    Sergey Shemyakov
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static byte[] authenticateUserPassword​(String password, byte[] o, int p, byte[] id, int revision, boolean metadataIsEncrypted, int length, byte[] u)
      Authenticates user password and returns encryption key if password is correct.
      static byte[] computeEncryptionKey​(String password, byte[] o, int p, byte[] id, int revision, boolean metadataIsEncrypted, int length)
      Method computes encryption key for given data as specified in 7.6.3.3.
      static byte[] computeOValue​(String ownerPassword, int revision, int length, String userPassword)
      Method computes O value in standard encryption dict for standard security handler as specified in 7.6.3.4.
      static byte[] computeUValue​(String password, byte[] o, int p, byte[] id, int revision, boolean metadataIsEncrypted, int length)
      Method computes U value in standard encryption dict for standard security handler as specified in 7.6.3.4.
      static byte[] intToBytesLowOrderFirst​(long p)
      Represents given integer as byte array where low-order bytes go first.
    • Method Detail

      • computeEncryptionKey

        public static byte[] computeEncryptionKey​(String password,
                                                  byte[] o,
                                                  int p,
                                                  byte[] id,
                                                  int revision,
                                                  boolean metadataIsEncrypted,
                                                  int length)
                                           throws NoSuchAlgorithmException
        Method computes encryption key for given data as specified in 7.6.3.3. Algorithm 2 of PDF32000_2008.
        Parameters:
        password - is password string.
        o - is O value of encryption dict for standard security handler.
        p - is P value of encryption dict for standard security handler.
        id - is the value of the ID entry in the document’s trailer dict.
        revision - is R value of encryption dict for standard security handler.
        metadataIsEncrypted - is true if metadata in file is encrypted.
        length - is value of Length in encryption dict.
        Returns:
        encryption key.
        Throws:
        NoSuchAlgorithmException
      • computeOValue

        public static byte[] computeOValue​(String ownerPassword,
                                           int revision,
                                           int length,
                                           String userPassword)
                                    throws NoSuchAlgorithmException
        Method computes O value in standard encryption dict for standard security handler as specified in 7.6.3.4. Algorithm 3 of PDF32000_2008.
        Parameters:
        ownerPassword - is owner password.
        revision - is R value of encryption dict for standard security handler.
        length - is value of Length in encryption dict.
        userPassword - is user password.
        Returns:
        value of O for encryption dict for standard security handler.
        Throws:
        NoSuchAlgorithmException
      • computeUValue

        public static byte[] computeUValue​(String password,
                                           byte[] o,
                                           int p,
                                           byte[] id,
                                           int revision,
                                           boolean metadataIsEncrypted,
                                           int length)
                                    throws NoSuchAlgorithmException
        Method computes U value in standard encryption dict for standard security handler as specified in 7.6.3.4. Algorithm 4 and 5 of PDF32000_2008.
        Parameters:
        password - is user password.
        o - is O value of encryption dict for standard security handler.
        p - is P value of encryption dict for standard security handler.
        id - is the value of the ID entry in the document’s trailer dict.
        revision - is R value of encryption dict for standard security handler.
        metadataIsEncrypted - is true if metadata in file is encrypted.
        length - is value of Length in encryption dict.
        Returns:
        value of U for encryption dict for standard security handler.
        Throws:
        NoSuchAlgorithmException
      • authenticateUserPassword

        public static byte[] authenticateUserPassword​(String password,
                                                      byte[] o,
                                                      int p,
                                                      byte[] id,
                                                      int revision,
                                                      boolean metadataIsEncrypted,
                                                      int length,
                                                      byte[] u)
                                               throws NoSuchAlgorithmException
        Authenticates user password and returns encryption key if password is correct.
        Parameters:
        password - is string to be checked.
        o - is O value of encryption dict for standard security handler.
        p - is P value of encryption dict for standard security handler.
        id - is the value of the ID entry in the document’s trailer dict.
        revision - is R value of encryption dict for standard security handler.
        metadataIsEncrypted - is true if metadata in file is encrypted.
        length - is value of Length in encryption dict.
        u - is U value of encryption dict for standard security handler.
        Returns:
        null if password is incorrect and encryption key if it is correct.
        Throws:
        NoSuchAlgorithmException
      • intToBytesLowOrderFirst

        public static byte[] intToBytesLowOrderFirst​(long p)
        Represents given integer as byte array where low-order bytes go first.
        Parameters:
        p - is integer.
        Returns:
        array of 4 bytes that represent this number as unsigned 32-bit integer, low-order bytes go first.