Class PdfEncryption
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final intprivate static final intprivate static final intprivate intprivate static final intprivate byte[]private booleanprivate booleanprivate static final intprivate static final intprivate AbstractMacIntegrityProtectorprivate Integerprivate SecurityHandlerprivate static longprivate static final intprivate static final int -
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 TypeMethodDescription(package private) voidcheckEncryptionRequirements(PdfDocument document) byte[]computeUserPassword(byte[] ownerPassword) Computes user password if standard encryption handler is used with Standard40, Standard128 or AES128 algorithm.private intconfigureAccessibilityPermissionsForMac(int permissions) (package private) voidconfigureEncryptionParametersFromReader(PdfDocument document, PdfDictionary trailer) (package private) voidstatic 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) private static intfixAccessibilityPermissionPdf20(int permissions) 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.(package private) AbstractMacIntegrityProtectorGets the encryption permissions.booleanbooleanbooleanprotected booleanDefines if the object behind this wrapper must be an indirect object in the resultant document.private static byte[]padByteArrayTo16(byte[] documentId) private intprivate int(package private) static booleanprivate intsetCryptoMode(int mode) private intsetCryptoMode(int mode, int length) voidsetHashKeyForNextObject(int objNumber, int objGeneration) private voidsetKeyLength(int keyLength) private void
-
Field Details
-
STANDARD_ENCRYPTION_40
private static final int STANDARD_ENCRYPTION_40- See Also:
-
STANDARD_ENCRYPTION_128
private static final int STANDARD_ENCRYPTION_128- See Also:
-
AES_128
private static final int AES_128- See Also:
-
AES_256
private static final int AES_256- See Also:
-
AES_GCM
private static final int AES_GCM- See Also:
-
DEFAULT_KEY_LENGTH
private static final int DEFAULT_KEY_LENGTH- See Also:
-
MAC_ENABLED
private static final int MAC_ENABLED- See Also:
-
MAC_DISABLED
private static final int MAC_DISABLED- See Also:
-
seq
private static long seq -
cryptoMode
private int cryptoMode -
permissions
-
encryptMetadata
private boolean encryptMetadata -
embeddedFilesOnly
private boolean embeddedFilesOnly -
documentId
private byte[] documentId -
securityHandler
-
macContainer
-
-
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 passwordownerPassword- the owner password. If it's null or empty, iText will generate a random string to be used as the owner passwordpermissions- 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 themencryptionType- 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 encryptionversion- thePdfVersionof the target document for encryptionmacContainer-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 encryptionpermissions- 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 themencryptionType- 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 encryptionmacContainer-AbstractMacIntegrityProtectorclass for MAC integrity protection
-
PdfEncryption
CreatesPdfEncryptioninstance based on already existing standard encryption dictionary.- Parameters:
pdfDict-PdfDictionary, which represents encryption dictionarypassword-byte[], which represents encryption passworddocumentId- 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 dictionarycertificateKey- the recipient privateKeyto the certificatecertificate- the recipientCertificate, which serves as recipient identifiercertificateKeyProvider- the certificate key provider id forSecurity.getProvider(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 idmodified- 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 idsecondId- the second idpreserveEncryption- the encryption preserve- Returns:
- PdfObject containing the two entries.
-
padByteArrayTo16
private static byte[] padByteArrayTo16(byte[] documentId) -
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.
-
setKeyLength
private void setKeyLength(int keyLength) -
setCryptoMode
private int setCryptoMode(int mode) -
setCryptoMode
private int setCryptoMode(int mode, int length) -
readAndSetCryptoModeForStdHandler
-
readAndSetCryptoModeForPubSecHandler
-
configureAccessibilityPermissionsForMac
private int configureAccessibilityPermissionsForMac(int permissions) -
readEmbeddedFilesOnlyFromEncryptDictionary
-
fixAccessibilityPermissionPdf20
private static int fixAccessibilityPermissionPdf20(int permissions) -
checkEncryptionRequirements
-
configureEncryptionParametersFromWriter
-
getMacContainer
AbstractMacIntegrityProtector getMacContainer() -
configureEncryptionParametersFromReader
-
updateMacPermission
private void updateMacPermission()
-