Package javax.crypto

Class 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 Detail

      • 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 Detail

      • 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. The provider supplied does not have to be registered.
        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.
      • getCurrentSpi

        public KeyAgreementSpi getCurrentSpi()
        Returns the KeyAgreementSpi backing this KeyAgreement or null if no KeyAgreementSpi is backing this KeyAgreement.
      • 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.
      • 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.