All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class au.net.aba.crypto.provider.BlockCipher

java.lang.Object
   |
   +----javax.crypto.CipherSpi
           |
           +----au.net.aba.crypto.provider.BlockCipher

public abstract class BlockCipher
extends CipherSpi
This abstract class is the base class for all Block Ciphers. It will perform the necessary block buffering and then pass the data on through the protected interface.

This class supports PKCS#5 and NoPadding, as well as supporting ECB and CBC modes. The standard block size is 8.

To implement a new block cipher it is necessary to implement the three abstract methods that re-key the cipher, do encryption and decryption using the current. It may also be necessary to override the engineSetMode method if CBC or ECB are not supported. If different padding mechanisms are used engineSetPadding may be overridden (in which case it may also be necessary to override engineGetOutputSize). For block sizes other than 8 engineGetBlockSize should be overridden. Finally if the algorithm supports AlgorithmParameterSpec just override the engineInit method that accepts those parameters.


Variable Index

 o BLOCK_SIZE
 o buffer
 o bufferPos
 o CBC
 o cbcV
 o ECB
 o firstBlock
 o ident
 o ivec
 o ivEncrypted
 o ivInline
 o key
 o mode
 o paddedStream
 o random
 o streamMode

Constructor Index

 o BlockCipher()

Method Index

 o decryptBlock(byte[], int, int, byte[], int)
Decrypt the given block starting at the given offset and place the result in the provided buffer starting at the given offset.
 o encryptBlock(byte[], int, int, byte[], int)
Encrypt the given block starting at the given offset and place the result in the provided buffer starting at the given offset.
 o engineDoFinal(byte[], int, int)
Encrypts or decrypts data in a single-part operation, or finishes a multiple-part operation.
 o engineDoFinal(byte[], int, int, byte[], int)
Encrypts or decrypts data in a single-part operation, or finishes a multiple-part operation.
 o engineGetBlockSize()
Returns the block size (in bytes).
 o engineGetIV()
Returns the initialisation vector (IV) in a new buffer.
 o engineGetOutputSize(int)
Returns the length in bytes that an output buffer would need to be in order to hold the result of the next update or doFinal operation, given the input length inputLen (in bytes).
 o engineGetParameters()
Returns the parameters used with this cipher.
 o engineInit(int, Key, AlgorithmParameters, SecureRandom)
Initialises this cipher with a key, a set of algorithm parameters, and a source of randomness.
 o engineInit(int, Key, AlgorithmParameterSpec, SecureRandom)
Initialises this cipher with a key, a set of algorithm parameters, and a source of randomness.
 o engineInit(int, Key, SecureRandom)
Initialises this cipher with a key and a source of randomness.
 o engineSetMode(String)
Sets the mode of this cipher.
 o engineSetPadding(String)
Sets the padding mechanism of this cipher.
 o engineUpdate(byte[], int, int)
Continues a multiple-part encryption or decryption operation (depending on how this cipher was initialised), processing another data part.
 o engineUpdate(byte[], int, int, byte[], int)
Continues a multiple-part encryption or decryption operation (depending on how this cipher was initialised), processing another data part.
 o reset()
Reset the Cipher to uninitialised.
 o setKey(Key)
Re-key the cipher.

Variables

 o ident
 public static final String ident
 o BLOCK_SIZE
 protected static final int BLOCK_SIZE
 o ECB
 protected static final int ECB
 o CBC
 protected static final int CBC
 o buffer
 protected byte buffer[]
 o bufferPos
 protected int bufferPos
 o mode
 protected int mode
 o paddedStream
 protected boolean paddedStream
 o streamMode
 protected int streamMode
 o ivec
 protected byte ivec[]
 o cbcV
 protected byte cbcV[]
 o ivInline
 protected boolean ivInline
 o ivEncrypted
 protected boolean ivEncrypted
 o firstBlock
 protected boolean firstBlock
 o key
 protected Key key
 o random
 protected SecureRandom random

Constructors

 o BlockCipher
 public BlockCipher()

Methods

 o engineSetMode
 protected void engineSetMode(String mode) throws NoSuchAlgorithmException
Sets the mode of this cipher. Valid modes for are; ECB or CBC.

Parameters:
mode - the cipher mode
Throws: NoSuchAlgorithmException
if the requested cipher mode does not exist
Overrides:
engineSetMode in class CipherSpi
 o engineSetPadding
 protected void engineSetPadding(String padding) throws NoSuchPaddingException
Sets the padding mechanism of this cipher. Valid padding mechanisms are; "PKCS5Padding" and "NoPadding".

Parameters:
padding - the padding mechanism
Throws: NoSuchPaddingException
if the requested padding mechanism does not exist.
Overrides:
engineSetPadding in class CipherSpi
 o engineGetBlockSize
 protected int engineGetBlockSize()
Returns the block size (in bytes). In this case 8. This method should be overridden if the given algorithm has a different block size.

Returns:
the block size (in bytes), or 0 if the underlying algorithm is not a block cipher
Overrides:
engineGetBlockSize in class CipherSpi
 o engineGetOutputSize
 protected int engineGetOutputSize(int inputLen)
Returns the length in bytes that an output buffer would need to be in order to hold the result of the next update or doFinal operation, given the input length inputLen (in bytes).

This call takes into account any unprocessed (buffered) data from a previous update call, and padding.

The actual output length of the next update or doFinal call may be smaller than the length returned by this method.

Parameters:
inputLen - the input length (in bytes)
Returns:
the required output buffer size (in bytes)
Overrides:
engineGetOutputSize in class CipherSpi
 o engineGetIV
 protected byte[] engineGetIV()
Returns the initialisation vector (IV) in a new buffer.

This is useful in the context of password-based encryption or decryption, where the IV is derived from a user-provided passphrase.

Returns:
the initialisation vector in a new buffer, or null if the underlying algorithm does not use an IV, or if the IV has not yet been set.
Overrides:
engineGetIV in class CipherSpi
 o engineGetParameters
 protected AlgorithmParameters engineGetParameters()
Returns the parameters used with this cipher.

The returned parameters may be the same that were used to initialise this cipher, or may contain the default set of parameters or a set of randomly generated parameters used by the underlying cipher implementation (provided that the underlying cipher implementation uses a default set of parameters or creates new parameters if it needs parameters but was not initialised with any).

Returns:
the parameters used with this cipher, or null if this cipher does not use any parameters.
Overrides:
engineGetParameters in class CipherSpi
 o engineInit
 protected void engineInit(int opmode,
                           Key key,
                           SecureRandom random) throws InvalidKeyException
Initialises this cipher with a key and a source of randomness.

The cipher is initialised for encryption or decryption, depending on the value of opmode.

If this cipher requires any algorithm parameters that cannot be derived from the given key, the underlying cipher implementation is supposed to generate the required parameters itself (using provider-specific default or random values) if it is being initialised for encryption, and raise an InvalidKeyException if it is being initialised for decryption. The generated parameters can be retrieved using engineGetParameters or engineGetIV (if the parameter is an IV).

If this cipher (including its underlying feedback or padding scheme) requires any random bytes (e.g., for parameter generation), it will get them from random.

Note that when a Cipher object is initialised, it loses all previously-acquired state. In other words, initialising a Cipher is equivalent to creating a new instance of that Cipher and initialising it

Parameters:
opmode - the operation mode of this cipher (this is either ENCRYPT_MODE or DECRYPT_MODE)
key - the encryption key
random - the source of randomness
Throws: InvalidKeyException
if the given key is inappropriate for initialising this cipher
Overrides:
engineInit in class CipherSpi
 o engineInit
 protected void engineInit(int opmode,
                           Key key,
                           AlgorithmParameters params,
                           SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException
Initialises this cipher with a key, a set of algorithm parameters, and a source of randomness.

The cipher is initialised for encryption or decryption, depending on the value of opmode.

If this cipher requires any algorithm parameters and params is null, the underlying cipher implementation is supposed to generate the required parameters itself (using provider-specific default or random values) if it is being initialised for encryption, and raise an InvalidAlgorithmParameterException if it is being initialised for decryption. The generated parameters can be retrieved using engineGetParameters or engineGetIV (if the parameter is an IV).

If this cipher (including its underlying feedback or padding scheme) requires any random bytes (e.g., for parameter generation), it will get them from random.

Note that when a Cipher object is initialised, it loses all previously-acquired state. In other words, initialising a Cipher is equivalent to creating a new instance of that Cipher and initialising it.

Parameters:
opmode - the operation mode of this cipher (this is either ENCRYPT_MODE or DECRYPT_MODE)
key - the encryption key
params - the algorithm parameters
random - the source of randomness
Throws: InvalidKeyException
if the given key is inappropriate for initialising this cipher
Throws: InvalidAlgorithmParameterException
if the given algorithm parameters are inappropriate for this cipher, or if this cipher is being initialised fro decryption and requires algorithm parameters and params is null
Overrides:
engineInit in class CipherSpi
 o engineInit
 protected void engineInit(int opmode,
                           Key key,
                           AlgorithmParameterSpec params,
                           SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException
Initialises this cipher with a key, a set of algorithm parameters, and a source of randomness.

The cipher is initialised for encryption or decryption, depending on the value of opmode.

If this cipher requires any algorithm parameters and params is null, the underlying cipher implementation is supposed to generate the required parameters itself (using provider-specific default or random values) if it is being initialised for encryption, and raise an InvalidAlgorithmParameterException if it is being initialised for decryption. The generated parameters can be retrieved using engineGetParameters or engineGetIV (if the parameter is an IV).

If this cipher (including its underlying feedback or padding scheme) requires any random bytes (e.g., for parameter generation), it will get them from random.

Note that when a Cipher object is initialised, it loses all previously-acquired state. In other words, initialising a Cipher is equivalent to creating a new instance of that Cipher and initialising it.

Parameters:
opmode - the operation mode of this cipher (this is either ENCRYPT_MODE or DECRYPT_MODE)
key - the encryption key
params - the algorithm parameters
random - the source of randomness
Throws: InvalidKeyException
if the given key is inappropriate for initialising this cipher
Throws: InvalidAlgorithmParameterException
if the given algorithm parameters are inappropriate for this cipher, or if this cipher is being initialised fro decryption and requires algorithm parameters and params is null
Overrides:
engineInit in class CipherSpi
 o engineUpdate
 protected byte[] engineUpdate(byte input[],
                               int inputOff,
                               int inputLen)
Continues a multiple-part encryption or decryption operation (depending on how this cipher was initialised), processing another data part.

The first inputLen bytes in the input buffer, starting at inputOffset, are processed, and the result is stored in a new buffer.

Parameters:
input - the input buffer
inputOffset - the offset in input where the input starts
inputLen - the input length
Returns:
the new buffer with the result, or null if the underlying cipher is a block cipher and the input data is too short to result in a new block.
Overrides:
engineUpdate in class CipherSpi
 o engineUpdate
 protected int engineUpdate(byte input[],
                            int inputOff,
                            int inputLen,
                            byte output[],
                            int outputOff) throws ShortBufferException
Continues a multiple-part encryption or decryption operation (depending on how this cipher was initialised), processing another data part.

The first inputLen bytes in the input buffer, starting at inputOffset, are processed, and the result is stored in the output buffer, starting at outputOffset.

If the output buffer is too small to hold the result, a ShortBufferException is thrown. In this case, repeat this call with a larger output buffer. Use getOutputSize to determine how big the output buffer should be.

Parameters:
input - the input buffer
inputOffset - the offset in input where the input starts
inputLen - the input length
output - the buffer for the result
outputOffset - the offset in output where the result is stored
Returns:
the number of bytes stored in output
Throws: ShortBufferException
if the given output buffer is too small to hold the result
Overrides:
engineUpdate in class CipherSpi
 o engineDoFinal
 protected byte[] engineDoFinal(byte input[],
                                int inputOff,
                                int inputLen) throws IllegalBlockSizeException, BadPaddingException
Encrypts or decrypts data in a single-part operation, or finishes a multiple-part operation. The data is encrypted or decrypted, depending on how this cipher was initialised.

The first inputLen bytes in the input buffer, starting at inputOffset, and any input bytes that may have been buffered during a previous update operation, are processed, with padding (if requested) being applied. The result is stored in a new buffer.

The cipher is reset to its initial state (uninitialised) after this call.

Parameters:
input - the input buffer
inputOff - the offset in input where the input starts
inputLen - the input length
Returns:
the new buffer with the result
Throws: 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
Throws: 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 bytes.
Overrides:
engineDoFinal in class CipherSpi
 o engineDoFinal
 protected int engineDoFinal(byte input[],
                             int inputOff,
                             int inputLen,
                             byte output[],
                             int outputOff) throws ShortBufferException, IllegalBlockSizeException, BadPaddingException
Encrypts or decrypts data in a single-part operation, or finishes a multiple-part operation. The data is encrypted or decrypted, depending on how this cipher was initialised.

The first inputLen bytes in the input buffer, starting at inputOffset, and any input bytes that may have been buffered during a previous update operation, are processed, with padding (if requested) being applied. The result is stored in the output buffer, starting at outputOffset.

If the output buffer is too small to hold the result, a ShortBufferException is thrown. In this case, repeat this call with a larger output buffer. Use getOutputSize to determine how big the output buffer should be.

Parameters:
input - the input buffer
inputOffset - - the offset in input where the input starts
inputLen - - the input length
output - - the buffer for the result
outputOffset - - the offset in output where the result is stored
Returns:
s the number of bytes stored in output
Throws: 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
Throws: ShortBufferException
if the given output buffer is too small to hold the result
Throws: 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 bytes
Overrides:
engineDoFinal in class CipherSpi
 o reset
 protected void reset()
Reset the Cipher to uninitialised.

 o setKey
 protected abstract void setKey(Key key) throws InvalidKeyException
Re-key the cipher. If the provided Key is not compatible with this cipher the exception should throw an InvalidKeyException.

 o encryptBlock
 protected abstract int encryptBlock(byte in[],
                                     int inoff,
                                     int len,
                                     byte out[],
                                     int outOff) throws IllegalBlockSizeException
Encrypt the given block starting at the given offset and place the result in the provided buffer starting at the given offset. The input will be an exact multiple of our blocksize.

 o decryptBlock
 protected abstract int decryptBlock(byte in[],
                                     int inoff,
                                     int len,
                                     byte out[],
                                     int outOff) throws BadPaddingException
Decrypt the given block starting at the given offset and place the result in the provided buffer starting at the given offset. The input will be an exact multiple of our blocksize.


All Packages  Class Hierarchy  This Package  Previous  Next  Index