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 Details

    • KeyAgreement

      protected KeyAgreement​(KeyAgreementSpi keyAgreeSpi, Provider provider, String algorithm)
      Creates a new KeyAgreement instance.
      Parameters:
      keyAgreeSpi - the SPI delegate.
      provider - the provider providing this KeyAgreement.
      algorithm - the name of the key agreement algorithm.
  • Method Details

    • getAlgorithm

      public final String getAlgorithm()
      Returns the name of the key agreement algorithm.
      Returns:
      the name of the key agreement algorithm.
    • getProvider

      public final Provider getProvider()
      Returns the provider for this KeyAgreement instance.
      Returns:
      the provider for this KeyAgreement instance.
    • getInstance

      public static final KeyAgreement getInstance​(String algorithm) throws NoSuchAlgorithmException
      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.
    • getInstance

      public static final KeyAgreement getInstance​(String algorithm, String provider) throws NoSuchAlgorithmException, NoSuchProviderException
      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.
    • getInstance

      public static final KeyAgreement getInstance​(String algorithm, Provider provider) throws NoSuchAlgorithmException
      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.
    • init

      public final void init​(Key key) throws InvalidKeyException
      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.
    • init

      public final void init​(Key key, SecureRandom random) throws InvalidKeyException
      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.
    • init

      public final void init​(Key key, AlgorithmParameterSpec params) throws InvalidKeyException, InvalidAlgorithmParameterException
      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.
    • init

      public final void init​(Key key, AlgorithmParameterSpec params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException
      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.
    • doPhase

      public final Key doPhase​(Key key, boolean lastPhase) throws InvalidKeyException, IllegalStateException
      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.
    • generateSecret

      public final byte[] generateSecret() throws IllegalStateException
      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, ShortBufferException
      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.
    • generateSecret

      public final SecretKey generateSecret​(String algorithm) throws IllegalStateException, NoSuchAlgorithmException, InvalidKeyException
      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.