javax.crypto
Class KeyGenerator

java.lang.Object
  extended by javax.crypto.KeyGenerator

public class KeyGenerator
extends Object

Engine class for creating secret keys in algorithm independent manner.


Attention:  This is not a SUN implementation!

This class has been developed by IAIK according to the documentation publicly available.
For SUNīs documentation of this class see http://java.sun.com/security/JCE1.2/spec/apidoc/index.html


This class represents a key generator for creating secret keys for symmetric algorithms. For creating a KeyGenerator object, an application shall use one of the getInstance methods, supplied with the particular symmetric algorithm in mind and (optionally) a particular provider.

For instance:

 KeyGenerator key_gen = KeyGenerator.getInstance("DES");
 

For actually generating the requested secret key from the KeyGenerator object just created (and initialized with one of the init methods), use the generateKey method:

For instance:

 SecretKey des_key = key_gen.generateKey();
 

Initializing the key generator maybe done either algorithm-independently with some random seed, or algorithm-specific with some particular algorithm parameter specification. If no explicit initialization is done at all, it is left to the actual provider implementation to take default settings.

Version:
File Revision 15
See Also:
getInstance(java.lang.String), generateKey(), SecretKey, SecretKey

Constructor Summary
protected KeyGenerator(KeyGeneratorSpi keyGenSpi, Provider provider, String algorithm)
          Default Constructor for creating a KeyGenerator object.
 
Method Summary
 SecretKey generateKey()
          Actually generates a secret key from this KeyGenerator object.
 String getAlgorithm()
          Returns the standard name of the key algorithm.
static KeyGenerator getInstance(String algorithm)
          Returns the default provider KeyGenerator implementation for the specified algorithm.
static KeyGenerator getInstance(String algorithm, String provider)
          Returns a KeyGenerator object for the specified algorithm, implemented by the given provider.
 Provider getProvider()
          Returns the provider used for creating this KeyGenerator object.
 void init(AlgorithmParameterSpec params)
          Initializes this key generator with the given algorithm parameters.
 void init(AlgorithmParameterSpec params, SecureRandom random)
          Initializes this key generator with given algorithm parameters and random seed.
 void init(int strength)
          Initializes this key generator for the given strength.
 void init(int strength, SecureRandom random)
          Initializes this key generator for the given strength with the given random seed
 void init(SecureRandom random)
          Initializes this key generator with the given random seed.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

KeyGenerator

protected KeyGenerator(KeyGeneratorSpi keyGenSpi,
                       Provider provider,
                       String algorithm)
Default Constructor for creating a KeyGenerator object. Only internally used for creating a KeyGenerator object. Applications shall use one of the getInstance factory methods for obtaining a KeyGenerator object.

Parameters:
keyGenSpi - the SPI KeyGenerator implementation of the actual provider
provider - the provider
algorithm - the algorithm
Method Detail

getAlgorithm

public final String getAlgorithm()
Returns the standard name of the key algorithm.

Returns:
the standard name of the algorithm as string.

getInstance

public static final KeyGenerator getInstance(String algorithm)
                                      throws NoSuchAlgorithmException
Returns the default provider KeyGenerator implementation for the specified algorithm.

Parameters:
algorithm - the standard name of the algorithm for which the KeyGenerator is used
Returns:
a KeyGenerator object for this algorithm.
Throws:
NoSuchAlgorithmException - if this algorithm is not implemented

getInstance

public static final KeyGenerator getInstance(String algorithm,
                                             String provider)
                                      throws NoSuchAlgorithmException,
                                             NoSuchProviderException
Returns a KeyGenerator object for the specified algorithm, implemented by the given provider.

Parameters:
algorithm - the standard name of algorithm for which the KeyGenerator is used
provider - the name of the provider as string.
Returns:
a KeyGenerator object for this algorithm
Throws:
NoSuchAlgorithmException - if this algorithm is not implemented by the given provider
NoSuchProviderException - if the given provider cannot be fetched

getProvider

public final Provider getProvider()
Returns the provider used for creating this KeyGenerator object.

Returns:
the provider used for creating this KeyGenerator object

init

public final void init(SecureRandom random)
Initializes this key generator with the given random seed.

Parameters:
random - the random seed

init

public final void init(int strength)
Initializes this key generator for the given strength. Since no random seed is specified, a default seeding mechanism will be used for initializing this key generator.

Parameters:
strength - the strength of the key to be created

init

public final void init(int strength,
                       SecureRandom random)
Initializes this key generator for the given strength with the given random seed

Parameters:
strength - the strength of the key to be created
random - the random seed

init

public final void init(AlgorithmParameterSpec params)
                throws InvalidAlgorithmParameterException
Initializes this key generator with the given algorithm parameters. Since no random seed is specified, a default seeding mechanism will be used for initializing this key generator.

Parameters:
params - the algorithm parameters
Throws:
InvalidAlgorithmParameterException - if the given algorithm parameters do not match to this KeyGenerator

init

public final void init(AlgorithmParameterSpec params,
                       SecureRandom random)
                throws InvalidAlgorithmParameterException
Initializes this key generator with given algorithm parameters and random seed.

Parameters:
params - the algorithm parameters
random - the random seed
Throws:
InvalidAlgorithmParameterException - if the given algorithm parameters do not match to this KeyGenerator

generateKey

public final SecretKey generateKey()
Actually generates a secret key from this KeyGenerator object.

Returns:
the generated secret key

This Javadoc may contain text parts from IETF Internet Standard specifications (see copyright note) and RSA Data Security Public-Key Cryptography Standards (PKCS, see copyright note).

IAIK-JCE 4.0
(c) 2002 IAIK, (c) 2003 - 2010 SIC