Package javax.crypto

Class KeyGenerator

java.lang.Object
javax.crypto.KeyGenerator

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

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

    • 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, String provider) throws NoSuchAlgorithmException, NoSuchProviderException
      Creates a new KeyGenerator instance that provides the specified key algorithm from the specified provider.
      Parameters:
      algorithm - the name of the requested key algorithm.
      provider - the name of 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.
      NoSuchProviderException - if the specified provider is not available.
      IllegalArgumentException - if the specified provider is name is null or empty.
      NullPointerException - if the specified algorithm name 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.
      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​(AlgorithmParameterSpec params) throws InvalidAlgorithmParameterException
      Initializes this KeyGenerator instance with the specified algorithm parameters.
      Parameters:
      params - the parameters for the key generation algorithm.
      Throws:
      InvalidAlgorithmParameterException - if the parameters cannot be used to initialize this key generator algorithm.
    • init

      public final void init​(AlgorithmParameterSpec params, SecureRandom random) throws InvalidAlgorithmParameterException
      Initializes this KeyGenerator instance with the specified algorithm parameters and randomness source.
      Parameters:
      params - the parameters for the key generation algorithm.
      random - the randomness source for any random bytes.
      Throws:
      InvalidAlgorithmParameterException - if the parameters cannot be uses to initialize this key generator algorithm.
    • 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.