Class SecurityHandler
- java.lang.Object
-
- com.tom_roush.pdfbox.pdmodel.encryption.SecurityHandler
-
- Direct Known Subclasses:
PublicKeySecurityHandler,StandardSecurityHandler
public abstract class SecurityHandler extends Object
A security handler as described in the PDF specifications. A security handler is responsible of documents protection.
-
-
Field Summary
Fields Modifier and Type Field Description protected byte[]encryptionKeyThe encryption key that will used to encrypt / decrypt.protected intkeyLengthThe length of the secret key used to encrypt the document.
-
Constructor Summary
Constructors Constructor Description SecurityHandler()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voiddecrypt(COSBase obj, long objNum, long genNum)This will dispatch to the correct method.voiddecryptStream(COSStream stream, long objNum, long genNum)This will decrypt a stream.protected voidencryptDataRC4(byte[] finalKey, byte[] input, OutputStream output)Encrypt or decrypt data with RC4.protected voidencryptDataRC4(byte[] finalKey, InputStream input, OutputStream output)Encrypt or decrypt data with RC4.voidencryptStream(COSStream stream, long objNum, int genNum)This will encrypt a stream, but not the dictionary as the dictionary is encrypted by visitFromString() in COSWriter and we don't want to encrypt it twice.voidencryptString(COSString string, long objNum, int genNum)This will encrypt a string.AccessPermissiongetCurrentAccessPermission()Returns the access permissions that were computed during document decryption.intgetKeyLength()Getter of the property keyLength.abstract booleanhasProtectionPolicy()Returns whether a protection policy has been set.booleanisAES()True if AES is used for encryption and decryption.abstract voidprepareDocumentForEncryption(PDDocument doc)Prepare the document for encryption.abstract voidprepareForDecryption(PDEncryption encryption, COSArray documentIDArray, DecryptionMaterial decryptionMaterial)Prepares everything to decrypt the document.voidsetAES(boolean aesValue)Set to true if AES for encryption and decryption should be used.voidsetCurrentAccessPermission(AccessPermission currentAccessPermission)Sets the access permissions.protected voidsetDecryptMetadata(boolean decryptMetadata)Set wether to decrypt meta data.voidsetKeyLength(int keyLen)Setter of the property keyLength.
-
-
-
Method Detail
-
setDecryptMetadata
protected void setDecryptMetadata(boolean decryptMetadata)
Set wether to decrypt meta data.- Parameters:
decryptMetadata- true if meta data has to be decrypted.
-
prepareDocumentForEncryption
public abstract void prepareDocumentForEncryption(PDDocument doc) throws IOException
Prepare the document for encryption.- Parameters:
doc- The document that will be encrypted.- Throws:
IOException- If there is an error with the document.
-
prepareForDecryption
public abstract void prepareForDecryption(PDEncryption encryption, COSArray documentIDArray, DecryptionMaterial decryptionMaterial) throws IOException
Prepares everything to decrypt the document.- Parameters:
encryption- encryption dictionary, can be retrieved viaPDDocument.getEncryption()documentIDArray- document id which is returned viaCOSDocument.getDocumentID()decryptionMaterial- Information used to decrypt the document.- Throws:
IOException- If there is an error accessing data.
-
encryptDataRC4
protected void encryptDataRC4(byte[] finalKey, InputStream input, OutputStream output) throws IOExceptionEncrypt or decrypt data with RC4.- Parameters:
finalKey- The final key obtained with viacalcFinalKey(long, long).input- The data to encrypt.output- The output to write the encrypted data to.- Throws:
IOException- If there is an error reading the data.
-
encryptDataRC4
protected void encryptDataRC4(byte[] finalKey, byte[] input, OutputStream output) throws IOExceptionEncrypt or decrypt data with RC4.- Parameters:
finalKey- The final key obtained with viacalcFinalKey(long, long).input- The data to encrypt.output- The output to write the encrypted data to.- Throws:
IOException- If there is an error reading the data.
-
decrypt
public void decrypt(COSBase obj, long objNum, long genNum) throws IOException
This will dispatch to the correct method.- Parameters:
obj- The object to decrypt.objNum- The object number.genNum- The object generation Number.- Throws:
IOException- If there is an error getting the stream data.
-
decryptStream
public void decryptStream(COSStream stream, long objNum, long genNum) throws IOException
This will decrypt a stream.- Parameters:
stream- The stream to decrypt.objNum- The object number.genNum- The object generation number.- Throws:
IOException- If there is an error getting the stream data.
-
encryptStream
public void encryptStream(COSStream stream, long objNum, int genNum) throws IOException
This will encrypt a stream, but not the dictionary as the dictionary is encrypted by visitFromString() in COSWriter and we don't want to encrypt it twice.- Parameters:
stream- The stream to decrypt.objNum- The object number.genNum- The object generation number.- Throws:
IOException- If there is an error getting the stream data.
-
encryptString
public void encryptString(COSString string, long objNum, int genNum) throws IOException
This will encrypt a string.- Parameters:
string- the string to encrypt.objNum- The object number.genNum- The object generation number.- Throws:
IOException- If an error occurs writing the new string.
-
getKeyLength
public int getKeyLength()
Getter of the property keyLength.- Returns:
- Returns the keyLength.
-
setKeyLength
public void setKeyLength(int keyLen)
Setter of the property keyLength.- Parameters:
keyLen- The keyLength to set.
-
setCurrentAccessPermission
public void setCurrentAccessPermission(AccessPermission currentAccessPermission)
Sets the access permissions.- Parameters:
currentAccessPermission- The access permissions to be set.
-
getCurrentAccessPermission
public AccessPermission getCurrentAccessPermission()
Returns the access permissions that were computed during document decryption. The returned object is in read only mode.- Returns:
- the access permissions or null if the document was not decrypted.
-
isAES
public boolean isAES()
True if AES is used for encryption and decryption.- Returns:
- true if AEs is used
-
setAES
public void setAES(boolean aesValue)
Set to true if AES for encryption and decryption should be used.- Parameters:
aesValue- if true AES will be used
-
hasProtectionPolicy
public abstract boolean hasProtectionPolicy()
Returns whether a protection policy has been set.- Returns:
- true if a protection policy has been set
-
-