Class OpenSSLCipher
- Direct Known Subclasses:
OpenSSLCipher.AES,OpenSSLCipher.ARC4,OpenSSLCipher.DESEDE
public abstract class OpenSSLCipher extends CipherSpi
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classOpenSSLCipher.AESstatic classOpenSSLCipher.ARC4static classOpenSSLCipher.DESEDEprotected static classOpenSSLCipher.ModeModes that a block cipher may support.protected static classOpenSSLCipher.PaddingPaddings that a block cipher may support. -
Constructor Summary
Constructors Modifier Constructor Description protectedOpenSSLCipher()protectedOpenSSLCipher(OpenSSLCipher.Mode mode, OpenSSLCipher.Padding padding) -
Method Summary
Modifier and Type Method Description protected abstract voidcheckSupportedKeySize(int keySize)Checks whether the cipher supports this particularkeySize(in bytes) and throwsInvalidKeyExceptionif it doesn't.protected abstract voidcheckSupportedMode(OpenSSLCipher.Mode mode)Checks whether the cipher supports this particular ciphermodeand throwsNoSuchAlgorithmExceptionif it doesn't.protected abstract voidcheckSupportedPadding(OpenSSLCipher.Padding padding)Checks whether the cipher supports this particular cipherpaddingand throwsNoSuchPaddingExceptionif it doesn't.protected byte[]engineDoFinal(byte[] input, int inputOffset, int inputLen)Finishes a multi-part transformation (encryption or decryption).protected intengineDoFinal(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset)Finishes a multi-part transformation (encryption or decryption).protected intengineGetBlockSize()Returns the block size of this cipher (in bytes)protected byte[]engineGetIV()Returns the Initialization Vector (IV) that was used to initialize this cipher ornullif none was used.protected intengineGetOutputSize(int inputLen)Returns the size for a buffer (in bytes), that the next call toupdateofdoFinalwould return, taking into account any buffered data from previousupdatecalls and padding.protected AlgorithmParametersengineGetParameters()Returns the parameters that where used to create this cipher instance.protected voidengineInit(int opmode, Key key, AlgorithmParameters params, SecureRandom random)Initializes this cipher instance with the specified key, algorithm parameters and a source of randomness.protected voidengineInit(int opmode, Key key, SecureRandom random)Initializes this cipher instance with the specified key and a source of randomness.protected voidengineInit(int opmode, Key key, AlgorithmParameterSpec params, SecureRandom random)Initializes this cipher instance with the specified key, algorithm parameters and a source of randomness.protected voidengineSetMode(String modeStr)Sets the mode for this cipher.protected voidengineSetPadding(String paddingStr)Sets the padding method for this cipher.protected KeyengineUnwrap(byte[] wrappedKey, String wrappedKeyAlgorithm, int wrappedKeyType)Unwraps a key using this cipher instance.protected byte[]engineUpdate(byte[] input, int inputOffset, int inputLen)Continues a multi-part transformation (encryption or decryption).protected intengineUpdate(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset)Continues a multi-part transformation (encryption or decryption).protected byte[]engineWrap(Key key)Wraps a key using this cipher instance.protected abstract StringgetBaseCipherName()Returns the standard name for the particular algorithm.protected abstract intgetCipherBlockSize()protected abstract StringgetCipherName(int keySize, OpenSSLCipher.Mode mode)Returns the OpenSSL cipher name for the particularkeySizeand ciphermode.protected booleansupportsVariableSizeKey()Methods inherited from class javax.crypto.CipherSpi
engineDoFinal, engineGetKeySize, engineUpdate, engineUpdateAAD, engineUpdateAAD
-
Constructor Details
-
OpenSSLCipher
protected OpenSSLCipher() -
OpenSSLCipher
-
-
Method Details
-
getBaseCipherName
Returns the standard name for the particular algorithm. -
getCipherName
Returns the OpenSSL cipher name for the particularkeySizeand ciphermode. -
checkSupportedKeySize
Checks whether the cipher supports this particularkeySize(in bytes) and throwsInvalidKeyExceptionif it doesn't.- Throws:
InvalidKeyException
-
checkSupportedMode
protected abstract void checkSupportedMode(OpenSSLCipher.Mode mode) throws NoSuchAlgorithmExceptionChecks whether the cipher supports this particular ciphermodeand throwsNoSuchAlgorithmExceptionif it doesn't.- Throws:
NoSuchAlgorithmException
-
checkSupportedPadding
protected abstract void checkSupportedPadding(OpenSSLCipher.Padding padding) throws NoSuchPaddingExceptionChecks whether the cipher supports this particular cipherpaddingand throwsNoSuchPaddingExceptionif it doesn't.- Throws:
NoSuchPaddingException
-
getCipherBlockSize
protected abstract int getCipherBlockSize() -
supportsVariableSizeKey
protected boolean supportsVariableSizeKey() -
engineSetMode
Description copied from class:CipherSpiSets the mode for this cipher.- Specified by:
engineSetModein classCipherSpi- Parameters:
modeStr- the name of the cipher mode.- Throws:
NoSuchAlgorithmException- if the specified cipher mode is not supported by this provider.
-
engineSetPadding
Description copied from class:CipherSpiSets the padding method for this cipher.- Specified by:
engineSetPaddingin classCipherSpi- Parameters:
paddingStr- the name of the padding method.- Throws:
NoSuchPaddingException- if the specified padding method is not supported by this cipher.
-
engineGetBlockSize
protected int engineGetBlockSize()Description copied from class:CipherSpiReturns the block size of this cipher (in bytes)- Specified by:
engineGetBlockSizein classCipherSpi- Returns:
- the block size of this cipher, or zero if this cipher is not a block cipher.
-
engineGetOutputSize
protected int engineGetOutputSize(int inputLen)Description copied from class:CipherSpiReturns the size for a buffer (in bytes), that the next call toupdateofdoFinalwould return, taking into account any buffered data from previousupdatecalls and padding.The actual output length of the next call to
updateordoFinalmay be smaller than the length returned by this method.- Specified by:
engineGetOutputSizein classCipherSpi- Parameters:
inputLen- the length of the input (in bytes).- Returns:
- the size for a buffer (in bytes).
-
engineGetIV
protected byte[] engineGetIV()Description copied from class:CipherSpiReturns the Initialization Vector (IV) that was used to initialize this cipher ornullif none was used.- Specified by:
engineGetIVin classCipherSpi- Returns:
- the Initialization Vector (IV), or
nullif none was used.
-
engineGetParameters
Description copied from class:CipherSpiReturns the parameters that where used to create this cipher instance.These may be a the same parameters that were used to create this cipher instance, or may be a combination of default and random parameters, depending on the underlying cipher implementation.
- Specified by:
engineGetParametersin classCipherSpi- Returns:
- the parameters that where used to create this cipher instance, or
nullif this cipher instance does not have any parameters at all.
-
engineInit
Description copied from class:CipherSpiInitializes this cipher instance with the specified key and a source of randomness.The cipher will be initialized for the specified operation (one of: encryption, decryption, key wrapping or key unwrapping) depending on
opmode.If this cipher instance needs any algorithm parameters or random values that the specified key cannot provide, the underlying implementation of this cipher is supposed to generate the required parameters (using its provider or random values). Random values will be generated using
random;When a cipher instance is initialized by a call to any of the
initmethods, the state of the instance is overridden, means it is equivalent to creating a new instance and calling itinitmethod.- Specified by:
engineInitin classCipherSpi- Parameters:
opmode- the operation this cipher instance should be initialized for (one of:ENCRYPT_MODE,DECRYPT_MODE,WRAP_MODEorUNWRAP_MODE).key- the input key for the operation.random- the source of randomness to use.- Throws:
InvalidKeyException- if the specified key cannot be used to initialize this cipher instance.
-
engineInit
protected void engineInit(int opmode, Key key, AlgorithmParameterSpec params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterExceptionDescription copied from class:CipherSpiInitializes this cipher instance with the specified key, algorithm parameters and a source of randomness.The cipher will be initialized for the specified operation (one of: encryption, decryption, key wrapping or key unwrapping) depending on
opmode.If this cipher instance needs any algorithm parameters and
paramsisnull, the underlying implementation of this cipher is supposed to generate the required parameters (using its provider or random values). Random values are generated usingrandom.When a cipher instance is initialized by a call to any of the
initmethods, the state of the instance is overridden, means it is equivalent to creating a new instance and calling itinitmethod.- Specified by:
engineInitin classCipherSpi- Parameters:
opmode- the operation this cipher instance should be initialized for (one of:ENCRYPT_MODE,DECRYPT_MODE,WRAP_MODEorUNWRAP_MODE).key- the input key for the operation.params- the algorithm parameters.random- the source of randomness to use.- Throws:
InvalidKeyException- if the specified key cannot be used to initialize this cipher instance.InvalidAlgorithmParameterException- it the specified parameters are inappropriate for this cipher.
-
engineInit
protected void engineInit(int opmode, Key key, AlgorithmParameters params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterExceptionDescription copied from class:CipherSpiInitializes this cipher instance with the specified key, algorithm parameters and a source of randomness.The cipher will be initialized for the specified operation (one of: encryption, decryption, key wrapping or key unwrapping) depending on
opmode.If this cipher instance needs any algorithm parameters and
paramsisnull, the underlying implementation of this cipher is supposed to generate the required parameters (using its provider or random values). Random values are generated usingrandom.When a cipher instance is initialized by a call to any of the
initmethods, the state of the instance is overridden, means it is equivalent to creating a new instance and calling itinitmethod.- Specified by:
engineInitin classCipherSpi- Parameters:
opmode- the operation this cipher instance should be initialized for (one of:ENCRYPT_MODE,DECRYPT_MODE,WRAP_MODEorUNWRAP_MODE).key- the input key for the operation.params- the algorithm parameters.random- the source of randomness to use.- Throws:
InvalidKeyException- if the specified key cannot be used to initialize this cipher instance.InvalidAlgorithmParameterException- if the specified parameters are inappropriate for this cipher.
-
engineUpdate
protected byte[] engineUpdate(byte[] input, int inputOffset, int inputLen)Description copied from class:CipherSpiContinues a multi-part transformation (encryption or decryption). The transformed bytes are returned.- Specified by:
engineUpdatein classCipherSpi- Parameters:
input- the input bytes to transform.inputOffset- the offset in the input to start.inputLen- the length of the input to transform.- Returns:
- the transformed bytes in a new buffer, or
nullif the input has zero length.
-
engineUpdate
protected int engineUpdate(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) throws ShortBufferExceptionDescription copied from class:CipherSpiContinues a multi-part transformation (encryption or decryption). The transformed bytes are stored in theoutputbuffer.If the size of the
outputbuffer is too small to hold the result, aShortBufferExceptionis thrown. UsegetOutputSizeto check for the size of the output buffer.- Specified by:
engineUpdatein classCipherSpi- Parameters:
input- the input bytes to transform.inputOffset- the offset in the input to start.inputLen- the length of the input to transform.output- the output buffer.outputOffset- the offset in the output buffer.- Returns:
- the number of bytes placed in output.
- Throws:
ShortBufferException- if the size of theoutputbuffer is too small.
-
engineDoFinal
protected byte[] engineDoFinal(byte[] input, int inputOffset, int inputLen) throws IllegalBlockSizeException, BadPaddingExceptionDescription copied from class:CipherSpiFinishes a multi-part transformation (encryption or decryption).Processes the
inputLenbytes ininputbuffer atinputOffset, and any bytes that have been buffered in previousupdatecalls.- Specified by:
engineDoFinalin classCipherSpi- Parameters:
input- the input buffer.inputOffset- the offset in the input buffer.inputLen- the length of the input.- Returns:
- the final bytes from the transformation.
- Throws:
IllegalBlockSizeException- if the size of the resulting bytes is not a multiple of the cipher block size.BadPaddingException- if the padding of the data does not match the padding scheme.
-
engineDoFinal
protected int engineDoFinal(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) throws ShortBufferException, IllegalBlockSizeException, BadPaddingExceptionDescription copied from class:CipherSpiFinishes a multi-part transformation (encryption or decryption).Processes the
inputLenbytes ininputbuffer atinputOffset, and any bytes that have been buffered in previousupdatecalls.- Specified by:
engineDoFinalin classCipherSpi- Parameters:
input- the input buffer.inputOffset- the offset in the input buffer.inputLen- the length of the input.output- the output buffer for the transformed bytes.outputOffset- the offset in the output buffer.- Returns:
- the number of bytes placed in the output buffer.
- Throws:
ShortBufferException- if the size of theoutputbuffer is too small.IllegalBlockSizeException- if the size of the resulting bytes is not a multiple of the cipher block size.BadPaddingException- if the padding of the data does not match the padding scheme.
-
engineWrap
Description copied from class:CipherSpiWraps a key using this cipher instance. This method has been added to this class (for backwards compatibility, it cannot be abstract). If this method is not overridden, it throws anUnsupportedOperationException.- Overrides:
engineWrapin classCipherSpi- Parameters:
key- the key to wrap.- Returns:
- the wrapped key
- Throws:
IllegalBlockSizeException- if the size of the resulting bytes is not a multiple of the cipher block size.InvalidKeyException- if this cipher instance cannot wrap this key.
-
engineUnwrap
protected Key engineUnwrap(byte[] wrappedKey, String wrappedKeyAlgorithm, int wrappedKeyType) throws InvalidKeyException, NoSuchAlgorithmExceptionDescription copied from class:CipherSpiUnwraps a key using this cipher instance.This method has been added to this class (for backwards compatibility, it cannot be abstract). If this method is not overridden, it throws an
UnsupportedOperationException.- Overrides:
engineUnwrapin classCipherSpi- Parameters:
wrappedKey- the wrapped key to unwrap.wrappedKeyAlgorithm- the algorithm for the wrapped key.wrappedKeyType- the type of the wrapped key (one of:SECRET_KEY,PRIVATE_KEYorPUBLIC_KEY)- Returns:
- the unwrapped key.
- Throws:
InvalidKeyException- if thewrappedKeycannot be unwrapped to a key of typewrappedKeyTypefor thewrappedKeyAlgorithm.NoSuchAlgorithmException- if no provider can be found that can create a key of typewrappedKeyTypefor thewrappedKeyAlgorithm.
-