|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--javax.crypto.CipherSpi | +--au.net.aba.crypto.provider.BlockCipher
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.
Field Summary | |
protected static int |
BLOCK_SIZE
|
protected byte[] |
buffer
|
protected int |
bufferPos
|
protected static int |
CBC
|
protected byte[] |
cbcV
|
protected static int |
ECB
|
protected boolean |
firstBlock
|
static java.lang.String |
ident
|
protected byte[] |
ivec
|
protected boolean |
ivEncrypted
|
protected boolean |
ivInline
|
protected java.security.Key |
key
|
protected int |
mode
|
protected boolean |
paddedStream
|
protected java.security.SecureRandom |
random
|
protected int |
streamMode
|
Fields inherited from class javax.crypto.CipherSpi |
ident |
Constructor Summary | |
BlockCipher()
|
Method Summary | |
protected abstract int |
decryptBlock(byte[] in,
int inoff,
int len,
byte[] out,
int outOff)
Decrypt the given block starting at the given offset and place the result in the provided buffer starting at the given offset. |
protected abstract int |
encryptBlock(byte[] in,
int inoff,
int len,
byte[] out,
int outOff)
Encrypt the given block starting at the given offset and place the result in the provided buffer starting at the given offset. |
protected byte[] |
engineDoFinal(byte[] input,
int inputOff,
int inputLen)
Encrypts or decrypts data in a single-part operation, or finishes a multiple-part operation. |
protected int |
engineDoFinal(byte[] input,
int inputOff,
int inputLen,
byte[] output,
int outputOff)
Encrypts or decrypts data in a single-part operation, or finishes a multiple-part operation. |
protected int |
engineGetBlockSize()
Returns the block size (in bytes). |
protected byte[] |
engineGetIV()
Returns the initialisation vector (IV) in a new buffer. |
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). |
protected java.security.AlgorithmParameters |
engineGetParameters()
Returns the parameters used with this cipher. |
protected void |
engineInit(int opmode,
java.security.Key key,
java.security.spec.AlgorithmParameterSpec params,
java.security.SecureRandom random)
Initialises this cipher with a key, a set of algorithm parameters, and a source of randomness. |
protected void |
engineInit(int opmode,
java.security.Key key,
java.security.AlgorithmParameters params,
java.security.SecureRandom random)
Initialises this cipher with a key, a set of algorithm parameters, and a source of randomness. |
protected void |
engineInit(int opmode,
java.security.Key key,
java.security.SecureRandom random)
Initialises this cipher with a key and a source of randomness. |
protected void |
engineSetMode(java.lang.String mode)
Sets the mode of this cipher. |
protected void |
engineSetPadding(java.lang.String padding)
Sets the padding mechanism of this cipher. |
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. |
protected int |
engineUpdate(byte[] input,
int inputOff,
int inputLen,
byte[] output,
int outputOff)
Continues a multiple-part encryption or decryption operation (depending on how this cipher was initialised), processing another data part. |
protected void |
reset()
Reset the Cipher to uninitialised. |
protected abstract void |
setKey(java.security.Key key)
Re-key the cipher. |
Methods inherited from class java.lang.Object |
clone,
equals,
finalize,
getClass,
hashCode,
notify,
notifyAll,
toString,
wait,
wait,
wait |
Field Detail |
public static final java.lang.String ident
protected static final int BLOCK_SIZE
protected static final int ECB
protected static final int CBC
protected byte[] buffer
protected int bufferPos
protected int mode
protected boolean paddedStream
protected int streamMode
protected byte[] ivec
protected byte[] cbcV
protected boolean ivInline
protected boolean ivEncrypted
protected boolean firstBlock
protected java.security.Key key
protected java.security.SecureRandom random
Constructor Detail |
public BlockCipher()
Method Detail |
protected void engineSetMode(java.lang.String mode) throws java.security.NoSuchAlgorithmException
mode
- the cipher modeprotected void engineSetPadding(java.lang.String padding) throws NoSuchPaddingException
padding
- the padding mechanismprotected int engineGetBlockSize()
protected int engineGetOutputSize(int inputLen)
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.
inputLen
- the input length (in bytes)protected byte[] engineGetIV()
This is useful in the context of password-based encryption or decryption, where the IV is derived from a user-provided passphrase.
protected java.security.AlgorithmParameters engineGetParameters()
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).
protected void engineInit(int opmode, java.security.Key key, java.security.SecureRandom random) throws java.security.InvalidKeyException
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
opmode
- the operation mode of this cipher (this is either
ENCRYPT_MODE or DECRYPT_MODE)key
- the encryption keyrandom
- the source of randomnessprotected void engineInit(int opmode, java.security.Key key, java.security.AlgorithmParameters params, java.security.SecureRandom random) throws java.security.InvalidKeyException, java.security.InvalidAlgorithmParameterException
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.
opmode
- the operation mode of this cipher (this is either
ENCRYPT_MODE or DECRYPT_MODE)key
- the encryption keyparams
- the algorithm parametersrandom
- the source of randomnessprotected void engineInit(int opmode, java.security.Key key, java.security.spec.AlgorithmParameterSpec params, java.security.SecureRandom random) throws java.security.InvalidKeyException, java.security.InvalidAlgorithmParameterException
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.
opmode
- the operation mode of this cipher (this is either
ENCRYPT_MODE or DECRYPT_MODE)key
- the encryption keyparams
- the algorithm parametersrandom
- the source of randomnessprotected byte[] engineUpdate(byte[] input, int inputOff, int inputLen)
The first inputLen bytes in the input buffer, starting at inputOffset, are processed, and the result is stored in a new buffer.
input
- the input bufferinputOffset
- the offset in input where the input startsinputLen
- the input lengthprotected int engineUpdate(byte[] input, int inputOff, int inputLen, byte[] output, int outputOff) throws ShortBufferException
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.
input
- the input bufferinputOffset
- the offset in input where the input startsinputLen
- the input lengthoutput
- the buffer for the resultoutputOffset
- the offset in output where the result is storedprotected byte[] engineDoFinal(byte[] input, int inputOff, int inputLen) throws IllegalBlockSizeException, BadPaddingException
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.
input
- the input bufferinputOff
- the offset in input where the input startsinputLen
- the input lengthprotected int engineDoFinal(byte[] input, int inputOff, int inputLen, byte[] output, int outputOff) throws ShortBufferException, IllegalBlockSizeException, BadPaddingException
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.
input
- the input bufferinputOffset
- - the offset in input where the input startsinputLen
- - the input lengthoutput
- - the buffer for the resultoutputOffset
- - the offset in output where the result is storedprotected void reset()
protected abstract void setKey(java.security.Key key) throws java.security.InvalidKeyException
protected abstract int encryptBlock(byte[] in, int inoff, int len, byte[] out, int outOff) throws IllegalBlockSizeException
protected abstract int decryptBlock(byte[] in, int inoff, int len, byte[] out, int outOff) throws BadPaddingException
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |