Skip to content

Most visited

Recently visited

navigation
Added in API level 1

KeyAgreement

public class KeyAgreement
extends Object

java.lang.Object
   ↳ javax.crypto.KeyAgreement


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 SecretKey generateSecret(String algorithm)

Generates the shared secret.

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

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

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, Provider provider)

Create 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 Provider getProvider()

Returns the provider for this KeyAgreement instance.

final void init(Key key, SecureRandom random)

Initializes this KeyAgreement with the specified key and the specified 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, AlgorithmParameterSpec params, SecureRandom random)

Initializes this KeyAgreement with the specified key, algorithm parameters and randomness source.

final void init(Key key)

Initializes this KeyAgreement with the specified key.

Inherited methods

From class java.lang.Object

Protected constructors

KeyAgreement

Added in API level 1
KeyAgreement (KeyAgreementSpi keyAgreeSpi, 
                Provider provider, 
                String algorithm)

Creates a new KeyAgreement instance.

Parameters
keyAgreeSpi KeyAgreementSpi: the SPI delegate.
provider Provider: the provider providing this KeyAgreement.
algorithm String: the name of the key agreement algorithm.

Public methods

doPhase

Added in API level 1
Key doPhase (Key key, 
                boolean lastPhase)

Does the next (or the last) phase of the key agreement, using the specified key.

Parameters
key Key: the key received from the other party for this phase.
lastPhase boolean: set to true if this is the last phase of this key agreement.
Returns
Key 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.

generateSecret

Added in API level 1
SecretKey generateSecret (String algorithm)

Generates the shared secret.

Parameters
algorithm String: the algorithm to for the SecretKey
Returns
SecretKey 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.

generateSecret

Added in API level 1
int generateSecret (byte[] sharedSecret, 
                int offset)

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

Parameters
sharedSecret byte: the buffer to store the shared secret.
offset int: the offset in the buffer.
Returns
int 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

Added in API level 1
byte[] generateSecret ()

Generates the shared secret.

Returns
byte[] the generated shared secret.
Throws
IllegalStateException if this key agreement is not complete.

getAlgorithm

Added in API level 1
String getAlgorithm ()

Returns the name of the key agreement algorithm.

Returns
String the name of the key agreement algorithm.

getInstance

Added in API level 1
KeyAgreement getInstance (String algorithm, 
                String provider)

Creates a new KeyAgreement for the specified algorithm from the specified provider.

Parameters
algorithm String: the name of the key agreement algorithm to create.
provider String: the name of the provider that provides the requested algorithm.
Returns
KeyAgreement 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.

getInstance

Added in API level 1
KeyAgreement getInstance (String algorithm, 
                Provider provider)

Create a new KeyAgreement for the specified algorithm from the specified provider. The provider supplied does not have to be registered.

Parameters
algorithm String: the name of the key agreement algorithm to create.
provider Provider: the provider that provides the requested algorithm.
Returns
KeyAgreement 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.

getInstance

Added in API level 1
KeyAgreement getInstance (String algorithm)

Creates a new KeyAgreement for the specified algorithm.

Parameters
algorithm String: the name of the key agreement algorithm to create.
Returns
KeyAgreement 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.

getProvider

Added in API level 1
Provider getProvider ()

Returns the provider for this KeyAgreement instance.

Returns
Provider the provider for this KeyAgreement instance.

init

Added in API level 1
void init (Key key, 
                SecureRandom random)

Initializes this KeyAgreement with the specified key and the specified randomness source.

Parameters
key Key: the key to initialize this key agreement.
random SecureRandom: the source for any randomness needed.
Throws
InvalidKeyException if the specified key cannot be used to initialize this key agreement.

init

Added in API level 1
void init (Key key, 
                AlgorithmParameterSpec params)

Initializes this KeyAgreement with the specified key and the algorithm parameters.

Parameters
key Key: the key to initialize this key agreement.
params AlgorithmParameterSpec: 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

Added in API level 1
void init (Key key, 
                AlgorithmParameterSpec params, 
                SecureRandom random)

Initializes this KeyAgreement with the specified key, algorithm parameters and randomness source.

Parameters
key Key: the key to initialize this key agreement.
params AlgorithmParameterSpec: the parameters for this key agreement algorithm.
random SecureRandom: 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.

init

Added in API level 1
void init (Key key)

Initializes this KeyAgreement with the specified key.

Parameters
key Key: the key to initialize this key agreement.
Throws
InvalidKeyException if the specified key cannot be used to initialize this key agreement.
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.