Package java.security

Class KeyPairGenerator

java.lang.Object
java.security.KeyPairGeneratorSpi
java.security.KeyPairGenerator
Direct Known Subclasses:
KeyPairGeneratorSpi, KeyPairGeneratorSpi, KeyPairGeneratorSpi, KeyPairGeneratorSpi, OpenSSLECKeyPairGenerator

public abstract class KeyPairGenerator
extends KeyPairGeneratorSpi
KeyPairGenerator is an engine class which is capable of generating a private key and its related public key utilizing the algorithm it was initialized with.
See Also:
KeyPairGeneratorSpi
  • Constructor Details

    • KeyPairGenerator

      protected KeyPairGenerator​(String algorithm)
      Constructs a new instance of KeyPairGenerator with the name of the algorithm to use.
      Parameters:
      algorithm - the name of algorithm to use
  • Method Details

    • getAlgorithm

      public String getAlgorithm()
      Returns the name of the algorithm of this KeyPairGenerator.
      Returns:
      the name of the algorithm of this KeyPairGenerator
    • getInstance

      public static KeyPairGenerator getInstance​(String algorithm) throws NoSuchAlgorithmException
      Returns a new instance of KeyPairGenerator that utilizes the specified algorithm.
      Parameters:
      algorithm - the name of the algorithm to use
      Returns:
      a new instance of KeyPairGenerator that utilizes the specified algorithm
      Throws:
      NoSuchAlgorithmException - if the specified algorithm is not available
      NullPointerException - if algorithm is null
    • getInstance

      public static KeyPairGenerator getInstance​(String algorithm, String provider) throws NoSuchAlgorithmException, NoSuchProviderException
      Returns a new instance of KeyPairGenerator that utilizes the specified algorithm from the specified provider.
      Parameters:
      algorithm - the name of the algorithm to use
      provider - the name of the provider
      Returns:
      a new instance of KeyPairGenerator that utilizes the specified algorithm from the specified provider
      Throws:
      NoSuchAlgorithmException - if the specified algorithm is not available
      NoSuchProviderException - if the specified provider is not available
      NullPointerException - if algorithm is null
      IllegalArgumentException - if provider == null || provider.isEmpty()
    • getInstance

      public static KeyPairGenerator getInstance​(String algorithm, Provider provider) throws NoSuchAlgorithmException
      Returns a new instance of KeyPairGenerator that utilizes the specified algorithm from the specified provider.
      Parameters:
      algorithm - the name of the algorithm to use
      provider - the provider
      Returns:
      a new instance of KeyPairGenerator that utilizes the specified algorithm from the specified provider
      Throws:
      NoSuchAlgorithmException - if the specified algorithm is not available
      NullPointerException - if algorithm is null
      IllegalArgumentException - if provider == null
    • getProvider

      public final Provider getProvider()
      Returns the provider associated with this KeyPairGenerator.
      Returns:
      the provider associated with this KeyPairGenerator
    • initialize

      public void initialize​(int keysize)
      Initializes this KeyPairGenerator with the given key size. The default parameter set and a default SecureRandom instance will be used.
      Parameters:
      keysize - the size of the key (number of bits)
    • initialize

      public void initialize​(AlgorithmParameterSpec param) throws InvalidAlgorithmParameterException
      Initializes this KeyPairGenerator with the given AlgorithmParameterSpec. A default SecureRandom instance will be used.
      Parameters:
      param - the parameters to use
      Throws:
      InvalidAlgorithmParameterException - if the specified parameters are not supported
    • genKeyPair

      public final KeyPair genKeyPair()
      Computes and returns a new unique KeyPair each time this method is called.

      This does exactly the same as generateKeyPair().

      Returns:
      a new unique KeyPair each time this method is called
    • generateKeyPair

      public KeyPair generateKeyPair()
      Computes and returns a new unique KeyPair each time this method is called.

      This does exactly the same as genKeyPair().

      Specified by:
      generateKeyPair in class KeyPairGeneratorSpi
      Returns:
      a new unique KeyPair each time this method is called
    • initialize

      public void initialize​(int keysize, SecureRandom random)
      Initializes this KeyPairGenerator with the given key size and the given SecureRandom. The default parameter set will be used.
      Specified by:
      initialize in class KeyPairGeneratorSpi
      Parameters:
      keysize - the key size
      random - the source of randomness
    • initialize

      public void initialize​(AlgorithmParameterSpec param, SecureRandom random) throws InvalidAlgorithmParameterException
      Initializes this KeyPairGenerator with the given AlgorithmParameterSpec and the given SecureRandom.
      Overrides:
      initialize in class KeyPairGeneratorSpi
      Parameters:
      param - the parameters to use
      random - the source of randomness
      Throws:
      InvalidAlgorithmParameterException - if the specified parameters are not supported