Interface CryptoCipher
-
- All Superinterfaces:
java.lang.AutoCloseable,java.io.Closeable
- All Known Implementing Classes:
JceCipher,OpenSslCipher,OpenSslJnaCipher
public interface CryptoCipher extends java.io.CloseableThe interface of cryptographic cipher for encryption and decryption.Note that implementations must provide a constructor that has 2 parameters: a Properties instance and a String (transformation)
-
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description intdoFinal(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset)Encrypts or decrypts data in a single-part operation, or finishes a multiple-part operation.intdoFinal(java.nio.ByteBuffer inBuffer, java.nio.ByteBuffer outBuffer)Encrypts or decrypts data in a single-part operation, or finishes a multiple-part operation.java.lang.StringgetAlgorithm()Returns the algorithm name of thisCryptoCipherobject.intgetBlockSize()Returns the block size (in bytes).voidinit(int mode, java.security.Key key, java.security.spec.AlgorithmParameterSpec params)Initializes the cipher with mode, key and iv.intupdate(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset)Continues a multiple-part encryption/decryption operation.intupdate(java.nio.ByteBuffer inBuffer, java.nio.ByteBuffer outBuffer)Continues a multiple-part encryption/decryption operation.default voidupdateAAD(byte[] aad)Continues a multi-part update of the Additional Authentication Data (AAD).default voidupdateAAD(java.nio.ByteBuffer aad)Continues a multi-part update of the Additional Authentication Data (AAD).
-
-
-
Method Detail
-
doFinal
int doFinal(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) throws javax.crypto.ShortBufferException, javax.crypto.IllegalBlockSizeException, javax.crypto.BadPaddingExceptionEncrypts or decrypts data in a single-part operation, or finishes a multiple-part operation.- Parameters:
input- the input byte arrayinputOffset- the offset in input where the input startsinputLen- the input lengthoutput- the byte array for the resultoutputOffset- the offset in output where the result is stored- Returns:
- the number of bytes stored in output
- Throws:
javax.crypto.ShortBufferException- if the given output byte array is too small to hold the resultjavax.crypto.BadPaddingException- if this cipher is in decryption mode, and (un)padding has been requested, but the decrypted data is not bounded by the appropriate padding bytesjavax.crypto.IllegalBlockSizeException- if this cipher is a block cipher, no padding has been requested (only in encryption mode), and the total input length of the data processed by this cipher is not a multiple of block size; or if this encryption algorithm is unable to process the input data provided.
-
doFinal
int doFinal(java.nio.ByteBuffer inBuffer, java.nio.ByteBuffer outBuffer) throws javax.crypto.ShortBufferException, javax.crypto.IllegalBlockSizeException, javax.crypto.BadPaddingExceptionEncrypts or decrypts data in a single-part operation, or finishes a multiple-part operation.- Parameters:
inBuffer- the input ByteBufferoutBuffer- the output ByteBuffer- Returns:
- int number of bytes stored in
output - Throws:
javax.crypto.BadPaddingException- if this cipher is in decryption mode, and (un)padding has been requested, but the decrypted data is not bounded by the appropriate padding bytesjavax.crypto.IllegalBlockSizeException- if this cipher is a block cipher, no padding has been requested (only in encryption mode), and the total input length of the data processed by this cipher is not a multiple of block size; or if this encryption algorithm is unable to process the input data provided.javax.crypto.ShortBufferException- if the given output buffer is too small to hold the result
-
getAlgorithm
java.lang.String getAlgorithm()
Returns the algorithm name of thisCryptoCipherobject.This is the same name that was specified in one of the
CryptoCipherFactory#getInstancecalls that created thisCryptoCipherobject..- Returns:
- the algorithm name of this
CryptoCipherobject.
-
getBlockSize
int getBlockSize()
Returns the block size (in bytes).- Returns:
- the block size (in bytes), or 0 if the underlying algorithm is not a block cipher
-
init
void init(int mode, java.security.Key key, java.security.spec.AlgorithmParameterSpec params) throws java.security.InvalidKeyException, java.security.InvalidAlgorithmParameterExceptionInitializes the cipher with mode, key and iv.- Parameters:
mode-Cipher.ENCRYPT_MODEorCipher.DECRYPT_MODEkey- crypto key for the cipherparams- the algorithm parameters- Throws:
java.security.InvalidKeyException- if the given key is inappropriate for initializing this cipher, or its keysize exceeds the maximum allowable keysize (as determined from the configured jurisdiction policy files).java.security.InvalidAlgorithmParameterException- if the given algorithm parameters are inappropriate for this cipher, or this cipher requires algorithm parameters andparamsis null, or the given algorithm parameters imply a cryptographic strength that would exceed the legal limits (as determined from the configured jurisdiction policy files).
-
update
int update(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) throws javax.crypto.ShortBufferExceptionContinues a multiple-part encryption/decryption operation. The data is encrypted or decrypted, depending on how this cipher was initialized.- Parameters:
input- the input byte arrayinputOffset- the offset in input where the input startsinputLen- the input lengthoutput- the byte array for the resultoutputOffset- the offset in output where the result is stored- Returns:
- the number of bytes stored in output
- Throws:
javax.crypto.ShortBufferException- if there is insufficient space in the output byte array
-
update
int update(java.nio.ByteBuffer inBuffer, java.nio.ByteBuffer outBuffer) throws javax.crypto.ShortBufferExceptionContinues a multiple-part encryption/decryption operation. The data is encrypted or decrypted, depending on how this cipher was initialized.- Parameters:
inBuffer- the input ByteBufferoutBuffer- the output ByteBuffer- Returns:
- int number of bytes stored in
output - Throws:
javax.crypto.ShortBufferException- if there is insufficient space in the output buffer
-
updateAAD
default void updateAAD(byte[] aad) throws java.lang.IllegalArgumentException, java.lang.IllegalStateException, java.lang.UnsupportedOperationExceptionContinues a multi-part update of the Additional Authentication Data (AAD).Calls to this method provide AAD to the cipher when operating in modes such as AEAD (GCM). If this cipher is operating in GCM mode, all AAD must be supplied before beginning operations on the ciphertext (via the
updateanddoFinalmethods).- Parameters:
aad- the buffer containing the Additional Authentication Data- Throws:
java.lang.IllegalArgumentException- if theaadbyte array is nulljava.lang.IllegalStateException- if this cipher is in a wrong state (e.g., has not been initialized), does not accept AAD, or if operating in either GCM or CCM mode and one of theupdatemethods has already been called for the active encryption/decryption operationjava.lang.UnsupportedOperationException- if the corresponding method has not been overridden by an implementation
-
updateAAD
default void updateAAD(java.nio.ByteBuffer aad) throws java.lang.IllegalArgumentException, java.lang.IllegalStateException, java.lang.UnsupportedOperationExceptionContinues a multi-part update of the Additional Authentication Data (AAD).Calls to this method provide AAD to the cipher when operating in modes such as AEAD (GCM). If this cipher is operating in GCM mode, all AAD must be supplied before beginning operations on the ciphertext (via the
updateanddoFinalmethods).- Parameters:
aad- the buffer containing the Additional Authentication Data- Throws:
java.lang.IllegalArgumentException- if theaadbyte array is nulljava.lang.IllegalStateException- if this cipher is in a wrong state (e.g., has not been initialized), does not accept AAD, or if operating in either GCM or CCM mode and one of theupdatemethods has already been called for the active encryption/decryption operationjava.lang.UnsupportedOperationException- if the corresponding method has not been overridden by an implementation
-
-