public class

KeyGenerator

extends Object
java.lang.Object
   ↳ javax.crypto.KeyGenerator

Class Overview

This class provides the public API for generating symmetric cryptographic keys.

Summary

Protected Constructors
KeyGenerator(KeyGeneratorSpi keyGenSpi, Provider provider, String algorithm)
Creates a new KeyGenerator instance.
Public Methods
final SecretKey generateKey()
Generates a secret key.
final String getAlgorithm()
Returns the name of the key generation algorithm.
final static KeyGenerator getInstance(String algorithm, String provider)
Creates a new KeyGenerator instance that provides the specified key algorithm from the specified provider.
final static KeyGenerator getInstance(String algorithm, Provider provider)
Creates a new KeyGenerator instance that provides the specified key algorithm from the specified provider.
final static KeyGenerator getInstance(String algorithm)
Creates a new KeyGenerator instance that provides the specified key algorithm,
final Provider getProvider()
Returns the provider of this KeyGenerator instance.
final void init(int keysize, SecureRandom random)
Initializes this KeyGenerator instance for the specified key size (in bits) using the specified randomness source.
final void init(SecureRandom random)
Initializes this KeyGenerator with the specified randomness source.
final void init(AlgorithmParameterSpec params, SecureRandom random)
Initializes this KeyGenerator instance with the specified algorithm parameters and randomness source.
final void init(AlgorithmParameterSpec params)
Initializes this KeyGenerator instance with the specified algorithm parameters.
final void init(int keysize)
Initializes this KeyGenerator instance for the specified key size (in bits).
[Expand]
Inherited Methods
From class java.lang.Object

Protected Constructors

protected KeyGenerator (KeyGeneratorSpi keyGenSpi, Provider provider, String algorithm)

Since: API Level 1

Creates a new KeyGenerator instance.

Parameters
keyGenSpi the implementation delegate.
provider the implementation provider.
algorithm the name of the algorithm.

Public Methods

public final SecretKey generateKey ()

Since: API Level 1

Generates a secret key.

Returns
  • the generated secret key.

public final String getAlgorithm ()

Since: API Level 1

Returns the name of the key generation algorithm.

Returns
  • the name of the key generation algorithm.

public static final KeyGenerator getInstance (String algorithm, String provider)

Since: API Level 1

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.

public static final KeyGenerator getInstance (String algorithm, Provider provider)

Since: API Level 1

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.

public static final KeyGenerator getInstance (String algorithm)

Since: API Level 1

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.

public final Provider getProvider ()

Since: API Level 1

Returns the provider of this KeyGenerator instance.

Returns
  • the provider of this KeyGenerator instance.

public final void init (int keysize, SecureRandom random)

Since: API Level 1

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.

public final void init (SecureRandom random)

Since: API Level 1

Initializes this KeyGenerator with the specified randomness source.

Parameters
random the randomness source for any random bytes.

public final void init (AlgorithmParameterSpec params, SecureRandom random)

Since: API Level 1

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.

public final void init (AlgorithmParameterSpec params)

Since: API Level 1

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.

public final void init (int keysize)

Since: API Level 1

Initializes this KeyGenerator instance for the specified key size (in bits).

Parameters
keysize the size of the key (in bits).