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

KeyGenerator(KeyGeneratorSpi kgSpi, Provider provider, String algorithm)
Create a new key generator.

Method Summary

SecretKey
generateKey()
Generate a key.
String
getAlgorithm()
Return the name of this key generator.
static KeyGenerator
getInstance(String algorithm)
Create a new key generator, returning the first available implementation.
static KeyGenerator
getInstance(String algorithm, String provider)
Create a new key generator from the named provider.
static KeyGenerator
getInstance(String algorithm, Provider provider)
Create a new key generator from the supplied provider.
Provider
getProvider()
Return the provider of the underlying implementation.
void
init(int keySize)
Initialize this key generator with a key size (in bits); the highest-priority SecureRandom implementation will be used.
void
init(int keySize, SecureRandom random)
Initialize this key generator with a key size (in bits) and a source of randomness.
void
init(SecureRandom random)
Initialize this key generator with a source of randomness.
void
init(AlgorithmParameterSpec params)
Initialize this key generator with a set of parameters; the highest-priority SecureRandom implementation will be used.
void
init(AlgorithmParameterSpec params, SecureRandom random)
Initialize this key generator with a set of parameters and a source of randomness.

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructor Details

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 Details

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)
            throws NoSuchAlgorithmException
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)
            throws NoSuchAlgorithmException,
                   NoSuchProviderException
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)
            throws NoSuchAlgorithmException
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(int keySize)
Initialize this key generator with a key size (in bits); the highest-priority SecureRandom implementation will be used.
Parameters:
keySize - The target key size, in bits.
Throws:
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.

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.

init

public final void init(AlgorithmParameterSpec params)
            throws InvalidAlgorithmParameterException
Initialize this key generator with a set of parameters; the highest-priority SecureRandom implementation will be used.
Parameters:
params - The algorithm parameters.
Throws:
InvalidAlgorithmParameterException - If the supplied parameters are inapproprate.

init

public final void init(AlgorithmParameterSpec params,
                       SecureRandom random)
            throws InvalidAlgorithmParameterException
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:
InvalidAlgorithmParameterException - If the supplied parameters are inapproprate.

KeyGenerator.java -- Interface to a symmetric key generator. Copyright (C) 2004 Free Software Foundation, Inc. This file is part of GNU Classpath. GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNU Classpath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Classpath; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version.