public abstract class AsymmetricBlockCipher extends CipherSpiExt
Modifier and Type | Field and Description |
---|---|
protected java.io.ByteArrayOutputStream |
buf
Internal buffer
|
protected int |
cipherTextSize
The maximum number of bytes the cipher can encrypt.
|
protected int |
maxPlainTextSize
The maximum number of bytes the cipher can decrypt.
|
protected java.security.spec.AlgorithmParameterSpec |
paramSpec
ParameterSpec used with this cipher
|
DECRYPT_MODE, ENCRYPT_MODE, opMode
Constructor and Description |
---|
AsymmetricBlockCipher()
The AsymmetricBlockCipher() constructor
|
Modifier and Type | Method and Description |
---|---|
protected void |
checkLength(int inLen)
Check if the message length plus the length of the input length can be
en/decrypted.
|
byte[] |
doFinal(byte[] input,
int inOff,
int inLen)
Finish a multiple-part encryption or decryption operation (depending on
how this cipher was initialized).
|
int |
doFinal(byte[] input,
int inOff,
int inLen,
byte[] output,
int outOff)
Finish a multiple-part encryption or decryption operation (depending on
how this cipher was initialized).
|
int |
getBlockSize()
Return the block size (in bytes).
|
byte[] |
getIV()
Return the initialization vector.
|
int |
getOutputSize(int inLen)
Return 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 inLen (in bytes).
|
java.security.spec.AlgorithmParameterSpec |
getParameters()
Returns the parameters used with this cipher.
|
protected abstract void |
initCipherDecrypt(java.security.Key key,
java.security.spec.AlgorithmParameterSpec params)
Initialize the AsymmetricBlockCipher with a certain key for data
encryption.
|
protected abstract void |
initCipherEncrypt(java.security.Key key,
java.security.spec.AlgorithmParameterSpec params,
java.security.SecureRandom sr)
Initialize the AsymmetricBlockCipher with a certain key for data
encryption.
|
void |
initDecrypt(java.security.Key key)
Initialize the cipher for decryption by forwarding it to
initDecrypt(Key, AlgorithmParameterSpec) . |
void |
initDecrypt(java.security.Key key,
java.security.spec.AlgorithmParameterSpec params)
This method initializes the AsymmetricBlockCipher with a certain key for
data decryption.
|
void |
initEncrypt(java.security.Key key)
Initializes the cipher for encryption by forwarding it to
initEncrypt(Key, FlexiSecureRandom).
|
void |
initEncrypt(java.security.Key key,
java.security.spec.AlgorithmParameterSpec params)
Initializes the cipher for encryption by forwarding it to
initEncrypt(Key, FlexiSecureRandom, AlgorithmParameterSpec).
|
void |
initEncrypt(java.security.Key key,
java.security.spec.AlgorithmParameterSpec params,
java.security.SecureRandom secureRandom)
This method initializes the AsymmetricBlockCipher with a certain key for
data encryption.
|
void |
initEncrypt(java.security.Key key,
java.security.SecureRandom random)
Initialize this cipher for encryption by forwarding it to
initEncrypt(Key, FlexiSecureRandom, AlgorithmParameterSpec).
|
protected abstract byte[] |
messageDecrypt(byte[] input)
Decrypt the ciphertext stored in input.
|
protected abstract byte[] |
messageEncrypt(byte[] input)
Encrypt the message stored in input.
|
protected void |
setMode(java.lang.String modeName)
Since asymmetric block ciphers do not support modes, this method does
nothing.
|
protected void |
setPadding(java.lang.String paddingName)
Since asymmetric block ciphers do not support padding, this method does
nothing.
|
byte[] |
update(byte[] input,
int inOff,
int inLen)
Continue a multiple-part encryption or decryption operation.
|
int |
update(byte[] input,
int inOff,
int inLen,
byte[] output,
int outOff)
Continue a multiple-part encryption or decryption operation (depending on
how this cipher was initialized), processing another data part.
|
doFinal, doFinal, engineDoFinal, engineDoFinal, engineGetBlockSize, engineGetIV, engineGetKeySize, engineGetOutputSize, engineGetParameters, engineInit, engineInit, engineInit, engineSetMode, engineSetPadding, engineUpdate, engineUpdate, getKeySize, getName, update
protected java.security.spec.AlgorithmParameterSpec paramSpec
protected java.io.ByteArrayOutputStream buf
protected int maxPlainTextSize
protected int cipherTextSize
public AsymmetricBlockCipher()
public final int getBlockSize()
getBlockSize
in class CipherSpiExt
public final byte[] getIV()
CipherSpiExt
getIV
in class CipherSpiExt
public final int getOutputSize(int inLen)
If the input length plus the length of the buffered data exceeds the maximum length, 0 is returned.
getOutputSize
in class CipherSpiExt
inLen
- the length of the inputpublic final java.security.spec.AlgorithmParameterSpec getParameters()
The returned parameters may be the same that were used to initialize 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 initialized with any).
getParameters
in class CipherSpiExt
public final void initEncrypt(java.security.Key key) throws java.security.InvalidKeyException
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 initialized for encryption, and raise an InvalidKeyException if it is being initialized for decryption. The generated parameters can be retrieved using engineGetParameters or engineGetIV (if the parameter is an IV).
key
- the encryption or decryption key.java.security.InvalidKeyException
- if the given key is inappropriate for initializing this
cipher.public final void initEncrypt(java.security.Key key, java.security.SecureRandom random) throws java.security.InvalidKeyException
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 initialized for encryption, and raise an InvalidKeyException if it is being initialized for decryption. The generated parameters can be retrieved using engineGetParameters or engineGetIV (if the parameter is an IV).
key
- the encryption or decryption key.random
- the source of randomness.java.security.InvalidKeyException
- if the given key is inappropriate for initializing this
cipher.public final void initEncrypt(java.security.Key key, java.security.spec.AlgorithmParameterSpec params) throws java.security.InvalidKeyException, java.security.InvalidAlgorithmParameterException
key
- the encryption or decryption key.params
- the algorithm parameters.java.security.InvalidKeyException
- if the given key is inappropriate for initializing this
cipher.java.security.InvalidAlgorithmParameterException
- if the given algortihm parameters are inappropriate for
this cipher, or if this cipher is being initialized for
decryption and requires algorithm parameters and params
is null.public final void initEncrypt(java.security.Key key, java.security.spec.AlgorithmParameterSpec params, java.security.SecureRandom secureRandom) throws java.security.InvalidKeyException, java.security.InvalidAlgorithmParameterException
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 initialized, it loses all previously-acquired state. In other words, initializing a Cipher is equivalent to creating a new instance of that Cipher and initializing it
initEncrypt
in class CipherSpiExt
key
- the key which has to be used to encrypt data.secureRandom
- the source of randomness.params
- the algorithm parameters.java.security.InvalidKeyException
- if the given key is inappropriate for initializing this
cipherjava.security.InvalidAlgorithmParameterException
- if the given algorithm parameters are inappropriate for
this cipher, or if this cipher is being initialized for
decryption and requires algorithm parameters and params
is null.public final void initDecrypt(java.security.Key key) throws java.security.InvalidKeyException
initDecrypt(Key, AlgorithmParameterSpec)
.
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 initialized for encryption, and raise an InvalidKeyException if it is being initialized for decryption. The generated parameters can be retrieved using engineGetParameters or engineGetIV (if the parameter is an IV).
key
- the encryption or decryption key.java.security.InvalidKeyException
- if the given key is inappropriate for initializing this
cipher.public final void initDecrypt(java.security.Key key, java.security.spec.AlgorithmParameterSpec params) throws java.security.InvalidKeyException, java.security.InvalidAlgorithmParameterException
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 initialized, it loses all previously-acquired state. In other words, initializing a Cipher is equivalent to creating a new instance of that Cipher and initializing it
initDecrypt
in class CipherSpiExt
key
- the key which has to be used to decrypt data.params
- the algorithm parameters.java.security.InvalidKeyException
- if the given key is inappropriate for initializing this
cipherjava.security.InvalidAlgorithmParameterException
- if the given algorithm parameters are inappropriate for
this cipher, or if this cipher is being initialized for
decryption and requires algorithm parameters and params
is null.public final byte[] update(byte[] input, int inOff, int inLen)
update
in class CipherSpiExt
input
- byte array containing the next part of the inputinOff
- index in the array where the input startsinLen
- length of the inputpublic final int update(byte[] input, int inOff, int inLen, byte[] output, int outOff)
update
in class CipherSpiExt
input
- the input bufferinOff
- the offset where the input startsinLen
- the input lengthoutput
- the output bufferoutOff
- the offset where the result is storedpublic final byte[] doFinal(byte[] input, int inOff, int inLen) throws javax.crypto.IllegalBlockSizeException, javax.crypto.BadPaddingException
doFinal
in class CipherSpiExt
input
- the input bufferinOff
- the offset where the input startsinLen
- the input lengthjavax.crypto.IllegalBlockSizeException
- if the plaintext or ciphertext size is too large.javax.crypto.BadPaddingException
- if the ciphertext is invalid.public final int doFinal(byte[] input, int inOff, int inLen, byte[] output, int outOff) throws javax.crypto.ShortBufferException, javax.crypto.IllegalBlockSizeException, javax.crypto.BadPaddingException
doFinal
in class CipherSpiExt
input
- the input bufferinOff
- the offset where the input startsinLen
- the input lengthoutput
- the buffer for the resultoutOff
- the offset where the result is storedjavax.crypto.ShortBufferException
- if the output buffer is too small to hold the result.javax.crypto.IllegalBlockSizeException
- if the plaintext or ciphertext size is too large.javax.crypto.BadPaddingException
- if the ciphertext is invalid.protected final void setMode(java.lang.String modeName)
setMode
in class CipherSpiExt
modeName
- the cipher mode (unused)protected final void setPadding(java.lang.String paddingName)
setPadding
in class CipherSpiExt
paddingName
- the name of the padding scheme (not used)protected void checkLength(int inLen) throws javax.crypto.IllegalBlockSizeException
maxPlainTextSize
and cipherTextSize
which are set by
the implementations. If the input length plus the length of the internal
buffer is greater than maxPlainTextSize
for encryption or not
equal to cipherTextSize
for decryption, an
IllegalBlockSizeException
will be thrown.inLen
- length of the input to checkjavax.crypto.IllegalBlockSizeException
- if the input length is invalid.protected abstract void initCipherEncrypt(java.security.Key key, java.security.spec.AlgorithmParameterSpec params, java.security.SecureRandom sr) throws java.security.InvalidKeyException, java.security.InvalidAlgorithmParameterException
key
- the key which has to be used to encrypt dataparams
- the algorithm parameterssr
- the source of randomnessjava.security.InvalidKeyException
- if the given key is inappropriate for initializing this
cipher.java.security.InvalidAlgorithmParameterException
- if the given parameters are inappropriate for
initializing this cipher.protected abstract void initCipherDecrypt(java.security.Key key, java.security.spec.AlgorithmParameterSpec params) throws java.security.InvalidKeyException, java.security.InvalidAlgorithmParameterException
key
- the key which has to be used to decrypt dataparams
- the algorithm parametersjava.security.InvalidKeyException
- if the given key is inappropriate for initializing this
cipherjava.security.InvalidAlgorithmParameterException
- if the given parameters are inappropriate for
initializing this cipher.protected abstract byte[] messageEncrypt(byte[] input) throws javax.crypto.IllegalBlockSizeException, javax.crypto.BadPaddingException
input
- the message to be encrypted (usually the message length is
less than or equal to maxPlainTextSize)javax.crypto.IllegalBlockSizeException
- if the input is inappropriate for this cipher.javax.crypto.BadPaddingException
- if the input format is invalid.protected abstract byte[] messageDecrypt(byte[] input) throws javax.crypto.IllegalBlockSizeException, javax.crypto.BadPaddingException
input
- the ciphertext to be decrypted (the ciphertext length is
less than or equal to maxCipherTextSize)javax.crypto.IllegalBlockSizeException
- if the input is inappropriate for this cipher.javax.crypto.BadPaddingException
- if the input format is invalid.