javax.crypto

Class 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
SecretKeygenerateKey()
Generate a key.
StringgetAlgorithm()
Return the name of this key generator.
static KeyGeneratorgetInstance(String algorithm)
Create a new key generator, returning the first available implementation.
static KeyGeneratorgetInstance(String algorithm, String provider)
Create a new key generator from the named provider.
static KeyGeneratorgetInstance(String algorithm, Provider provider)
Create a new key generator from the supplied provider.
ProvidergetProvider()
Return the provider of the underlying implementation.
voidinit(AlgorithmParameterSpec params)
Initialize this key generator with a set of parameters; the highest-priority {@link java.security.SecureRandom} implementation will be used.
voidinit(AlgorithmParameterSpec params, SecureRandom random)
Initialize this key generator with a set of parameters and a source of randomness.
voidinit(int keySize)
Initialize this key generator with a key size (in bits); the highest-priority {@link java.security.SecureRandom} implementation will be used.
voidinit(int keySize, SecureRandom random)
Initialize this key generator with a key size (in bits) and a source of randomness.
voidinit(SecureRandom random)
Initialize this key generator with a source of randomness.

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

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.

getInstance

public static final KeyGenerator getInstance(String algorithm)
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)
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)
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.

getProvider

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

Returns: The provider.

init

public final void init(AlgorithmParameterSpec params)
Initialize this key generator with a set of parameters; the highest-priority {@link java.security.SecureRandom} implementation will be used.

Parameters: params The algorithm parameters.

Throws: java.security.InvalidAlgorithmParameterException If the supplied parameters are inapproprate.

init

public final void init(AlgorithmParameterSpec params, SecureRandom random)
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: java.security.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 {@link java.security.SecureRandom} implementation will be used.

Parameters: keySize The target key size, in bits.

Throws: java.security.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: java.security.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.