public class

KeyAgreement

extends Object
java.lang.Object
   ↳ javax.crypto.KeyAgreement

Class Overview

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.

Summary

Protected Constructors
KeyAgreement(KeyAgreementSpi keyAgreeSpi, Provider provider, String algorithm)
Creates a new KeyAgreement instance.
Public Methods
final Key doPhase(Key key, boolean lastPhase)
Does the next (or the last) phase of the key agreement, using the specified key.
final int generateSecret(byte[] sharedSecret, int offset)
Generates the shared secret and stores it into the buffer sharedSecred at offset.
final SecretKey generateSecret(String algorithm)
Generates the shared secret.
final byte[] generateSecret()
Generates the shared secret.
final String getAlgorithm()
Returns the name of the key agreement algorithm.
final static KeyAgreement getInstance(String algorithm, String provider)
Creates a new KeyAgreement for the specified algorithm from the specified provider.
final static KeyAgreement getInstance(String algorithm)
Creates a new KeyAgreement for the specified algorithm.
final static KeyAgreement getInstance(String algorithm, Provider provider)
Create a new KeyAgreement for the specified algorithm from the specified provider.
final Provider getProvider()
Returns the provider for this KeyAgreement instance.
final void init(Key key, AlgorithmParameterSpec params, SecureRandom random)
Initializes this KeyAgreement with the specified key, algorithm parameters and randomness source.
final void init(Key key, AlgorithmParameterSpec params)
Initializes this KeyAgreement with the specified key and the algorithm parameters.
final void init(Key key)
Initializes this KeyAgreement with the specified key.
final void init(Key key, SecureRandom random)
Initializes this KeyAgreement with the specified key and the specified randomness source.
[Expand]
Inherited Methods
From class java.lang.Object

Protected Constructors

protected KeyAgreement (KeyAgreementSpi keyAgreeSpi, Provider provider, String algorithm)

Since: API Level 1

Creates a new KeyAgreement instance.

Parameters
keyAgreeSpi the SPI delegate.
provider the provider providing this KeyAgreement.
algorithm the name of the key agreement algorithm.

Public Methods

public final Key doPhase (Key key, boolean lastPhase)

Since: API Level 1

Does 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 to true if this is the last phase of this key agreement.
Returns
  • the intermediate key from this phase or null if 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.

public final int generateSecret (byte[] sharedSecret, int offset)

Since: API Level 1

Generates the shared secret and stores it into the buffer sharedSecred at offset.

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.

public final SecretKey generateSecret (String algorithm)

Since: API Level 1

Generates the shared secret.

Parameters
algorithm the algorithm to for the SecretKey
Returns
  • the shared secret as a SecretKey of 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 a SecretKey with the specified algorithm cannot be created using the generated shared secret.

public final byte[] generateSecret ()

Since: API Level 1

Generates the shared secret.

Returns
  • the generated shared secret.
Throws
IllegalStateException if this key agreement is not complete.

public final String getAlgorithm ()

Since: API Level 1

Returns the name of the key agreement algorithm.

Returns
  • the name of the key agreement algorithm.

public static final KeyAgreement getInstance (String algorithm, String provider)

Since: API Level 1

Creates a new KeyAgreement for 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 is null or empty.

public static final KeyAgreement getInstance (String algorithm)

Since: API Level 1

Creates a new KeyAgreement for 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 is null.

public static final KeyAgreement getInstance (String algorithm, Provider provider)

Since: API Level 1

Create a new KeyAgreement for 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 is null.
NullPointerException if the specified algorithm name is null.

public final Provider getProvider ()

Since: API Level 1

Returns the provider for this KeyAgreement instance.

Returns
  • the provider for this KeyAgreement instance.

public final void init (Key key, AlgorithmParameterSpec params, SecureRandom random)

Since: API Level 1

Initializes this KeyAgreement with 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.

public final void init (Key key, AlgorithmParameterSpec params)

Since: API Level 1

Initializes this KeyAgreement with 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.

public final void init (Key key)

Since: API Level 1

Initializes this KeyAgreement with 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.

public final void init (Key key, SecureRandom random)

Since: API Level 1

Initializes this KeyAgreement with 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.