Package com.itextpdf.kernel.pdf
Class PdfEncryption
-
Constructor Summary
ConstructorsConstructorDescriptionPdfEncryption(byte[] userPassword, byte[] ownerPassword, int permissions, int encryptionType, byte[] documentId, PdfVersion version, AbstractMacIntegrityProtector macContainer) Creates the encryption.PdfEncryption(PdfDictionary pdfDict, byte[] password, byte[] documentId) CreatesPdfEncryptioninstance based on already existing standard encryption dictionary.PdfEncryption(PdfDictionary pdfDict, Key certificateKey, Certificate certificate, String certificateKeyProvider, IExternalDecryptionProcess externalDecryptionProcess) CreatesPdfEncryptioninstance based on already existing public encryption dictionary.PdfEncryption(Certificate[] certs, int[] permissions, int encryptionType, PdfVersion version, AbstractMacIntegrityProtector macContainer) Creates the certificate encryption. -
Method Summary
Modifier and TypeMethodDescriptionbyte[]computeUserPassword(byte[] ownerPassword) Computes user password if standard encryption handler is used with Standard40, Standard128 or AES128 algorithm.static PdfObjectcreateInfoId(byte[] id, boolean modified) Creates a PdfLiteral that contains an array of two id entries.static PdfObjectcreateInfoId(byte[] firstId, byte[] secondId, boolean preserveEncryption) Creates a PdfLiteral that contains an array of two id entries.byte[]decryptByteArray(byte[] b) byte[]encryptByteArray(byte[] b) voidflush()To manually flush aPdfObjectbehind this wrapper, you have to ensure that this object is added to the document, i.e.static byte[]intGets encryption algorithm and access permissions.byte[]intGets encryption algorithm.Gets the encryption permissions.booleanbooleanbooleanprotected booleanDefines if the object behind this wrapper must be an indirect object in the resultant document.voidsetHashKeyForNextObject(int objNumber, int objGeneration) Methods inherited from class com.itextpdf.kernel.pdf.PdfObjectWrapper
ensureObjectIsAddedToDocument, ensureUnderlyingObjectHasIndirectReference, getPdfObject, isFlushed, makeIndirect, makeIndirect, markObjectAsIndirect, setForbidRelease, setModified, setPdfObject, unsetForbidRelease
-
Constructor Details
-
PdfEncryption
public PdfEncryption(byte[] userPassword, byte[] ownerPassword, int permissions, int encryptionType, byte[] documentId, PdfVersion version, AbstractMacIntegrityProtector macContainer) Creates the encryption.- Parameters:
-
userPassword- the user password. Can be null or of zero length, which is equal to omitting the user password -
ownerPassword- the owner password. If it's null or empty, iText will generate a random string to be used as the owner password -
permissions- the user permissions The open permissions for the document can beEncryptionConstants.ALLOW_PRINTING,EncryptionConstants.ALLOW_MODIFY_CONTENTS,EncryptionConstants.ALLOW_COPY,EncryptionConstants.ALLOW_MODIFY_ANNOTATIONS,EncryptionConstants.ALLOW_FILL_IN,EncryptionConstants.ALLOW_SCREENREADERS,EncryptionConstants.ALLOW_ASSEMBLYandEncryptionConstants.ALLOW_DEGRADED_PRINTING. The permissions can be combined by ORing them -
encryptionType- the type of encryption. It can be one ofEncryptionConstants.STANDARD_ENCRYPTION_40,EncryptionConstants.STANDARD_ENCRYPTION_128,EncryptionConstants.ENCRYPTION_AES_128orEncryptionConstants.ENCRYPTION_AES_256. OptionallyEncryptionConstants.DO_NOT_ENCRYPT_METADATAcan be ORed to output the metadata in cleartext.EncryptionConstants.EMBEDDED_FILES_ONLYcan be ORed as well. Please be aware that the passed encryption types may override permissions:EncryptionConstants.STANDARD_ENCRYPTION_40implicitly setsEncryptionConstants.DO_NOT_ENCRYPT_METADATAandEncryptionConstants.EMBEDDED_FILES_ONLYas false;EncryptionConstants.STANDARD_ENCRYPTION_128implicitly setsEncryptionConstants.EMBEDDED_FILES_ONLYas false; -
documentId- document id which will be used for encryption -
version- thePdfVersionof the target document for encryption -
macContainer-AbstractMacIntegrityProtectorclass for MAC integrity protection
-
PdfEncryption
public PdfEncryption(Certificate[] certs, int[] permissions, int encryptionType, PdfVersion version, AbstractMacIntegrityProtector macContainer) Creates the certificate encryption.An array of one or more public certificates must be provided together with an array of the same size for the permissions for each certificate.
- Parameters:
-
certs- the public certificates to be used for the encryption -
permissions- the user permissions for each of the certificates The open permissions for the document can beEncryptionConstants.ALLOW_PRINTING,EncryptionConstants.ALLOW_MODIFY_CONTENTS,EncryptionConstants.ALLOW_COPY,EncryptionConstants.ALLOW_MODIFY_ANNOTATIONS,EncryptionConstants.ALLOW_FILL_IN,EncryptionConstants.ALLOW_SCREENREADERS,EncryptionConstants.ALLOW_ASSEMBLYandEncryptionConstants.ALLOW_DEGRADED_PRINTING. The permissions can be combined by ORing them -
encryptionType- the type of encryption. It can be one ofEncryptionConstants.STANDARD_ENCRYPTION_40,EncryptionConstants.STANDARD_ENCRYPTION_128,EncryptionConstants.ENCRYPTION_AES_128orEncryptionConstants.ENCRYPTION_AES_256. OptionallyEncryptionConstants.DO_NOT_ENCRYPT_METADATAcan be ORed to output the metadata in cleartext.EncryptionConstants.EMBEDDED_FILES_ONLYcan be ORed as well. Please be aware that the passed encryption types may override permissions:EncryptionConstants.STANDARD_ENCRYPTION_40implicitly setsEncryptionConstants.DO_NOT_ENCRYPT_METADATAandEncryptionConstants.EMBEDDED_FILES_ONLYas false;EncryptionConstants.STANDARD_ENCRYPTION_128implicitly setsEncryptionConstants.EMBEDDED_FILES_ONLYas false; -
version- thePdfVersionof the target document for encryption -
macContainer-AbstractMacIntegrityProtectorclass for MAC integrity protection
-
PdfEncryption
CreatesPdfEncryptioninstance based on already existing standard encryption dictionary.- Parameters:
-
pdfDict-PdfDictionary, which represents encryption dictionary -
password-byte[], which represents encryption password -
documentId- original file ID, the first element inPdfName.IDkey of trailer
-
PdfEncryption
public PdfEncryption(PdfDictionary pdfDict, Key certificateKey, Certificate certificate, String certificateKeyProvider, IExternalDecryptionProcess externalDecryptionProcess) CreatesPdfEncryptioninstance based on already existing public encryption dictionary.- Parameters:
-
pdfDict-PdfDictionary, which represents encryption dictionary -
certificateKey- the recipient privateKeyto the certificate -
certificate- the recipientCertificate, which serves as recipient identifier -
certificateKeyProvider- the certificate key provider id forSecurity.getProvider(java.lang.String) -
externalDecryptionProcess-IExternalDecryptionProcessthe external decryption process to be used
-
-
Method Details
-
generateNewDocumentId
public static byte[] generateNewDocumentId() -
createInfoId
Creates a PdfLiteral that contains an array of two id entries. These entries are both hexadecimal strings containing 16 hex characters. The first entry is the original id, the second entry should be different from the first one if the document has changed.- Parameters:
-
id- the first id -
modified- whether the document has been changed or not - Returns:
- PdfObject containing the two entries
-
createInfoId
Creates a PdfLiteral that contains an array of two id entries. These entries are both hexadecimal strings containing up to 16 hex characters. The first entry is the original id, the second entry should be different from the first one if the document has changed.- Parameters:
-
firstId- the first id -
secondId- the second id -
preserveEncryption- the encryption preserve - Returns:
- PdfObject containing the two entries.
-
getPermissions
Gets the encryption permissions. It can be used directly inWriterProperties.setStandardEncryption(byte[], byte[], int, int). See ISO 32000-1, Table 22 for more details.- Returns:
- the encryption permissions, an unsigned 32-bit quantity.
-
getCryptoMode
public int getCryptoMode()Gets encryption algorithm and access permissions.- Returns:
- the crypto mode value
- See Also:
-
getEncryptionAlgorithm
public int getEncryptionAlgorithm()Gets encryption algorithm.- Returns:
- the encryption algorithm
- See Also:
-
isMetadataEncrypted
public boolean isMetadataEncrypted() -
isEmbeddedFilesOnly
public boolean isEmbeddedFilesOnly() -
getDocumentId
public byte[] getDocumentId()- Returns:
- document id which was used for encryption. Could be null, if encryption doesn't rely on document id.
-
setHashKeyForNextObject
public void setHashKeyForNextObject(int objNumber, int objGeneration) -
getEncryptionStream
-
encryptByteArray
public byte[] encryptByteArray(byte[] b) -
decryptByteArray
public byte[] decryptByteArray(byte[] b) -
isOpenedWithFullPermission
public boolean isOpenedWithFullPermission() -
computeUserPassword
public byte[] computeUserPassword(byte[] ownerPassword) Computes user password if standard encryption handler is used with Standard40, Standard128 or AES128 algorithm.- Parameters:
-
ownerPassword- owner password of the encrypted document. - Returns:
- user password, or null if not a standard encryption handler was used.
-
flush
public void flush()To manually flush aPdfObjectbehind this wrapper, you have to ensure that this object is added to the document, i.e. it has an indirect reference. Basically this means that before flushing you need to explicitly callPdfObjectWrapper.makeIndirect(PdfDocument). For example: wrapperInstance.makeIndirect(document).flush(); Note that not every wrapper require this, only those that have such warning in documentation.- Overrides:
-
flushin classPdfObjectWrapper<PdfDictionary>
-
isWrappedObjectMustBeIndirect
protected boolean isWrappedObjectMustBeIndirect()Description copied from class:PdfObjectWrapperDefines if the object behind this wrapper must be an indirect object in the resultant document.
If this method returns true it doesn't necessarily mean that object must be in the indirect state at any moment, but rather defines that when the object will be written to the document it will be transformed into indirect object if it's not indirect yet.
Return value of this method shouldn't depend on any logic, it should return always true or false.- Specified by:
-
isWrappedObjectMustBeIndirectin classPdfObjectWrapper<PdfDictionary> - Returns:
- true if in the resultant document the object behind the wrapper must be indirect, otherwise false.
-