APIdia Launch Partner
Top Description Fields Constructors Methods
javax.crypto

public Class KeyGenerator

extends Object
Class Inheritance
Imports
java.util.*, java.security.*, .Provider.Service, java.security.spec.*, sun.security.jca.*, .GetInstance.Instance, sun.security.util.Debug

This class provides the functionality of a secret (symmetric) key generator.

Key generators are constructed using one of the getInstance class methods of this class.

KeyGenerator objects are reusable, i.e., after a key has been generated, the same KeyGenerator object can be re-used to generate further keys.

There are two ways to generate a key: in an algorithm-independent manner, and in an algorithm-specific manner. The only difference between the two is the initialization of the object:

In case the client does not explicitly initialize the KeyGenerator (via a call to an init method), each provider must supply (and document) a default initialization. See the Keysize Restriction sections of the JDK Providers document for information on the KeyGenerator defaults used by JDK providers. However, note that defaults may vary across different providers. Additionally, the default value for a provider may change in a future version. Therefore, it is recommended to explicitly initialize the KeyGenerator instead of relying on provider-specific defaults.

Every implementation of the Java platform is required to support the following standard KeyGenerator algorithms with the keysizes in parentheses:

These algorithms are described in the KeyGenerator section of the Java Security Standard Algorithm Names Specification. Consult the release documentation for your implementation to see if any other algorithms are supported.
Author
Jan Luehe
Since
1.4
External Specification
Java Security Standard Algorithm Names
See Also
SecretKey

Field Summary

Modifier and TypeField and Description
private final String
private static final int
private static final int
private static final int
private static final int
private int
private AlgorithmParameterSpec
private SecureRandom
private int
private final Object
private static final Debug
private Provider
private Iterator<Provider.Service>
private static final boolean
private volatile KeyGeneratorSpi

Constructor Summary

AccessConstructor and Description
protected
KeyGenerator(KeyGeneratorSpi
the delegate
keyGenSpi
,
Provider
the provider
provider
,
String
the algorithm
algorithm
)

Creates a KeyGenerator object.

private
KeyGenerator(String algorithm)

Method Summary

Modifier and TypeMethod and Description
pack-priv void
public final SecretKey

Returns:

the new key
generateKey
()

Generates a secret key.

public final String

Returns:

the algorithm name of this KeyGenerator object.
getAlgorithm
()

Returns the algorithm name of this KeyGenerator object.

public static final KeyGenerator

Returns:

the new KeyGenerator object
getInstance
(String
the standard name of the requested key algorithm. See the KeyGenerator section in the Java Security Standard Algorithm Names Specification for information about standard algorithm names.
algorithm
)

Returns a KeyGenerator object that generates secret keys for the specified algorithm.

public static final KeyGenerator

Returns:

the new KeyGenerator object
getInstance
(String
the standard name of the requested key algorithm. See the KeyGenerator section in the Java Security Standard Algorithm Names Specification for information about standard algorithm names.
algorithm
,
String
the name of the provider.
provider
)

Returns a KeyGenerator object that generates secret keys for the specified algorithm.

public static final KeyGenerator

Returns:

the new KeyGenerator object
getInstance
(String
the standard name of the requested key algorithm. See the KeyGenerator section in the Java Security Standard Algorithm Names Specification for information about standard algorithm names.
algorithm
,
Provider
the provider.
provider
)

Returns a KeyGenerator object that generates secret keys for the specified algorithm.

public final Provider

Returns:

the provider of this KeyGenerator object
getProvider
()

Returns the provider of this KeyGenerator object.

private String
public final void
init(SecureRandom
the source of randomness for this generator
random
)

Initializes this key generator.

public final void
init(AlgorithmParameterSpec
the key generation parameters
params
)

Initializes this key generator with the specified parameter set.

public final void
init(AlgorithmParameterSpec
the key generation parameters
params
,
SecureRandom
the source of randomness for this key generator
random
)

Initializes this key generator with the specified parameter set and a user-provided source of randomness.

public final void
init(int
the keysize. This is an algorithm-specific metric, specified in number of bits.
keysize
)

Initializes this key generator for a certain keysize.

public final void
init(int
the keysize. This is an algorithm-specific metric, specified in number of bits.
keysize
,
SecureRandom
the source of randomness for this key generator
random
)

Initializes this key generator for a certain keysize, using a user-provided source of randomness.

private KeyGeneratorSpi
nextSpi(KeyGeneratorSpi oldSpi, boolean reinit)

Update the active spi of this class and return the next implementation for failover.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

algorithmback to summary
private final String algorithm
I_NONEback to summary
private static final int I_NONE
I_PARAMSback to summary
private static final int I_PARAMS
I_RANDOMback to summary
private static final int I_RANDOM
I_SIZEback to summary
private static final int I_SIZE
initKeySizeback to summary
private int initKeySize
initParamsback to summary
private AlgorithmParameterSpec initParams
initRandomback to summary
private SecureRandom initRandom
initTypeback to summary
private int initType
lockback to summary
private final Object lock
pdebugback to summary
private static final Debug pdebug
providerback to summary
private Provider provider
serviceIteratorback to summary
private Iterator<Provider.Service> serviceIterator
skipDebugback to summary
private static final boolean skipDebug
spiback to summary
private volatile KeyGeneratorSpi spi

Constructor Detail

KeyGeneratorback to summary
protected KeyGenerator(KeyGeneratorSpi keyGenSpi, Provider provider, String algorithm)

Creates a KeyGenerator object.

Parameters
keyGenSpi:KeyGeneratorSpi

the delegate

provider:Provider

the provider

algorithm:String

the algorithm

KeyGeneratorback to summary
private KeyGenerator(String algorithm) throws NoSuchAlgorithmException

Method Detail

disableFailoverback to summary
pack-priv void disableFailover()
generateKeyback to summary
public final SecretKey generateKey()

Generates a secret key.

Returns:SecretKey

the new key

getAlgorithmback to summary
public final String getAlgorithm()

Returns the algorithm name of this KeyGenerator object.

This is the same name that was specified in one of the getInstance calls that created this KeyGenerator object.

Returns:String

the algorithm name of this KeyGenerator object.

getInstanceback to summary
public static final KeyGenerator getInstance(String algorithm) throws NoSuchAlgorithmException

Returns a KeyGenerator object that generates secret keys for the specified algorithm.

This method traverses the list of registered security providers, starting with the most preferred provider. A new KeyGenerator object encapsulating the KeyGeneratorSpi implementation from the first provider that supports the specified algorithm is returned.

Note that the list of registered providers may be retrieved via the Security.getProviders() method.

Implementation Note

The JDK Reference Implementation additionally uses the jdk.security.provider.preferred Security property to determine the preferred provider order for the specified algorithm. This may be different from the order of providers returned by Security.getProviders().

Parameters
algorithm:String

the standard name of the requested key algorithm. See the KeyGenerator section in the Java Security Standard Algorithm Names Specification for information about standard algorithm names.

Returns:KeyGenerator

the new KeyGenerator object

Exceptions
NoSuchAlgorithmException:
if no Provider supports a KeyGeneratorSpi implementation for the specified algorithm
NullPointerException:
if algorithm is null
External Specification
Java Security Standard Algorithm Names
See Also
java.security.Provider
getInstanceback to summary
public static final KeyGenerator getInstance(String algorithm, String provider) throws NoSuchAlgorithmException, NoSuchProviderException

Returns a KeyGenerator object that generates secret keys for the specified algorithm.

A new KeyGenerator object encapsulating the KeyGeneratorSpi implementation from the specified provider is returned. The specified provider must be registered in the security provider list.

Note that the list of registered providers may be retrieved via the Security.getProviders() method.

Parameters
algorithm:String

the standard name of the requested key algorithm. See the KeyGenerator section in the Java Security Standard Algorithm Names Specification for information about standard algorithm names.

provider:String

the name of the provider.

Returns:KeyGenerator

the new KeyGenerator object

Exceptions
NoSuchAlgorithmException:
if a KeyGeneratorSpi implementation for the specified algorithm is not available from the specified provider
NoSuchProviderException:
if the specified provider is not registered in the security provider list
IllegalArgumentException:
if the provider is null or empty
NullPointerException:
if algorithm is null
External Specification
Java Security Standard Algorithm Names
See Also
java.security.Provider
getInstanceback to summary
public static final KeyGenerator getInstance(String algorithm, Provider provider) throws NoSuchAlgorithmException

Returns a KeyGenerator object that generates secret keys for the specified algorithm.

A new KeyGenerator object encapsulating the KeyGeneratorSpi implementation from the specified provider object is returned. Note that the specified provider object does not have to be registered in the provider list.

Parameters
algorithm:String

the standard name of the requested key algorithm. See the KeyGenerator section in the Java Security Standard Algorithm Names Specification for information about standard algorithm names.

provider:Provider

the provider.

Returns:KeyGenerator

the new KeyGenerator object

Exceptions
NoSuchAlgorithmException:
if a KeyGeneratorSpi implementation for the specified algorithm is not available from the specified Provider object
IllegalArgumentException:
if the provider is null
NullPointerException:
if algorithm is null
External Specification
Java Security Standard Algorithm Names
See Also
java.security.Provider
getProviderback to summary
public final Provider getProvider()

Returns the provider of this KeyGenerator object.

Returns:Provider

the provider of this KeyGenerator object

getProviderNameback to summary
private String getProviderName()
initback to summary
public final void init(SecureRandom random)

Initializes this key generator.

Parameters
random:SecureRandom

the source of randomness for this generator

initback to summary
public final void init(AlgorithmParameterSpec params) throws InvalidAlgorithmParameterException

Initializes this key generator with the specified parameter set.

If this key generator requires any random bytes, it will get them using the java.security.SecureRandom implementation of the highest-priority installed provider as the source of randomness. (If none of the installed providers supply an implementation of SecureRandom, a system-provided source of randomness will be used.)

Parameters
params:AlgorithmParameterSpec

the key generation parameters

Exceptions
InvalidAlgorithmParameterException:
if the given parameters are inappropriate for this key generator
initback to summary
public final void init(AlgorithmParameterSpec params, SecureRandom random) throws InvalidAlgorithmParameterException

Initializes this key generator with the specified parameter set and a user-provided source of randomness.

Parameters
params:AlgorithmParameterSpec

the key generation parameters

random:SecureRandom

the source of randomness for this key generator

Exceptions
InvalidAlgorithmParameterException:
if params is inappropriate for this key generator
initback to summary
public final void init(int keysize)

Initializes this key generator for a certain keysize.

If this key generator requires any random bytes, it will get them using the java.security.SecureRandom implementation of the highest-priority installed provider as the source of randomness. (If none of the installed providers supply an implementation of SecureRandom, a system-provided source of randomness will be used.)

Parameters
keysize:int

the keysize. This is an algorithm-specific metric, specified in number of bits.

Exceptions
InvalidParameterException:
if the keysize is wrong or not supported.
initback to summary
public final void init(int keysize, SecureRandom random)

Initializes this key generator for a certain keysize, using a user-provided source of randomness.

Parameters
keysize:int

the keysize. This is an algorithm-specific metric, specified in number of bits.

random:SecureRandom

the source of randomness for this key generator

Exceptions
InvalidParameterException:
if the keysize is wrong or not supported.
nextSpiback to summary
private KeyGeneratorSpi nextSpi(KeyGeneratorSpi oldSpi, boolean reinit)

Update the active spi of this class and return the next implementation for failover. If no more implementations are available, this method returns null. However, the active spi of this class is never set to null.