Name

KeyGenerator

Synopsis

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 ...

Get Java in a Nutshell, 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.