Package javax.crypto
Class KeyGenerator
java.lang.Object
javax.crypto.KeyGenerator
public class KeyGenerator extends Object
This class provides the public API for generating symmetric cryptographic
keys.
-
Constructor Summary
Constructors Modifier Constructor Description protectedKeyGenerator(KeyGeneratorSpi keyGenSpi, Provider provider, String algorithm)Creates a newKeyGeneratorinstance. -
Method Summary
Modifier and Type Method Description SecretKeygenerateKey()Generates a secret key.StringgetAlgorithm()Returns the name of the key generation algorithm.static KeyGeneratorgetInstance(String algorithm)Creates a newKeyGeneratorinstance that provides the specified key algorithm,static KeyGeneratorgetInstance(String algorithm, String provider)Creates a newKeyGeneratorinstance that provides the specified key algorithm from the specified provider.static KeyGeneratorgetInstance(String algorithm, Provider provider)Creates a newKeyGeneratorinstance that provides the specified key algorithm from the specified provider.ProvidergetProvider()Returns the provider of thisKeyGeneratorinstance.voidinit(int keysize)Initializes thisKeyGeneratorinstance for the specified key size (in bits).voidinit(int keysize, SecureRandom random)Initializes thisKeyGeneratorinstance for the specified key size (in bits) using the specified randomness source.voidinit(SecureRandom random)Initializes thisKeyGeneratorwith the specified randomness source.voidinit(AlgorithmParameterSpec params)Initializes thisKeyGeneratorinstance with the specified algorithm parameters.voidinit(AlgorithmParameterSpec params, SecureRandom random)Initializes thisKeyGeneratorinstance with the specified algorithm parameters and randomness source.
-
Constructor Details
-
KeyGenerator
Creates a newKeyGeneratorinstance.- Parameters:
keyGenSpi- the implementation delegate.provider- the implementation provider.algorithm- the name of the algorithm.
-
-
Method Details
-
getAlgorithm
Returns the name of the key generation algorithm.- Returns:
- the name of the key generation algorithm.
-
getProvider
Returns the provider of thisKeyGeneratorinstance.- Returns:
- the provider of this
KeyGeneratorinstance.
-
getInstance
Creates a newKeyGeneratorinstance that provides the specified key algorithm,- Parameters:
algorithm- the name of the requested key algorithm- Returns:
- the new
KeyGeneratorinstance. - Throws:
NoSuchAlgorithmException- if the specified algorithm is not available by any provider.NullPointerException- ifalgorithmisnull.
-
getInstance
public static final KeyGenerator getInstance(String algorithm, String provider) throws NoSuchAlgorithmException, NoSuchProviderExceptionCreates a newKeyGeneratorinstance that provides the specified key algorithm from the specified provider.- Parameters:
algorithm- the name of the requested key algorithm.provider- the name of the provider that is providing the algorithm.- Returns:
- the new
KeyGeneratorinstance. - Throws:
NoSuchAlgorithmException- if the specified algorithm is not provided by the specified provider.NoSuchProviderException- if the specified provider is not available.IllegalArgumentException- if the specified provider is name isnullor empty.NullPointerException- if the specified algorithm name isnull.
-
getInstance
public static final KeyGenerator getInstance(String algorithm, Provider provider) throws NoSuchAlgorithmExceptionCreates a newKeyGeneratorinstance that provides the specified key algorithm from the specified provider.- Parameters:
algorithm- the name of the requested key algorithm.provider- the provider that is providing the algorithm- Returns:
- the new
KeyGeneratorinstance. - Throws:
NoSuchAlgorithmException- if the specified algorithm is not provided by the specified provider.IllegalArgumentException- if the specified provider isnull.NullPointerException- if the specified algorithm name isnull.
-
generateKey
Generates a secret key.- Returns:
- the generated secret key.
-
init
Initializes thisKeyGeneratorinstance with the specified algorithm parameters.- Parameters:
params- the parameters for the key generation algorithm.- Throws:
InvalidAlgorithmParameterException- if the parameters cannot be used to initialize this key generator algorithm.
-
init
public final void init(AlgorithmParameterSpec params, SecureRandom random) throws InvalidAlgorithmParameterExceptionInitializes thisKeyGeneratorinstance with the specified algorithm parameters and randomness source.- Parameters:
params- the parameters for the key generation algorithm.random- the randomness source for any random bytes.- Throws:
InvalidAlgorithmParameterException- if the parameters cannot be uses to initialize this key generator algorithm.
-
init
public final void init(int keysize)Initializes thisKeyGeneratorinstance for the specified key size (in bits).- Parameters:
keysize- the size of the key (in bits).
-
init
Initializes thisKeyGeneratorinstance for the specified key size (in bits) using the specified randomness source.- Parameters:
keysize- the size of the key (in bits).random- the randomness source for any random bytes.
-
init
Initializes thisKeyGeneratorwith the specified randomness source.- Parameters:
random- the randomness source for any random bytes.
-