Package javax.crypto

Class KeyGenerator


  • public class KeyGenerator
    extends Object
    This class provides the public API for generating symmetric cryptographic keys.
    • Constructor Detail

      • KeyGenerator

        protected KeyGenerator​(KeyGeneratorSpi keyGenSpi,
                               Provider provider,
                               String algorithm)
        Creates a new KeyGenerator instance.
        Parameters:
        keyGenSpi - the implementation delegate.
        provider - the implementation provider.
        algorithm - the name of the algorithm.
    • Method Detail

      • getAlgorithm

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

        public final Provider getProvider()
        Returns the provider of this KeyGenerator instance.
        Returns:
        the provider of this KeyGenerator instance.
      • getInstance

        public static final KeyGenerator getInstance​(String algorithm)
                                              throws NoSuchAlgorithmException
        Creates a new KeyGenerator instance that provides the specified key algorithm,
        Parameters:
        algorithm - the name of the requested key algorithm
        Returns:
        the new KeyGenerator instance.
        Throws:
        NoSuchAlgorithmException - if the specified algorithm is not available by any provider.
        NullPointerException - if algorithm is null.
      • getInstance

        public static final KeyGenerator getInstance​(String algorithm,
                                                     Provider provider)
                                              throws NoSuchAlgorithmException
        Creates a new KeyGenerator instance that provides the specified key algorithm from the specified provider. The provider supplied does not have to be registered.
        Parameters:
        algorithm - the name of the requested key algorithm.
        provider - the provider that is providing the algorithm
        Returns:
        the new KeyGenerator instance.
        Throws:
        NoSuchAlgorithmException - if the specified algorithm is not provided by the specified provider.
        IllegalArgumentException - if the specified provider is null.
        NullPointerException - if the specified algorithm name is null.
      • generateKey

        public final SecretKey generateKey()
        Generates a secret key.
        Returns:
        the generated secret key.
      • init

        public final void init​(int keysize)
        Initializes this KeyGenerator instance for the specified key size (in bits).
        Parameters:
        keysize - the size of the key (in bits).
      • init

        public final void init​(int keysize,
                               SecureRandom random)
        Initializes this KeyGenerator instance for the specified key size (in bits) using the specified randomness source.
        Parameters:
        keysize - the size of the key (in bits).
        random - the randomness source for any random bytes.
      • init

        public final void init​(SecureRandom random)
        Initializes this KeyGenerator with the specified randomness source.
        Parameters:
        random - the randomness source for any random bytes.