|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object javax.crypto.CipherSpi tribble.crypto.CipherSpi
Cryptographic cipher service provider interface (SPI).
This is the base class for all encryption cipher SPI implementations within
this package. Cipher objects (of type javax.crypto.Cipher) are
constructed from a combination of a cipher SPI object, which is one of generic
cipher algorithm types derived from this class, CipherSpi
, and a
primitive cipher algorithm, which is a class derived from class AbstractCipher
.
Cipher SPI implementations derived from this class are further divided into
two types, BlockCipherSpi
and StreamCipherSpi
. These two base
classes implement the encryption cipher service provider interfaces that serve
as the foundation upon which the rest of this cryptographic package is built.
The MODE_XXX
constants define the various modes
that can be supported by the cryptographic cipher classes in this package.
Likewise, the PADDING_XXX
constants define the
various padding schemes for this package.
Copyright ©2003-2005 by David R. Tribble, all rights reserved.
Permission is granted to freely use and distribute this source code
provided that the original copyright and authorship notices remain
intact.
BlockCipherSpi
,
StreamCipherSpi
,
AbstractCipher
Field Summary | |
static short |
DECRYPT_MODE
Initialization mode: Decryption. |
static short |
ENCRYPT_MODE
Initialization mode: Encryption. |
static java.lang.String |
MODE_BC
Operating mode (block): BC, block chaining. |
static java.lang.String |
MODE_CBC
Operating mode (block): CBC, cipher block chaining. |
static java.lang.String |
MODE_CFB
Operating mode (stream): CFB, cipher feedback. |
static java.lang.String |
MODE_CFB8
Operating mode (stream): CFB-8, cipher feedback. |
static java.lang.String |
MODE_CTR
Operating mode (stream): CTR, counter mode. |
static java.lang.String |
MODE_CTR8
Operating mode (stream): CTR-8, counter mode. |
static java.lang.String |
MODE_ECB
Operating mode (block): ECB, electronic codebook (no chaining). |
static java.lang.String |
MODE_OFB
Operating mode (stream): OFB, output feedback. |
static java.lang.String |
MODE_OFB8
Operating mode (stream): OFB-8, output feedback. |
static java.lang.String |
MODE_PBC
Operating mode (block): PBC, plaintext block chaining. |
static java.lang.String |
MODE_PCBC
Operating mode (block): PCBC, propagating cipher block chaining. |
static java.lang.String |
MODE_PFB
Operating mode (stream): PFB, plaintext feedback. |
static java.lang.String |
MODE_PFB8
Operating mode (stream): PFB-8, plaintext feedback. |
static java.lang.String |
PADDING_CTS
Padding scheme: Ciphertext stealing in the final partial block. |
static java.lang.String |
PADDING_NONE
Padding scheme: None. |
static java.lang.String |
PADDING_PKCS5
Padding scheme: PKCS#5. |
static java.lang.String |
PADDING_ZEROS
Padding scheme: Pad with zero bytes (nulls). |
static int |
SERIES
Class series version. |
Constructor Summary | |
protected |
CipherSpi(java.lang.String alg,
java.lang.String mode,
java.lang.String pad)
Constructor. |
Method Summary | |
protected void |
engineClear()
Clear (wipe) this cipher. |
protected byte[] |
engineDoFinal(byte[] in,
int off,
int len)
Add final input bytes to this cipher. |
protected abstract int |
engineDoFinal(byte[] in,
int inOff,
int len,
byte[] out,
int outOff)
Add final input bytes to this cipher. |
protected int |
engineGetBlockSize()
Retrieve the block size (in bytes) of this cipher. |
protected byte[] |
engineGetIV()
Retrieve the initial vector (IV) used by this cipher. |
protected int |
engineGetKeySize()
Retrieve the key size (in bytes) of this cipher. |
protected int |
engineGetKeySize(java.security.Key key)
Determine the size of a key for this cipher. |
protected abstract int |
engineGetOutputSize(int n)
Determine the output buffer size of this cipher. |
protected abstract java.security.AlgorithmParameters |
engineGetParameters()
Retrieve algorithm-specific initialization parameters for this cipher. |
protected abstract void |
engineInit(int mode,
java.security.Key key,
java.security.spec.AlgorithmParameterSpec parms,
java.security.SecureRandom rand)
Initialize this cipher. |
protected abstract void |
engineInit(int mode,
java.security.Key key,
java.security.AlgorithmParameters parms,
java.security.SecureRandom rand)
Initialize this cipher. |
protected abstract void |
engineInit(int mode,
java.security.Key key,
java.security.SecureRandom rand)
Initialize this cipher. |
protected void |
engineSetIV(byte[] iv)
Set the initial vector (IV) used by this cipher. |
protected abstract void |
engineSetMode(java.lang.String mode)
Set the operating mode of this cipher. |
protected abstract void |
engineSetPadding(java.lang.String padding)
Set the padding scheme of this cipher. |
protected java.security.Key |
engineUnwrap(byte[] wrappedKey,
java.lang.String alg,
int type)
Unwrap a cipher key from a raw encoded form. |
protected byte[] |
engineUpdate(byte[] in,
int off,
int len)
Encrypt/decrypt an array of bytes using this cipher. |
protected abstract int |
engineUpdate(byte[] in,
int inOff,
int len,
byte[] out,
int outOff)
Encrypt/decrypt an array of bytes using this cipher. |
protected byte[] |
engineWrap(java.security.Key key)
Wrap a cipher key into a raw encoded form. |
protected void |
finalize()
Finalization. |
Methods inherited from class java.lang.Object |
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
public static final int SERIES
public static final short ENCRYPT_MODE
public static final short DECRYPT_MODE
public static final java.lang.String MODE_ECB
public static final java.lang.String MODE_CBC
public static final java.lang.String MODE_PBC
public static final java.lang.String MODE_PCBC
public static final java.lang.String MODE_BC
public static final java.lang.String MODE_CFB
public static final java.lang.String MODE_CFB8
public static final java.lang.String MODE_OFB
public static final java.lang.String MODE_OFB8
public static final java.lang.String MODE_PFB
public static final java.lang.String MODE_PFB8
public static final java.lang.String MODE_CTR
public static final java.lang.String MODE_CTR8
public static final java.lang.String PADDING_NONE
public static final java.lang.String PADDING_PKCS5
public static final java.lang.String PADDING_ZEROS
public static final java.lang.String PADDING_CTS
Constructor Detail |
protected CipherSpi(java.lang.String alg, java.lang.String mode, java.lang.String pad) throws java.security.NoSuchAlgorithmException, javax.crypto.NoSuchPaddingException
alg
- Cryptographic cipher algorithm name.mode
- Operating mode, which must match one of the
MODE_XXX
constants.pad
- Padding scheme, which must match one of the
PADDING_XXX
constants}.
java.security.NoSuchAlgorithmException
- Thrown if mode does not specify a supported operating mode.
javax.crypto.NoSuchPaddingException
- Thrown if pad does not specify a supported padding scheme.Method Detail |
protected abstract void engineInit(int mode, java.security.Key key, java.security.SecureRandom rand) throws java.security.InvalidKeyException
mode
- Ciphering mode, which is either ENCRYPT_MODE
or
DECRYPT_MODE
.key
- Key for this cipher to use.rand
- Source of random values, or null if they are to be supplied by a default
source.
java.security.InvalidKeyException
- Thrown if key is not the proper kind of key for use with this
cipher.
java.lang.IllegalArgumentException
- (unchecked)
Thrown if mode is not a valid value.protected abstract void engineInit(int mode, java.security.Key key, java.security.spec.AlgorithmParameterSpec parms, java.security.SecureRandom rand) throws java.security.InvalidKeyException, java.security.InvalidAlgorithmParameterException
mode
- Ciphering mode, which is either ENCRYPT_MODE
or
DECRYPT_MODE
.key
- Key for this cipher to use.parms
- Algorithm-specific initialization parameters (e.g., an IV passed as a
javax.crypto.spec.IvParameterSpec object).rand
- Source of random values, or null if this is to be supplied by a default
source.
java.security.InvalidKeyException
- Thrown if key is not the proper kind of key for use with this
cipher.
java.security.InvalidAlgorithmParameterException
- Thrown if parms is not a proper initialization parameter for this
cipher.protected abstract void engineInit(int mode, java.security.Key key, java.security.AlgorithmParameters parms, java.security.SecureRandom rand) throws java.security.InvalidKeyException, java.security.InvalidAlgorithmParameterException
mode
- Ciphering mode, which is either ENCRYPT_MODE
or
DECRYPT_MODE
.key
- Key for this cipher to use.parms
- Algorithm-specific initialization parameters (e.g., an IV passed as a
javax.crypto.spec.IvParameterSpec object).rand
- Source of random values, or null if this is to be supplied by a default
source.
java.security.InvalidKeyException
- Thrown if key is not the proper kind of key for use with this
cipher.
java.security.InvalidAlgorithmParameterException
- Thrown if parms is not a proper initialization parameter for this
cipher.protected abstract void engineSetMode(java.lang.String mode) throws java.security.NoSuchAlgorithmException
Subclasses must override this method, and may call
engineSetMode()
.
mode
- Cipher operating mode to use
(e.g., "ECB", "CBC", "CFB", "OFB",
"PCBC", etc.).
See the MODE_XXX
constants.
java.security.NoSuchAlgorithmException
- Thrown if mode does not specify an operating mode supported by
this cipher.
java.lang.NullPointerException
- (unchecked)
Thrown if mode is null.protected abstract void engineSetPadding(java.lang.String padding) throws javax.crypto.NoSuchPaddingException
padding
- Cipher padding scheme to use (e.g., "PKCS5Padding").
javax.crypto.NoSuchPaddingException
- Thrown if padding does not specify a padding scheme supported by
this cipher.
java.lang.NullPointerException
- (unchecked)
Thrown if padding is null.protected void engineSetIV(byte[] iv) throws java.security.InvalidAlgorithmParameterException
This method is not part of the standard set defined in class javax.crypto.CipherSpi, but is provided as a convenient enhancement.
iv
- Initial vector bytes to be used by this cipher.
This array must be no longer than the cipher block size
(see engineGetBlockSize()
).
It can be shorter than the cipher block size, in which case the IV of this
cipher will be padded on the right with zeros.
Note that the contents of this array are copied to an internal buffer, so
that subsequent changes made to the array do not affect this cipher
object.
java.security.InvalidAlgorithmParameterException
- Thrown if the length of iv is longer than the cipher block size.
java.lang.NullPointerException
- (unchecked)
Thrown if iv is null.protected int engineGetBlockSize()
protected int engineGetKeySize()
This method is not part of the standard set defined in class javax.crypto.CipherSpi, but is provided as a convenient enhancement.
java.lang.UnsupportedOperationException
- (unchecked)
Thrown if this cipher does not use a fixed key size.
java.lang.IllegalStateException
- (unchecked)
Thrown if this cipher has not been initialized.protected int engineGetKeySize(java.security.Key key) throws java.security.InvalidKeyException
key
- A key suitable for use with this cipher.
java.security.InvalidKeyException
- Thrown if the given key cannot be used with this cipher.
java.lang.IllegalStateException
- (unchecked)
Thrown if this cipher has not been initialized.
java.lang.UnsupportedOperationException
- (unchecked)
Thrown if this method is not overridden, or if this cipher does not use a
fixed key size.protected abstract int engineGetOutputSize(int n)
n
- Size of the input buffer (in addition to any previous pending input
bytes).
java.lang.IllegalStateException
- (unchecked)
Thrown if this cipher has not been initialized.protected byte[] engineGetIV()
java.lang.IllegalStateException
- (unchecked)
Thrown if this cipher has not been initialized.protected abstract java.security.AlgorithmParameters engineGetParameters()
java.lang.IllegalStateException
- (unchecked)
Thrown if this cipher has not been initialized.protected byte[] engineUpdate(byte[] in, int off, int len)
in
- Array of bytes to add as input to this cipher.
The data from this array is appended to any input data previously added to
this cipher.off
- Index of the first byte in array in to encrypt/decrypt.len
- Number of bytes in array in to input to encrypt/decrypt.
java.lang.IllegalArgumentException
- (unchecked)
Thrown if len is negative.
java.lang.IllegalStateException
- (unchecked)
Thrown if this cipher has not been initialized.protected abstract int engineUpdate(byte[] in, int inOff, int len, byte[] out, int outOff) throws javax.crypto.ShortBufferException
in
- Array of bytes to add as input to this cipher.
The data from this array is appended to any input data previously added to
this cipher.inOff
- Index of the first byte of array in to input to this cipher.len
- Number of bytes in array in to encrypt/decrypt.out
- Resulting encrypted/decrypted output bytes.outOff
- Index of the first byte in array out to write the resulting
output bytes to.
javax.crypto.ShortBufferException
- Thrown if the output buffer is too small to hold the resulting data.
java.lang.IllegalStateException
- (unchecked)
Thrown if this cipher has not been initialized.
java.lang.IllegalArgumentException
- (unchecked)
Thrown if len is negative.protected byte[] engineDoFinal(byte[] in, int off, int len) throws javax.crypto.BadPaddingException, javax.crypto.IllegalBlockSizeException
in
- Array of bytes to add as input to this cipher.off
- Index of the first byte in array in to input to this cipher.len
- Number of bytes in array in to input to this cipher.
javax.crypto.BadPaddingException
- Thrown if the final input block does not contain the appropriate padding
(decrypt mode only).
javax.crypto.IllegalBlockSizeException
- Thrown if the total number of input bytes so far is insufficient to
produce a complete output block (encrypt mode only).
java.lang.IllegalArgumentException
- (unchecked)
Thrown if len is negative.
java.lang.IllegalStateException
- (unchecked)
Thrown if this cipher has not been initialized.protected abstract int engineDoFinal(byte[] in, int inOff, int len, byte[] out, int outOff) throws javax.crypto.BadPaddingException, javax.crypto.IllegalBlockSizeException, javax.crypto.ShortBufferException
in
- Array of bytes to add as input to this cipher.inOff
- Index of the first byte in array in to input to this cipher.len
- Number of bytes in array in to input to this cipher.out
- Output array to write the resulting encrypted/decrypted bytes to.outOff
- Index of the first byte in array out to write the resulting
output bytes to.
javax.crypto.BadPaddingException
- Thrown if the final input block does not contain the appropriate padding
(decrypt mode only).
javax.crypto.IllegalBlockSizeException
- Thrown if the total number of input bytes so far is insufficient to
produce a complete output block (encrypt mode only).
javax.crypto.ShortBufferException
- Thrown if the output buffer is too small to hold the resulting data.
java.lang.IllegalArgumentException
- (unchecked)
Thrown if len is negative.
java.lang.IllegalStateException
- (unchecked)
Thrown if this cipher has not been initialized.protected byte[] engineWrap(java.security.Key key) throws javax.crypto.IllegalBlockSizeException, java.security.InvalidKeyException
key
- Key suitable for use with this cipher.
javax.crypto.IllegalBlockSizeException
- Thrown if key is not encoded with a block length supported by
this cipher.
java.security.InvalidKeyException
- Thrown if key is not a valid key for this cipher.
java.lang.UnsupportedOperationException
- (unchecked)
Thrown if this operation is not supported by this cipher.engineUnwrap()
protected java.security.Key engineUnwrap(byte[] wrappedKey, java.lang.String alg, int type) throws java.security.InvalidKeyException, java.security.NoSuchAlgorithmException
java.security.InvalidKeyException
- Thrown if key is not a valid key for this cipher.
java.security.NoSuchAlgorithmException
- Thrown if key is not encoded in a recognizable form.
java.lang.UnsupportedOperationException
- (unchecked)
Thrown if this operation is not supported by this cipher.engineWrap()
protected void engineClear()
This method is not part of the standard set defined in class javax.crypto.CipherSpi, but is provided as a convenient enhancement.
This method should be overridden by classes extending this base class. It should erase sensitive cryptographic information contained within the cipher object, such as key schedules, passwords/passphrases, and leftover plaintext (pre-encrypted or post-decrypted) data blocks. Note that this kind of information must be written over (with zeros, for example) before being garbage collected, otherwise it may remain accessible in local memory for an indeterminate period of time. Some of the cipher information does not need to be wiped since it does not compromise security, such as the IV, mode, and padding settings.
The derived overridding method should wipe its sensitive member variables, and then this method can be called (super.engineClear()) to wipe the remaining base class members.
Note that this cipher object is no longer usable after calling this method.
protected void finalize() throws java.lang.Throwable
java.lang.Throwable
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |