|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--javax.crypto.Cipher
Engine class for algorithm independent Cipher object creation.
Attention: This is not a SUN implementation!
This class has been developed by IAIK according to the documentation publically available.
For SUNīs documentation of this class see
http://java.sun.com/security/JCE1.2/spec/apidoc/index.html
This class represents the super API class for building cipher objects (e.g. DES, IDEA) for data encryption
and data decryption.
For creating a Cipher object use the getInstance
method supplied
with the particular cryptographic algorithm in mind. Additionally you explicitly may
specify a feedback mode and padding scheme to be used and a particular provider.
e.g: Cipher des_cipher = Cipher.getInstance("DES"/ECB/PKCS5Padding");
The string "algorithm/operation mode/padding scheme"
is called transformation.
At least you have to specifiy the algorithm name; the other parameters may be set by agreeing with
default settings.
Generally, a cipher object may be used for either encrypting or decrypting. It therefore
has to be properly initialized by using one of the corresponding init
methods,
at least supplied with operation mode (ENCRYPT_MODE or DECRYPT_MODE) and key:
e.g.: des_cipher.init(Cipher.ENCRYPT_MODE, des_key);
The following cipher modes are supported by the IAIK
} provider:
Since ECB encrypts each single block independently, it enables random access to encrypted data blocks which may be preferable for database encrypting. Often ECB is used for key-encrypting.
CBC is effective against block replay and makes it harder to try a codebook attack; presuming that using different initialization vectors for every message to be encrypted.
If in CBC some error occurs when transmitting a ciphertext block, this and the immediately following block cannot be decrypted correctly. All subsequent blocks will not be affected by the error. This kind of error propagation may be utilized by an eavesdropper to distort some single ciphertext block for calculated impairing the subsequently decrypted plaintext block. For that reason, the receiver should never trust a block following a faulty block.
CBC often is used for encrypting files.
The CFB mode uses the underlying block cipher for generating a key stream. Initially, an input register of given length (which is allowed to be smaller than the block size of the the underlying block cipher) is filled with an initilaization vector that has to be changed with every new message to be encrypted. After encrypting the contents of the input register it is XORcombined with the corresponding number of plaintext characters, giving the first ciphertext sequence. Additionally the result of this XORcombination is fed back into the input register to contribute to the generation of the next sequence of the key stream. On the decryption side the block cipher also is used in encryption mode. The initiailization vector for initially filling the register is the same. Every incoming cipher text sequence is fed into the register, encrypted and subsequently XORcombined with the corresponding number of ciphertext characters for forming the corresponding plaintext sequence.
In contrast to OFB, there is an error propagation when using CFB. A single-bit ciphertext error causes a single-bit error in the plaintext and distorts ciphertext as long as the error passes through the block cipherīs input register. All subsequent ciphertext will be decrypted correctly. An eavesdropper may change single plaintext characters for avtive impairing a ciphertext character leading to the desired plaintext character after decrypting. Since such an attack will strongly distort the subsequently decrypted text, it should be detected, as long as the modified bit is not the last bit of the message.
CFB is self-recovering and self-synchronizing.
As in common with CFB, OFB uses the underlying block cipher for generating a key stream. Unlike CFB, in OFB the plaintext is not involved into the key stream generation process: The encrypted input register (initially filled with an initialization vector) output is fed back into the register before being XORed with plaintext characters to form the actual ciphertext sequence.
On decryption side, the key stream also is generated independently from the received ciphertext. Again, the underlying block cipher only is used in encryption mode on both encryption and decryption side.
There is no error propagation in OFB making it possible for some eavesdropper to calculated modify the decrypted plaintext by correspondingly changing the ciphertext. It is important when using OFB to ensure that encryption and decryption side work fully syncronized. If some synchronization error occurs, the received text cannot be decrypted correctly. For that reason, take care for particular synchronization mechanism when using OFB!
Counter-mode encryption ("CTR mode") was introduced by Diffie and Hellman. CTR mode has significant efficiency advantages over the standard encryption modes without weakening the security. In particular its tight security has been proven. Most of the perceived disadvantages of CTR mode are not valid criticisms, but rather caused by the lack of knowledge.
IAIK
provider:
KeyGenerator
,
SecretKey
,
AlgorithmParameters
,
AlgorithmParameterSpec
Field Summary | |
static int |
DECRYPT_MODE
Cipher decrypt mode. |
static int |
ENCRYPT_MODE
Cipher encrypt mode. |
static int |
PRIVATE_KEY
Type identifying a private key. |
static int |
PUBLIC_KEY
Type identifying a public key. |
static int |
SECRET_KEY
Type identifying a secret key. |
static int |
UNWRAP_MODE
Mode for key unwrapping (JCE 1.2.1) |
static int |
WRAP_MODE
Mode for key wrapping (JCE 1.2.1) |
Constructor Summary | |
protected |
Cipher(CipherSpi cipherSpi,
Provider provider,
String transformation)
Creates a Cipher object. |
Method Summary | |
byte[] |
doFinal()
Returns the result of the last step of a multi-step en/decryption operation by processing any remaining buffered data. |
byte[] |
doFinal(byte[] input)
Returns the result of the last step of a multi-step en/decryption operation or the result of a single-step en/decryption operation by processing the given input data and any remaining buffered data. |
int |
doFinal(byte[] output,
int outputOffset)
Performs the last step of a multi-step en/decryption operation by processing any remaining buffered data. |
byte[] |
doFinal(byte[] input,
int inputOffset,
int inputLen)
Returns the result of the last step of a multi-step en/decryption operation or the result of a single-step en/decryption operation by processing the given input data and any remaining buffered data. |
int |
doFinal(byte[] input,
int inputOffset,
int inputLen,
byte[] output)
Performs the last step of a multi-step en/decryption operation or a single-step en/decryption operation by processing the given input data and any remaining buffered data. |
int |
doFinal(byte[] input,
int inputOffset,
int inputLen,
byte[] output,
int outputOffset)
Performs the last step of a multi-step en/decryption operation or a single-step en/decryption operation by processing the given input data and any remaining buffered data. |
String |
getAlgorithm()
Returns the algorithm name used when creating this object. |
int |
getBlockSize()
Returns the block size corresponding to this cipher. |
ExemptionMechanism |
getExemptionMechanism()
Return the exemption mechanism for this cipher (JCE 1.2.1). |
static Cipher |
getInstance(String algorithm)
Returns the default provider Cipher object implementation of the specified algorithm. |
static Cipher |
getInstance(String algorithm,
String provider)
Returns a Cipher object for the specified algorithm, implemented by the given provider. |
byte[] |
getIV()
Returns a byte array containing the initialization vector (IV). |
int |
getOutputSize(int inputLen)
Returns the output buffer size necessary for capturing the data resulting from the next update or doFinal operation including
any data currently being buffered. |
AlgorithmParameters |
getParameters()
Returns the parameters used with this cipher. |
Provider |
getProvider()
Returns the provider used for creating this Cipher object. |
void |
init(int opmode,
Certificate certificate)
Initialize this cipher. |
void |
init(int opmode,
Certificate certificate,
SecureRandom random)
Initialize this cipher. |
void |
init(int opmode,
Key key)
Initializes this cipher object with a proper key. |
void |
init(int opmode,
Key key,
AlgorithmParameters params)
Initializes this cipher with a key and a set of algorithm parameters. |
void |
init(int opmode,
Key key,
AlgorithmParameterSpec params)
Initializes this cipher object with proper key and algorithm parameters. |
void |
init(int opmode,
Key key,
AlgorithmParameterSpec params,
SecureRandom random)
Initializes this cipher object with proper key and algorithm parameters, and some random seed. |
void |
init(int opmode,
Key key,
AlgorithmParameters params,
SecureRandom random)
Initializes this cipher with a key, a set of algorithm parameters, and a source of randomness. |
void |
init(int opmode,
Key key,
SecureRandom random)
Initializes this cipher object with a proper key and some random seed. |
String |
toString()
Returns a string representation of this Cipher. |
Key |
unwrap(byte[] wrappedKey,
String wrappedKeyAlgorithm,
int wrappedKeyType)
Unwrap a wrapped key. |
byte[] |
update(byte[] input)
Returns the result of the next step of a multi-step en/decryption operation. |
byte[] |
update(byte[] input,
int inputOffset,
int inputLen)
Returns the result of the next step of a multi-step en/decryption operation. |
int |
update(byte[] input,
int inputOffset,
int inputLen,
byte[] output)
Performs the next step of a multi-step en/decryption operation. |
int |
update(byte[] input,
int inputOffset,
int inputLen,
byte[] output,
int outputOffset)
Performs the next step of a multi-step en/decryption operation. |
byte[] |
wrap(Key key)
Wrap a key. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
public static final int ENCRYPT_MODE
public static final int DECRYPT_MODE
public static final int WRAP_MODE
public static final int UNWRAP_MODE
public static final int PUBLIC_KEY
public static final int PRIVATE_KEY
public static final int SECRET_KEY
Constructor Detail |
protected Cipher(CipherSpi cipherSpi, Provider provider, String transformation)
getInstance
factory methods for obtaining a Cipher object.cipherSpi
- the SPI Cipher implementation of the given providerprovider
- the providertransformation
- the transformationMethod Detail |
public static Cipher getInstance(String algorithm) throws NoSuchAlgorithmException, NoSuchPaddingException
algorithm
- the algorithm as string transformation
e.g., DES/CBC/PKCS5PaddingNoSuchAlgorithmException
- if this algorithm is not implementedNoSuchPaddingException
- if the specified padding is not supportedpublic static Cipher getInstance(String algorithm, String provider) throws NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException
algorithm
- the algorithm as string transformation
e.g., DES/CBC/PKCS5PaddingproviderName
- the name of the provider implementing this algorithm.NoSuchAlgorithmException
- if this algorithm is not implemented by this providerNoSuchProviderException
- if this provider cannot be fetchedNoSuchPaddingException
- if the specified padding is not supportedpublic final Provider getProvider()
public final String getAlgorithm()
public final int getBlockSize()
public final int getOutputSize(int inputLen) throws IllegalStateException
update
or doFinal
operation including
any data currently being buffered.inputLen
- the number of bytes to processIllegalStateException
- if this cipher is in a wrong state (e.g., has not yet been initialized)public final byte[] getIV()
null
is returned.null
otherwise.public final AlgorithmParameters getParameters()
public final void init(int opmode, Key key) throws InvalidKeyException
opmode
parameter (either ENCRYPT_MODE or DECCRYPT_MODE).
e.g. cipher_obj.init(Cipher.ENCRYPT_MODE, key);
opmode
- the operation mode for which this cipher is used (ENCRYPT_MODE or DECRYPT_MODE)key
- the keyInvalidKeyException
- if the given key cannot be used for initializing this cipherpublic final void init(int opmode, Key key, SecureRandom random) throws InvalidKeyException
opmode
parameter (either ENCRYPT_MODE or DECCRYPT_MODE).
e.g. cipher_obj.init(Cipher.ENCRYPT_MODE, key, random_seed);
If this cipher (including its underlying feedback or padding scheme) requires any random bytes, it will get them from random.
opmode
- the operation mode for which this cipher is used (ENCRYPT_MODE or DECRYPT_MODE)key
- the keyrandom
- the random seedInvalidKeyException
- if the given key cannot be used for initializing this cipherpublic final void init(int opmode, Key key, AlgorithmParameterSpec params) throws InvalidKeyException, InvalidAlgorithmParameterException
opmode
parameter (either ENCRYPT_MODE or DECCRYPT_MODE).
e.g. cipher_obj.init(Cipher.ENCRYPT_MODE, key, alg_params);
opmode
- the operation mode for which this cipher is used (ENCRYPT_MODE or DECRYPT_MODE)key
- the keyparams
- the algorithm parametersInvalidKeyException
- if the given key cannot be used for initializing this cipherInvalidAlgorithmParameterException
- if the given algorithm parameters do not match to this cipherpublic final void init(int opmode, Key key, AlgorithmParameterSpec params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException
opmode
parameter (either ENCRYPT_MODE or DECCRYPT_MODE).
e.g. cipher_obj.init(Cipher.ENCRYPT_MODE, key, alg_params, random_seed);
opmode
- the operation mode for which this cipher is used (ENCRYPT_MODE or DECRYPT_MODE)key
- the keyparams
- the algorithm parametersrandom
- the random seedInvalidKeyException
- if the given key cannot be used for initializing this cipherInvalidAlgorithmParameterException
- if the given algorithm parameters donīt match to this cipherpublic final void init(int opmode, Key key, AlgorithmParameters params) throws InvalidKeyException, InvalidAlgorithmParameterException
opmode
- the operation mode of this cipher
(this is either ENCRYPT_MODE or DECRYPT_MODE)key
- the encryption keyparams
- the algorithm parametersInvalidKeyException
- if the given key is inappropriate for initializing thiscipherInvalidAlgorithmParameterException
- if the given algorithm parameters are inappropriate for this cipherpublic final void init(int opmode, Key key, AlgorithmParameters params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException
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 randomnessInvalidKeyException
- if the given key is inappropriate for initializing thiscipherInvalidAlgorithmParameterException
- if the given algorithm parameters are inappropriate for this cipherpublic final void init(int opmode, Certificate certificate) throws InvalidKeyException
public final void init(int opmode, Certificate certificate, SecureRandom random) throws InvalidKeyException
public final byte[] update(byte[] input) throws IllegalStateException
input
- the byte array holding the data to be processedIllegalStateException
- if this cipher is in a wrong state (e.g., has not yet been initialized)public final byte[] update(byte[] input, int inputOffset, int inputLen) throws IllegalStateException
inputOffset
,
only the first inputLen
bytes are en/decrypted. The result is returned as a byte array.input
- the byte array holding the data to be processedinputOffset
- the offset indicating the start position within the input byte arrayinputLen
- the number of bytes to be processedIllegalStateException
- if this cipher is in a wrong state (e.g., has not yet been initialized)public final int update(byte[] input, int inputOffset, int inputLen, byte[] output) throws IllegalStateException, ShortBufferException
inputOffset
,
only the first inputLen
bytes are en/decrypted. The result is stored in the given
output byte array. The number of bytes stored in this output byte array are returned.input
- the byte array holding the data to be processedinputOffset
- the offset indicating the start position within the input byte arrayinputLen
- the number of bytes to be processedoutput
- the byte array for holding the resultIllegalStateException
- if this cipher is in a wrong state (e.g., has not yet been initialized)ShortBufferException
- if the given output buffer is too small for holding the resultpublic final int update(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) throws IllegalStateException, ShortBufferException
inputOffset
,
only the first inputLen
bytes are en/decrypted. The result is stored in the given
output byte array, beginning at outputOffset
. The number of bytes stored in this
output byte array are returned.input
- the byte array holding the data to be processedinputOffset
- the offset indicating the start position within the input byte arrayinputLen
- the number of bytes to be processedoutput
- the byte array for holding the resultoutputOffset
- the offset indicating the start position within the output byte array
to which the en/decrypted data is writtenIllegalStateException
- if this cipher is in a wrong state (e.g., has not yet been initialized)ShortBufferException
- if the given output buffer is too small for holding the resultpublic final byte[] doFinal() throws IllegalStateException, IllegalBlockSizeException, BadPaddingException
IllegalStateException
- if this cipher is in a wrong state (e.g., has not yet been initialized)IllegalBlockSizeException
- if the total length of the processed data is not a multiple of the block size for a
(no padding performing) block cipherBadPaddingException
- if the decrypted data is not bounded by the proper padding bytes after data
decryption including (un)paddingpublic final int doFinal(byte[] output, int outputOffset) throws IllegalStateException, IllegalBlockSizeException, ShortBufferException, BadPaddingException
outputOffset
.
The number of bytes stored in the output byte array are returned.output
- the byte array for holding the resultoutputOffset
- the offset indicating the start position within the output byte array
to which the en/decrypted data is writtenIllegalStateException
- if this cipher is in a wrong state (e.g., has not yet been initialized)IllegalBlockSizeException
- if the total length of the processed data is not a multiple of the block size for a
(no padding performing) block cipherShortBufferException
- if the given output buffer is too small for holding the resultBadPaddingException
- if the decrypted data is not bounded by the proper padding bytes after data
decryption including (un)paddingpublic final byte[] doFinal(byte[] input) throws IllegalStateException, IllegalBlockSizeException, BadPaddingException
input
- the byte array holding the data to be processedIllegalStateException
- if this cipher is in a wrong state (e.g., has not yet been initialized)IllegalBlockSizeException
- if the total length of the processed data is not a multiple of the block size for a
(no padding performing) block cipherBadPaddingException
- if the decrypted data is not bounded by the proper padding bytes after data
decryption including (un)paddingpublic final byte[] doFinal(byte[] input, int inputOffset, int inputLen) throws IllegalStateException, IllegalBlockSizeException, BadPaddingException
inputOffset
,
only the first inputLen
bytes are en/decrypted, including any buffered bytes of a previous
update
operation. If necessary, padding is performed. The result is returned as
a output byte array.input
- the byte array holding the data to be processedinputOffset
- the offset indicating the start position within the input byte arrayinputLen
- the number of bytes to be processedIllegalStateException
- if this cipher is in a wrong state (e.g., has not yet been initialized)IllegalBlockSizeException
- if the total length of the processed data is not a multiple of the block size for a
(no padding performing) block cipherBadPaddingException
- if the decrypted data is not bounded by the proper padding bytes after data
decryption including (un)paddingpublic final int doFinal(byte[] input, int inputOffset, int inputLen, byte[] output) throws IllegalStateException, ShortBufferException, IllegalBlockSizeException, BadPaddingException
inputOffset
,
only the first inputLen
bytes are en/decrypted, including any buffered bytes of a previous
update
operation. If necessary, padding is performed. The result is stored in the
given output byte array. The number of bytes stored in this byte array are returned.input
- the byte array holding the data to be processedinputOffset
- the offset indicating the start position within the input byte arrayinputLen
- the number of bytes to be processedoutput
- the byte array for holding the resultIllegalStateException
- if this cipher is in a wrong state (e.g., has not yet been initialized)ShortBufferException
- if the given output buffer is too small for holding the resultIllegalBlockSizeException
- the offset indicating the start position within the output byte array
to which the en/decrypted data is writtenBadPaddingException
- if the decrypted data is not bounded by the proper padding bytes after data
decryption including (un)paddingpublic final int doFinal(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) throws IllegalStateException, ShortBufferException, IllegalBlockSizeException, BadPaddingException
inputOffset
,
only the first inputLen
bytes are en/decrypted, including any buffered bytes of a previous
update
operation. If necessary, padding is performed. The result is stored in the
given output byte array, beginning at outputOffset
. The number of bytes stored in
this byte array are returned.input
- the byte array holding the data to be processedinputOffset
- the offset indicating the start position within the input byte arrayinputLen
- the number of bytes to be processedoutput
- the byte array for holding the resultoutputOffset
- the offset indicating the start position within the output byte array
to which the en/decrypted data is writtenIllegalStateException
- if this cipher is in a wrong state (e.g., has not yet been initialized)ShortBufferException
- if the given output buffer is too small for holding the resultIllegalBlockSizeException
- if the total length of the processed data is not a multiple of the block size for a
(no padding performing) block cipherBadPaddingException
- if the decrypted data is not bounded by the proper padding bytes after data
decryption including (un)paddingpublic final byte[] wrap(Key key) throws IllegalStateException, IllegalBlockSizeException, InvalidKeyException
public final Key unwrap(byte[] wrappedKey, String wrappedKeyAlgorithm, int wrappedKeyType) throws IllegalStateException, InvalidKeyException, NoSuchAlgorithmException
public final ExemptionMechanism getExemptionMechanism()
public String toString()
toString
in class Object
|
This Javadoc may contain text parts from Internet Standard specifications (RFC 2459, 3280, 3039, 2560, 1521, 821, 822, 2253, 1319, 1321, ,2630, 2631, 2268, 3058, 2984, 2104, 2144, 2040, 2311, 2279, see copyright note) and RSA Data Security Public-Key Cryptography Standards (PKCS#1,3,5,7,8,9,10,12, see copyright note). | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |