javax.crypto

Class KeyGenerator

  1. java.lang.Object
  2. extended byjavax.crypto.KeyGenerator

  1. public class KeyGenerator
  2. extends java.lang.Object
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.

Since:
1.4
Version:
1.49, 01/06/04
Author:
Jan Luehe
See Also:
SecretKey

Constructor Summary

Modifier Constructor and Description
  1. protected
KeyGenerator(KeyGeneratorSpi keyGenSpi,java.security.Provider provider,java.lang.String algorithm)
Creates a KeyGenerator object.

Method Summary

Modifier and Type Method and Description
  1. SecretKey
generateKey()
Generates a secret key.
  1. java.lang.String
getAlgorithm()
Returns the algorithm name of this KeyGenerator object.
  1. static
  2. KeyGenerator
getInstance(java.lang.String algorithm)
Returns a KeyGenerator object that generates secret keys for the specified algorithm.
  1. static
  2. KeyGenerator
getInstance(java.lang.String algorithm,java.security.Provider provider)
Returns a KeyGenerator object that generates secret keys for the specified algorithm.
  1. static
  2. KeyGenerator
getInstance(java.lang.String algorithm,java.lang.String provider)
Returns a KeyGenerator object that generates secret keys for the specified algorithm.
  1. java.security.Provider
getProvider()
Returns the provider of this KeyGenerator object.
  1. void
init(java.security.spec.AlgorithmParameterSpec params)
Initializes this key generator with the specified parameter set.
  1. void
init(java.security.spec.AlgorithmParameterSpec params,java.security.SecureRandom random)
Initializes this key generator with the specified parameter set and a user-provided source of randomness.
  1. void
init(int keysize)
Initializes this key generator for a certain keysize.
  1. void
init(int keysize,java.security.SecureRandom random)
Initializes this key generator for a certain keysize, using a user-provided source of randomness.
  1. void
init(java.security.SecureRandom random)
Initializes this key generator.
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructor Detail

KeyGenerator

  1. protected KeyGenerator(KeyGeneratorSpi keyGenSpi,
  2. java.security.Provider provider,
  3. java.lang.String algorithm)
Creates a KeyGenerator object.
Parameters:
keyGenSpi - the delegate
provider - the provider
algorithm - the algorithm

Method Detail

getAlgorithm

  1. public final java.lang.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:
the algorithm name of this KeyGenerator object.

getInstance

  1. public static final KeyGenerator getInstance( java.lang.String algorithm)
  2. throws java.security.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.

Parameters:
algorithm - the standard name of the requested key algorithm. See Appendix A in the Java Cryptography Extension Reference Guide for information about standard algorithm names.
Returns:
the new KeyGenerator object.
Throws:
java.lang.NullPointerException - if the specified algorithm is null.
java.security.NoSuchAlgorithmException - if no Provider supports a KeyGeneratorSpi implementation for the specified algorithm.
See Also:
Provider

getInstance

  1. public static final KeyGenerator getInstance( java.lang.String algorithm,
  2. java.lang.String provider)
  3. throws java.security.NoSuchAlgorithmException
  4. java.security.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 - the standard name of the requested key algorithm. See Appendix A in the Java Cryptography Extension Reference Guide for information about standard algorithm names.
provider - the name of the provider.
Returns:
the new KeyGenerator object.
Throws:
java.lang.NullPointerException - if the specified algorithm is null.
java.security.NoSuchAlgorithmException - if a KeyGeneratorSpi implementation for the specified algorithm is not available from the specified provider.
java.security.NoSuchProviderException - if the specified provider is not registered in the security provider list.
java.lang.IllegalArgumentException - if the provider is null or empty.
See Also:
Provider

getInstance

  1. public static final KeyGenerator getInstance( java.lang.String algorithm,
  2. java.security.Provider provider)
  3. throws java.security.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 - the standard name of the requested key algorithm. See Appendix A in the Java Cryptography Extension Reference Guide for information about standard algorithm names.
provider - the provider.
Returns:
the new KeyGenerator object.
Throws:
java.lang.NullPointerException - if the specified algorithm is null.
java.security.NoSuchAlgorithmException - if a KeyGeneratorSpi implementation for the specified algorithm is not available from the specified Provider object.
java.lang.IllegalArgumentException - if the provider is null.
See Also:
Provider

getProvider

  1. public final java.security.Provider getProvider( )
Returns the provider of this KeyGenerator object.
Returns:
the provider of this KeyGenerator object

init

  1. public final void init(java.security.SecureRandom random)
Initializes this key generator.
Parameters:
random - the source of randomness for this generator

init

  1. public final void init(java.security.spec.AlgorithmParameterSpec params)
  2. throws java.security.InvalidAlgorithmParameterException
Initializes this key generator with the specified parameter set.

If this key generator requires any random bytes, it will get them using the 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 - the key generation parameters
Throws:
java.security.InvalidAlgorithmParameterException - if the given parameters are inappropriate for this key generator

init

  1. public final void init(java.security.spec.AlgorithmParameterSpec params,
  2. java.security.SecureRandom random)
  3. throws java.security.InvalidAlgorithmParameterException
Initializes this key generator with the specified parameter set and a user-provided source of randomness.
Parameters:
params - the key generation parameters
random - the source of randomness for this key generator
Throws:
java.security.InvalidAlgorithmParameterException - if params is inappropriate for this key generator

init

  1. 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 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 - the keysize. This is an algorithm-specific metric, specified in number of bits.
Throws:
java.security.InvalidParameterException - if the keysize is wrong or not supported.

init

  1. public final void init(int keysize,
  2. java.security.SecureRandom random)
Initializes this key generator for a certain keysize, using a user-provided source of randomness.
Parameters:
keysize - the keysize. This is an algorithm-specific metric, specified in number of bits.
random - the source of randomness for this key generator
Throws:
java.security.InvalidParameterException - if the keysize is wrong or not supported.

generateKey

  1. public final SecretKey generateKey( )
Generates a secret key.
Returns:
the new key