Class PDFEncryptionJCE

    • Field Detail

      • digest

        private final java.security.MessageDigest digest
      • random

        private java.security.SecureRandom random
      • encryptionKey

        private byte[] encryptionKey
      • encryptionDictionary

        private java.lang.String encryptionDictionary
      • useAlgorithm31a

        private boolean useAlgorithm31a
      • encryptMetadata

        private boolean encryptMetadata
      • pdfVersion

        private Version pdfVersion
      • ivZero

        private static byte[] ivZero
    • Method Detail

      • make

        public static PDFEncryption make​(PDFObjectNumber objectNumber,
                                         PDFEncryptionParams params,
                                         PDFDocument pdf)
        Creates and returns an encryption object.
        Parameters:
        objectNumber - the object number for the encryption dictionary
        params - the encryption parameters
        pdf - the PDF document to be encrypted
        Returns:
        the newly created encryption object
      • encrypt

        public byte[] encrypt​(byte[] data,
                              PDFObject refObj)
        Encrypt an array of bytes using a reference PDFObject for calculating the encryption key.
        Specified by:
        encrypt in interface PDFEncryption
        Parameters:
        data - data to encrypt
        refObj - reference PDFObject
        Returns:
        byte[] the encrypted data
      • applyFilter

        public void applyFilter​(AbstractPDFStream stream)
        Adds a PDFFilter to the PDFStream object
        Specified by:
        applyFilter in interface PDFEncryption
        Parameters:
        stream - the stream to add an encryption filter to
      • toPDF

        public byte[] toPDF()
        Prepares the encryption dictionary for output to a PDF file.
        Overrides:
        toPDF in class PDFObject
        Returns:
        the encryption dictionary as a byte array
      • getTrailerEntry

        public java.lang.String getTrailerEntry()
        Returns the /Encrypt entry in the file trailer dictionary.
        Specified by:
        getTrailerEntry in interface PDFEncryption
        Returns:
        the string "/Encrypt n g R\n" where n and g are the number and generation of the document's encryption dictionary
      • encryptWithKey

        private static byte[] encryptWithKey​(byte[] key,
                                             byte[] data)
      • encryptWithKey

        private static byte[] encryptWithKey​(byte[] key,
                                             byte[] data,
                                             boolean noPadding,
                                             byte[] iv)
      • initCipher

        private static javax.crypto.Cipher initCipher​(byte[] key)
      • initCipher

        private static javax.crypto.Cipher initCipher​(byte[] key,
                                                      boolean noPadding,
                                                      byte[] iv)
      • createEncryptionKey

        private byte[] createEncryptionKey​(int objectNumber,
                                           int generationNumber)
        Applies Algorithm 3.1 from the PDF 1.4 Reference.
        Parameters:
        objectNumber - the object number
        generationNumber - the generation number
        Returns:
        the key to use for encryption
      • prepareMD5Input

        private byte[] prepareMD5Input​(int objectNumber,
                                       int generationNumber)
      • getPDFVersion

        public Version getPDFVersion()
        Returns the PDF version required by the current encryption algorithm.
        Specified by:
        getPDFVersion in interface PDFEncryption
        Returns:
        the PDF Version