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:
All key generators share the concepts of a keysize and a
source of randomness.
There is an
init
method in this KeyGenerator class that takes these two universally
shared types of arguments. There is also one that takes just a
keysize argument, and uses the SecureRandom implementation
of the highest-priority installed provider as the source of randomness
(or a system-provided source of randomness if none of the installed
providers supply a SecureRandom implementation), and one that takes just a
source of randomness.
Since no other parameters are specified when you call the above
algorithm-independent init methods, it is up to the
provider what to do about the algorithm-specific parameters (if any) to be
associated with each of the keys.
For situations where a set of algorithm-specific parameters already
exists, there are two
init
methods that have an AlgorithmParameterSpec
argument. One also has a SecureRandom argument, while the
other uses the SecureRandom implementation
of the highest-priority installed provider as the source of randomness
(or a system-provided source of randomness if none of the installed
providers supply a SecureRandom implementation).
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.
See the Keysize Restriction sections of the
JDK Providers
document for information on the KeyGenerator defaults used by
JDK providers.
However, note that defaults may vary across different providers.
Additionally, the default value for a provider may change in a future
version. Therefore, it is recommended to explicitly initialize the
KeyGenerator instead of relying on provider-specific defaults.
Every implementation of the Java platform is required to support the
following standard KeyGenerator algorithms with the keysizes in
parentheses:
AES (128, 256)ChaCha20HmacSHA1HmacSHA256SecretKey
| Modifier and Type | Field and Description |
|---|---|
| private final String | |
| private static final int | |
| private static final int | |
| private static final int | |
| private static final int | |
| private int | |
| private AlgorithmParameterSpec | |
| private SecureRandom | |
| private int | |
| private final Object | |
| private static final Debug | |
| private Provider | |
| private Iterator | |
| private static final boolean | |
| private volatile KeyGeneratorSpi |
| Access | Constructor and Description |
|---|---|
| protected | KeyGenerator(KeyGeneratorSpi
the delegate keyGenSpi, Provider the provider provider, String the algorithm algorithm)Creates a |
| private |
| Modifier and Type | Method and Description |
|---|---|
| pack-priv void | |
| public final SecretKey | |
| public final String | Returns: the algorithm name of thisKeyGenerator object.Returns the algorithm name of this |
| public static final KeyGenerator | Returns: the newKeyGenerator objectthe standard name of the requested key algorithm.
See the KeyGenerator section in the
Java Security Standard Algorithm Names Specification
for information about standard algorithm names. algorithm)Returns a |
| public static final KeyGenerator | Returns: the newKeyGenerator objectthe standard name of the requested key algorithm.
See the KeyGenerator section in the
Java Security Standard Algorithm Names Specification
for information about standard algorithm names. algorithm, String the name of the provider. provider)Returns a |
| public static final KeyGenerator | Returns: the newKeyGenerator objectthe standard name of the requested key algorithm.
See the KeyGenerator section in the
Java Security Standard Algorithm Names Specification
for information about standard algorithm names. algorithm, Provider the provider. provider)Returns a |
| public final Provider | Returns: the provider of thisKeyGenerator objectReturns the provider of this |
| private String | |
| public final void | |
| public final void | init(AlgorithmParameterSpec
the key generation parameters params)Initializes this key generator with the specified parameter set. |
| public final void | init(AlgorithmParameterSpec
the key generation parameters params, SecureRandom the source of randomness for this key generator random)Initializes this key generator with the specified parameter set and a user-provided source of randomness. |
| public final void | init(int
the keysize. This is an algorithm-specific metric,
specified in number of bits. keysize)Initializes this key generator for a certain keysize. |
| public final void | init(int
the keysize. This is an algorithm-specific metric,
specified in number of bits. keysize, SecureRandom the source of randomness for this key generator random)Initializes this key generator for a certain keysize, using a user-provided source of randomness. |
| private KeyGeneratorSpi | nextSpi(KeyGeneratorSpi oldSpi, boolean reinit)
Update the active spi of this class and return the next implementation for failover. |
| algorithm | back to summary |
|---|---|
| private final String algorithm | |
| I_NONE | back to summary |
|---|---|
| private static final int I_NONE | |
| I_PARAMS | back to summary |
|---|---|
| private static final int I_PARAMS | |
| I_RANDOM | back to summary |
|---|---|
| private static final int I_RANDOM | |
| I_SIZE | back to summary |
|---|---|
| private static final int I_SIZE | |
| initKeySize | back to summary |
|---|---|
| private int initKeySize | |
| initParams | back to summary |
|---|---|
| private AlgorithmParameterSpec initParams | |
| initRandom | back to summary |
|---|---|
| private SecureRandom initRandom | |
| initType | back to summary |
|---|---|
| private int initType | |
| lock | back to summary |
|---|---|
| private final Object lock | |
| pdebug | back to summary |
|---|---|
| private static final Debug pdebug | |
| provider | back to summary |
|---|---|
| private Provider provider | |
| serviceIterator | back to summary |
|---|---|
| private Iterator<Provider. | |
| skipDebug | back to summary |
|---|---|
| private static final boolean skipDebug | |
| spi | back to summary |
|---|---|
| private volatile KeyGeneratorSpi spi | |
| KeyGenerator | back to summary |
|---|---|
| protected KeyGenerator(KeyGeneratorSpi keyGenSpi, Provider provider, String algorithm) Creates a
| |
| KeyGenerator | back to summary |
|---|---|
| private KeyGenerator(String algorithm) throws NoSuchAlgorithmException | |
| disableFailover | back to summary |
|---|---|
| pack-priv void disableFailover() | |
| generateKey | back to summary |
|---|---|
| public final SecretKey generateKey() Generates a secret key.
| |
| getAlgorithm | back to summary |
|---|---|
| public final String getAlgorithm() Returns the algorithm name of this This is the same name that was specified in one of the
| |
| getInstance | back to summary |
|---|---|
| public static final KeyGenerator getInstance(String algorithm) throws NoSuchAlgorithmException Returns a This method traverses the list of registered security providers,
starting with the most preferred provider.
A new Note that the list of registered providers may be retrieved via
the Implementation Note The JDK Reference Implementation additionally uses the
| |
| getInstance | back to summary |
|---|---|
| public static final KeyGenerator getInstance(String algorithm, String provider) throws NoSuchAlgorithmException, NoSuchProviderException Returns a A new Note that the list of registered providers may be retrieved via
the
| |
| getInstance | back to summary |
|---|---|
| public static final KeyGenerator getInstance(String algorithm, Provider provider) throws NoSuchAlgorithmException Returns a A new
| |
| getProvider | back to summary |
|---|---|
| public final Provider getProvider() Returns the provider of this
| |
| getProviderName | back to summary |
|---|---|
| private String getProviderName() | |
| init | back to summary |
|---|---|
| public final void init(SecureRandom random) Initializes this key generator.
| |
| init | back to summary |
|---|---|
| public final void init(AlgorithmParameterSpec params) throws InvalidAlgorithmParameterException Initializes this key generator with the specified parameter set. If this key generator requires any random bytes, it will get them
using the
| |
| init | back to summary |
|---|---|
| public final void init(AlgorithmParameterSpec params, SecureRandom random) throws InvalidAlgorithmParameterException Initializes this key generator with the specified parameter set and a user-provided source of randomness.
| |
| init | back to summary |
|---|---|
| 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
| |
| init | back to summary |
|---|---|
| public final void init(int keysize, SecureRandom random) Initializes this key generator for a certain keysize, using a user-provided source of randomness.
| |
| nextSpi | back to summary |
|---|---|
| private KeyGeneratorSpi nextSpi(KeyGeneratorSpi oldSpi, boolean reinit) Update the active spi of this class and return the next
implementation for failover. If no more implementations are
available, this method returns | |