javax.crypto
Class KeyGenerator

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

public class KeyGenerator
extends Object

A generic producer of keys for symmetric cryptography. The keys returned may be simple wrappers around byte arrays, or, if the target cipher requires them, more complex objects.

Since:
1.4
See Also:
Cipher, Mac

Constructor Summary
protected KeyGenerator(KeyGeneratorSpi kgSpi, Provider provider, String algorithm)
          Create a new key generator.
 
Method Summary
 SecretKey generateKey()
          Generate a key.
 String getAlgorithm()
          Return the name of this key generator.
static KeyGenerator getInstance(String algorithm)
          Create a new key generator, returning the first available implementation.
static KeyGenerator getInstance(String algorithm, Provider provider)
          Create a new key generator from the supplied provider.
static KeyGenerator getInstance(String algorithm, String provider)
          Create a new key generator from the named provider.
 Provider getProvider()
          Return the provider of the underlying implementation.
 void init(AlgorithmParameterSpec params)
          Initialize this key generator with a set of parameters; the highest-priority SecureRandom implementation will be used.
 void init(AlgorithmParameterSpec params, SecureRandom random)
          Initialize this key generator with a set of parameters and a source of randomness.
 void init(int keySize)
          Initialize this key generator with a key size (in bits); the highest-priority SecureRandom implementation will be used.
 void init(int keySize, SecureRandom random)
          Initialize this key generator with a key size (in bits) and a source of randomness.
 void init(SecureRandom random)
          Initialize this key generator with a source of randomness.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

KeyGenerator

protected KeyGenerator(KeyGeneratorSpi kgSpi,
                       Provider provider,
                       String algorithm)
Create a new key generator.

Parameters:
kgSpi - The underlying generator.
provider - The provider of this implementation.
algorithm - The algorithm's name.
Method Detail

getInstance

public static final KeyGenerator getInstance(String algorithm)
                                      throws NoSuchAlgorithmException
Create a new key generator, returning the first available implementation.

Parameters:
algorithm - The generator algorithm name.
Throws:
NoSuchAlgorithmException - If the specified algorithm does not exist.
IllegalArgumentException - if algorithm is null or is an empty string.

getInstance

public static final KeyGenerator getInstance(String algorithm,
                                             String provider)
                                      throws NoSuchAlgorithmException,
                                             NoSuchProviderException
Create a new key generator from the named provider.

Parameters:
algorithm - The generator algorithm name.
provider - The name of the provider to use.
Returns:
An appropriate key generator, if found.
Throws:
NoSuchAlgorithmException - If the specified algorithm is not implemented by the named provider.
NoSuchProviderException - If the named provider does not exist.
IllegalArgumentException - if either algorithm or provider is null, or if algorithm is an empty string.

getInstance

public static final KeyGenerator getInstance(String algorithm,
                                             Provider provider)
                                      throws NoSuchAlgorithmException
Create a new key generator from the supplied provider.

Parameters:
algorithm - The generator algorithm name.
provider - The provider to use.
Returns:
An appropriate key generator, if found.
Throws:
NoSuchAlgorithmException - If the specified algorithm is not implemented by the provider.
IllegalArgumentException - if either algorithm or provider is null, or if algorithm is an empty string.

generateKey

public final SecretKey generateKey()
Generate a key.

Returns:
The new key.

getAlgorithm

public final String getAlgorithm()
Return the name of this key generator.

Returns:
The algorithm name.

getProvider

public final Provider getProvider()
Return the provider of the underlying implementation.

Returns:
The provider.

init

public final void init(AlgorithmParameterSpec params)
                throws InvalidAlgorithmParameterException
Initialize this key generator with a set of parameters; the highest-priority SecureRandom implementation will be used.

Parameters:
params - The algorithm parameters.
Throws:
InvalidAlgorithmParameterException - If the supplied parameters are inapproprate.

init

public final void init(AlgorithmParameterSpec params,
                       SecureRandom random)
                throws InvalidAlgorithmParameterException
Initialize this key generator with a set of parameters and a source of randomness.

Parameters:
params - The algorithm parameters.
random - The source of randomness.
Throws:
InvalidAlgorithmParameterException - If the supplied parameters are inapproprate.

init

public final void init(int keySize)
Initialize this key generator with a key size (in bits); the highest-priority SecureRandom implementation will be used.

Parameters:
keySize - The target key size, in bits.
Throws:
InvalidParameterException - If the key size is unsupported.

init

public final void init(int keySize,
                       SecureRandom random)
Initialize this key generator with a key size (in bits) and a source of randomness.

Parameters:
keySize - The target key size, in bits.
random - The source of randomness.
Throws:
InvalidAlgorithmParameterException - If the key size is unsupported.

init

public final void init(SecureRandom random)
Initialize this key generator with a source of randomness. The implementation-specific default parameters (such as key size) will be used.

Parameters:
random - The source of randomness.