Please note that the contents of this offline web site may be out of date. To access the most recent documentation visit the online version .
Note that links that point to online resources are green in color and will open in a new window.
We would love it if you could give us feedback about this material by filling this form (You have to be online to fill it)
Android APIs
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 ( 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 ( SecureRandom random)
Initializes this KeyGenerator with the specified randomness source.
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 (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)

Added in 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 ()

Added in API level 1

Generates a secret key.

Returns
  • the generated secret key.

public final String getAlgorithm ()

Added in 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)

Added in 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)

Added in 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)

Added in 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 ()

Added in API level 1

Returns the provider of this KeyGenerator instance.

Returns
  • the provider of this KeyGenerator instance.

public final void init ( AlgorithmParameterSpec params, SecureRandom random)

Added in 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)

Added in 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 ( SecureRandom random)

Added in API level 1

Initializes this KeyGenerator with the specified randomness source.

Parameters
random the randomness source for any random bytes.

public final void init (int keysize, SecureRandom random)

Added in 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 (int keysize)

Added in API level 1

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

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