Package javax.crypto
Class KeyAgreement
java.lang.Object
javax.crypto.KeyAgreement
public class KeyAgreement extends Object
This class provides the functionality for a key exchange protocol. This
enables two or more parties to agree on a secret key for symmetric
cryptography.
-
Constructor Summary
Constructors Modifier Constructor Description protectedKeyAgreement(KeyAgreementSpi keyAgreeSpi, Provider provider, String algorithm)Creates a newKeyAgreementinstance. -
Method Summary
Modifier and Type Method Description KeydoPhase(Key key, boolean lastPhase)Does the next (or the last) phase of the key agreement, using the specified key.byte[]generateSecret()Generates the shared secret.intgenerateSecret(byte[] sharedSecret, int offset)Generates the shared secret and stores it into the buffersharedSecredatoffset.SecretKeygenerateSecret(String algorithm)Generates the shared secret.StringgetAlgorithm()Returns the name of the key agreement algorithm.static KeyAgreementgetInstance(String algorithm)Creates a newKeyAgreementfor the specified algorithm.static KeyAgreementgetInstance(String algorithm, String provider)Creates a newKeyAgreementfor the specified algorithm from the specified provider.static KeyAgreementgetInstance(String algorithm, Provider provider)Create a newKeyAgreementfor the specified algorithm from the specified provider.ProvidergetProvider()Returns the provider for thisKeyAgreementinstance.voidinit(Key key)Initializes thisKeyAgreementwith the specified key.voidinit(Key key, SecureRandom random)Initializes thisKeyAgreementwith the specified key and the specified randomness source.voidinit(Key key, AlgorithmParameterSpec params)Initializes thisKeyAgreementwith the specified key and the algorithm parameters.voidinit(Key key, AlgorithmParameterSpec params, SecureRandom random)Initializes thisKeyAgreementwith the specified key, algorithm parameters and randomness source.
-
Constructor Details
-
KeyAgreement
Creates a newKeyAgreementinstance.- Parameters:
keyAgreeSpi- the SPI delegate.provider- the provider providing this KeyAgreement.algorithm- the name of the key agreement algorithm.
-
-
Method Details
-
getAlgorithm
Returns the name of the key agreement algorithm.- Returns:
- the name of the key agreement algorithm.
-
getProvider
Returns the provider for thisKeyAgreementinstance.- Returns:
- the provider for this
KeyAgreementinstance.
-
getInstance
Creates a newKeyAgreementfor the specified algorithm.- Parameters:
algorithm- the name of the key agreement algorithm to create.- Returns:
- a key agreement for the specified algorithm.
- Throws:
NoSuchAlgorithmException- if no installed provider can provide the requested algorithm.NullPointerException- if the specified algorithm isnull.
-
getInstance
public static final KeyAgreement getInstance(String algorithm, String provider) throws NoSuchAlgorithmException, NoSuchProviderExceptionCreates a newKeyAgreementfor the specified algorithm from the specified provider.- Parameters:
algorithm- the name of the key agreement algorithm to create.provider- the name of the provider that provides the requested algorithm.- Returns:
- a key agreement for the specified algorithm from the specified provider.
- Throws:
NoSuchAlgorithmException- if the specified provider cannot provide the requested algorithm.NoSuchProviderException- if the specified provider does not exist.IllegalArgumentException- if the specified provider name isnullor empty.
-
getInstance
public static final KeyAgreement getInstance(String algorithm, Provider provider) throws NoSuchAlgorithmExceptionCreate a newKeyAgreementfor the specified algorithm from the specified provider.- Parameters:
algorithm- the name of the key agreement algorithm to create.provider- the provider that provides the requested algorithm.- Returns:
- a key agreement for the specified algorithm from the specified provider.
- Throws:
NoSuchAlgorithmException- if the specified provider cannot provide the requested algorithm.IllegalArgumentException- if the specified provider isnull.NullPointerException- if the specified algorithm name isnull.
-
init
Initializes thisKeyAgreementwith the specified key.- Parameters:
key- the key to initialize this key agreement.- Throws:
InvalidKeyException- if the specified key cannot be used to initialize this key agreement.
-
init
Initializes thisKeyAgreementwith the specified key and the specified randomness source.- Parameters:
key- the key to initialize this key agreement.random- the source for any randomness needed.- Throws:
InvalidKeyException- if the specified key cannot be used to initialize this key agreement.
-
init
public final void init(Key key, AlgorithmParameterSpec params) throws InvalidKeyException, InvalidAlgorithmParameterExceptionInitializes thisKeyAgreementwith the specified key and the algorithm parameters.- Parameters:
key- the key to initialize this key agreement.params- the parameters for this key agreement algorithm.- Throws:
InvalidKeyException- if the specified key cannot be used to initialize this key agreement.InvalidAlgorithmParameterException- if the specified parameters are invalid for this key agreement algorithm.
-
init
public final void init(Key key, AlgorithmParameterSpec params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterExceptionInitializes thisKeyAgreementwith the specified key, algorithm parameters and randomness source.- Parameters:
key- the key to initialize this key agreement.params- the parameters for this key agreement algorithm.random- the source for any randomness needed.- Throws:
InvalidKeyException- if the specified key cannot be used to initialize this key agreement.InvalidAlgorithmParameterException- if the specified parameters are invalid for this key agreement algorithm.
-
doPhase
public final Key doPhase(Key key, boolean lastPhase) throws InvalidKeyException, IllegalStateExceptionDoes the next (or the last) phase of the key agreement, using the specified key.- Parameters:
key- the key received from the other party for this phase.lastPhase- set totrueif this is the last phase of this key agreement.- Returns:
- the intermediate key from this phase or
nullif there is no intermediate key for this phase. - Throws:
InvalidKeyException- if the specified key cannot be used in this key agreement or this phase,IllegalStateException- if this instance has not been initialized.
-
generateSecret
Generates the shared secret.- Returns:
- the generated shared secret.
- Throws:
IllegalStateException- if this key agreement is not complete.
-
generateSecret
public final int generateSecret(byte[] sharedSecret, int offset) throws IllegalStateException, ShortBufferExceptionGenerates the shared secret and stores it into the buffersharedSecredatoffset.- Parameters:
sharedSecret- the buffer to store the shared secret.offset- the offset in the buffer.- Returns:
- the number of bytes stored in the buffer.
- Throws:
IllegalStateException- if this key agreement is not complete.ShortBufferException- if the specified buffer is too small for the shared secret.
-
generateSecret
public final SecretKey generateSecret(String algorithm) throws IllegalStateException, NoSuchAlgorithmException, InvalidKeyExceptionGenerates the shared secret.- Parameters:
algorithm- the algorithm to for theSecretKey- Returns:
- the shared secret as a
SecretKeyof the specified algorithm. - Throws:
IllegalStateException- if this key agreement is not complete.NoSuchAlgorithmException- if the specified algorithm for the secret key does not exists.InvalidKeyException- if aSecretKeywith the specified algorithm cannot be created using the generated shared secret.
-