This class provides an API for
generating secret keys for symmetric cryptography. It is similar to
java.security.KeyPairGenerator, which generates
public/private key pairs for asymmetric or public-key cryptography.
KeyGenerator is algorithm-independent and
provider-based, so you must obtain a KeyGenerator
instance by calling one of the static getInstance(
) factory methods and specifying the name of the
cryptographic algorithm for which a key is desired and, optionally,
the name of the security provider whose key-generation implementation
is to be used. In Java 5.0 the
"SunJCE" provider includes
KeyGenerator implementations algorithms with the
following names:
AES
|
DESede
|
HmacSHA384
|
ARCFOUR
|
HmacMD5
|
HmacSHA512
|
Blowfish
|
HmacSHA1
|
RC2
|
DES
|
HmacSHA256
| |
Once you have obtained a
KeyGenerator, you initialize it with the
init( ) method. You can provide a
java.security.spec.AlgorithmParameterSpec object
to provide algorithm-specific initialization parameters or simply
specify the desired size (in bits) of the key to be generated. In
either case, you can also specify a source of randomness in the form
of a SecureRandom object. If you do not specify a
SecureRandom, the KeyGenerator
instantiates one of its own. None of the algorithms supported by the
"SunJCE" provider require
algorithm-specific parameters.
After calling
getInstance( ) to obtain a
KeyGenerator and init( ) to
initialize it, simply call generateKey( ) to
create a new SecretKey. Remember that the
SecretKey must be kept secret. Take precautions
when storing or transmitting the key, so that it does not fall into
the wrong hands. You may want to use a
java.security.KeyStore object to store the key in
a password-protected form.
public class KeyGenerator {
// Protected Constructors
protected KeyGenerator(KeyGeneratorSpi keyGenSpi, java.security.Provider provider,
String algorithm);
// Public Class Methods
public static final KeyGenerator getInstance(String algorithm)
throws java.security.NoSuchAlgorithmException;
public static final KeyGenerator getInstance(String algorithm,
java.security.Provider provider) throws java.security.NoSuchAlgorithmException;
public static final KeyGenerator getInstance(String algorithm,
String provider) throws java.security.NoSuchAlgorithmException,
java.security.NoSuchProviderException;
// Public Instance Methods
public final SecretKey generateKey( );
public final String getAlgorithm( );
public final java.security.Provider getProvider( );
public final void init(int keysize);
public final void init(java.security.spec.AlgorithmParameterSpec params)
throws java.security.InvalidAlgorithmParameterException;
public final void init(java.security.SecureRandom random);
public final void init(int keysize, java.security.SecureRandom random);
public final void init(java.security.spec.AlgorithmParameterSpec params,
java.security.SecureRandom random)
throws java.security.InvalidAlgorithmParameterException;
}