# KeyGenerator

public class **KeyGenerator** extends [Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html)  
This class provides the functionality of a secret (symmetric) key generator.

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:

- **Algorithm-Independent Initialization**

  All key generators share the concepts of a *keysize* and a
  *source of randomness*.
  There is an
  `https://developers.google.com/j2objc/javadoc/jre/reference/javax/crypto/KeyGenerator.html#init(int,%20java.security.SecureRandom)`
  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.

- **Algorithm-Specific Initialization**

  For situations where a set of algorithm-specific parameters already
  exists, there are two
  `https://developers.google.com/j2objc/javadoc/jre/reference/javax/crypto/KeyGenerator.html#init(java.security.spec.AlgorithmParameterSpec)`
  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.

Android provides the following `KeyGenerator` algorithms:

| Algorithm | Supported API Levels |
|---|---|
| AES | 1+ |
| AESWRAP | 1-8 |
| ARC4 | 14+ |
| BLOWFISH | 10+ |
| ChaCha20 | 28+ |
| DES | 1+ |
| DESede | 1+ |
| DESedeWRAP | 1-8 |
| HmacMD5 | 1+ |
| HmacSHA1 | 11+ |
| HmacSHA224 | 1-8,22+ |
| HmacSHA256 | 1+ |
| HmacSHA384 | 1+ |
| HmacSHA512 | 1+ |
| RC4 | 10-13 |

These algorithms are described in the [KeyGenerator section](https://developers.google.com/j2objc/javadoc/jre/technotes/guides/security/StandardNames.html#KeyGenerator) of the Java Cryptography Architecture Standard Algorithm Name Documentation.

<br />

##### See Also

- `https://developers.google.com/j2objc/javadoc/jre/reference/javax/crypto/SecretKey.html`

### Protected Constructor Summary

|---|---|
|   | [KeyGenerator](https://developers.google.com/j2objc/javadoc/jre/reference/javax/crypto/KeyGenerator.html#KeyGenerator(javax.crypto.KeyGeneratorSpi,%20java.security.Provider,%20java.lang.String))([KeyGeneratorSpi](https://developers.google.com/j2objc/javadoc/jre/reference/javax/crypto/KeyGeneratorSpi.html) keyGenSpi, [Provider](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/Provider.html) provider, [String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html) algorithm) Creates a KeyGenerator object. |

### Public Method Summary

|---|---|
| final [SecretKey](https://developers.google.com/j2objc/javadoc/jre/reference/javax/crypto/SecretKey.html) | [generateKey](https://developers.google.com/j2objc/javadoc/jre/reference/javax/crypto/KeyGenerator.html#generateKey())() Generates a secret key. |
| final [String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html) | [getAlgorithm](https://developers.google.com/j2objc/javadoc/jre/reference/javax/crypto/KeyGenerator.html#getAlgorithm())() Returns the algorithm name of this `KeyGenerator` object. |
| final static [KeyGenerator](https://developers.google.com/j2objc/javadoc/jre/reference/javax/crypto/KeyGenerator.html) | [getInstance](https://developers.google.com/j2objc/javadoc/jre/reference/javax/crypto/KeyGenerator.html#getInstance(java.lang.String))([String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html) algorithm) Returns a `KeyGenerator` object that generates secret keys for the specified algorithm. |
| final static [KeyGenerator](https://developers.google.com/j2objc/javadoc/jre/reference/javax/crypto/KeyGenerator.html) | [getInstance](https://developers.google.com/j2objc/javadoc/jre/reference/javax/crypto/KeyGenerator.html#getInstance(java.lang.String,%20java.lang.String))([String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html) algorithm, [String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html) provider) Returns a `KeyGenerator` object that generates secret keys for the specified algorithm. |
| final static [KeyGenerator](https://developers.google.com/j2objc/javadoc/jre/reference/javax/crypto/KeyGenerator.html) | [getInstance](https://developers.google.com/j2objc/javadoc/jre/reference/javax/crypto/KeyGenerator.html#getInstance(java.lang.String,%20java.security.Provider))([String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html) algorithm, [Provider](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/Provider.html) provider) Returns a `KeyGenerator` object that generates secret keys for the specified algorithm. |
| final [Provider](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/Provider.html) | [getProvider](https://developers.google.com/j2objc/javadoc/jre/reference/javax/crypto/KeyGenerator.html#getProvider())() Returns the provider of this `KeyGenerator` object. |
| final void | [init](https://developers.google.com/j2objc/javadoc/jre/reference/javax/crypto/KeyGenerator.html#init(java.security.spec.AlgorithmParameterSpec,%20java.security.SecureRandom))([AlgorithmParameterSpec](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/spec/AlgorithmParameterSpec.html) params, [SecureRandom](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/SecureRandom.html) random) Initializes this key generator with the specified parameter set and a user-provided source of randomness. |
| final void | [init](https://developers.google.com/j2objc/javadoc/jre/reference/javax/crypto/KeyGenerator.html#init(java.security.SecureRandom))([SecureRandom](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/SecureRandom.html) random) Initializes this key generator. |
| final void | [init](https://developers.google.com/j2objc/javadoc/jre/reference/javax/crypto/KeyGenerator.html#init(int))(int keysize) Initializes this key generator for a certain keysize. |
| final void | [init](https://developers.google.com/j2objc/javadoc/jre/reference/javax/crypto/KeyGenerator.html#init(int,%20java.security.SecureRandom))(int keysize, [SecureRandom](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/SecureRandom.html) random) Initializes this key generator for a certain keysize, using a user-provided source of randomness. |
| final void | [init](https://developers.google.com/j2objc/javadoc/jre/reference/javax/crypto/KeyGenerator.html#init(java.security.spec.AlgorithmParameterSpec))([AlgorithmParameterSpec](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/spec/AlgorithmParameterSpec.html) params) Initializes this key generator with the specified parameter set. |

### Inherited Method Summary

From class [java.lang.Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html)

|---|---|
| [Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html) | [clone](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#clone())() Creates and returns a copy of this `Object`. |
| boolean | [equals](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#equals(java.lang.Object))([Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html) obj) Compares this instance with the specified object and indicates if they are equal. |
| void | [finalize](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#finalize())() Invoked when the garbage collector has detected that this instance is no longer reachable. |
| final [Class](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Class.html)\<?\> | [getClass](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#getClass())() Returns the unique instance of `https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Class.html` that represents this object's class. |
| int | [hashCode](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#hashCode())() Returns an integer hash code for this object. |
| final void | [notify](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#notify())() Causes a thread which is waiting on this object's monitor (by means of calling one of the `wait()` methods) to be woken up. |
| final void | [notifyAll](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#notifyAll())() Causes all threads which are waiting on this object's monitor (by means of calling one of the `wait()` methods) to be woken up. |
| [String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html) | [toString](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#toString())() Returns a string containing a concise, human-readable description of this object. |
| final void | [wait](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#wait(long,%20int))(long timeout, int nanos) Causes the calling thread to wait until another thread calls the `notify()` or `notifyAll()` method of this object or until the specified timeout expires. |
| final void | [wait](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#wait(long))(long timeout) Causes the calling thread to wait until another thread calls the `notify()` or `notifyAll()` method of this object or until the specified timeout expires. |
| final void | [wait](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#wait())() Causes the calling thread to wait until another thread calls the `notify()` or `notifyAll()` method of this object. |

## Protected Constructors

#### protected
**KeyGenerator**
([KeyGeneratorSpi](https://developers.google.com/j2objc/javadoc/jre/reference/javax/crypto/KeyGeneratorSpi.html) keyGenSpi, [Provider](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/Provider.html) provider, [String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html) algorithm)

Creates a KeyGenerator object.

##### Parameters

| keyGenSpi | the delegate |
| provider | the provider |
| algorithm | the algorithm |
|---|---|

## Public Methods

#### public final [SecretKey](https://developers.google.com/j2objc/javadoc/jre/reference/javax/crypto/SecretKey.html)
**generateKey**
()

Generates a secret key.

##### Returns

- the new key

#### public final [String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html)
**getAlgorithm**
()

Returns the algorithm name of this `KeyGenerator` object.

This is the same name that was specified in one of the
`getInstance` calls that created this
`KeyGenerator` object.

##### Returns

- the algorithm name of this `KeyGenerator` object.

#### public static final [KeyGenerator](https://developers.google.com/j2objc/javadoc/jre/reference/javax/crypto/KeyGenerator.html)
**getInstance**
([String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html) algorithm)

Returns a `KeyGenerator` object that generates secret keys
for the specified algorithm.

This method traverses the list of registered security Providers,
starting with the most preferred Provider.
A new KeyGenerator object encapsulating the
KeyGeneratorSpi implementation from the first
Provider that supports the specified algorithm is returned.

Note that the list of registered providers may be retrieved via
the `https://developers.google.com/j2objc/javadoc/jre/reference/java/security/Security.html#getProviders()` method.

##### Parameters

| algorithm | the standard name of the requested key algorithm. See the KeyGenerator section in the [Java Cryptography Architecture Standard Algorithm Name Documentation](https://developers.google.com/j2objc/javadoc/jre/technotes/guides/security/StandardNames.html#KeyGenerator) for information about standard algorithm names. |
|---|---|

##### Returns

- the new `KeyGenerator` object.

##### Throws

| [NullPointerException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/NullPointerException.html) | if the specified algorithm is null. |
| [NoSuchAlgorithmException](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/NoSuchAlgorithmException.html) | if no Provider supports a KeyGeneratorSpi implementation for the specified algorithm. |
|---|---|

##### See Also

- `https://developers.google.com/j2objc/javadoc/jre/reference/java/security/Provider.html`

#### public static final [KeyGenerator](https://developers.google.com/j2objc/javadoc/jre/reference/javax/crypto/KeyGenerator.html)
**getInstance**
([String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html) algorithm, [String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html) provider)

Returns a `KeyGenerator` object that generates secret keys
for the specified algorithm.

A new KeyGenerator object encapsulating the
KeyGeneratorSpi implementation from the specified provider
is returned. The specified provider must be registered
in the security provider list.

Note that the list of registered providers may be retrieved via
the `https://developers.google.com/j2objc/javadoc/jre/reference/java/security/Security.html#getProviders()` method.

##### Parameters

| algorithm | the standard name of the requested key algorithm. See the KeyGenerator section in the [Java Cryptography Architecture Standard Algorithm Name Documentation](https://developers.google.com/j2objc/javadoc/jre/technotes/guides/security/StandardNames.html#KeyGenerator) for information about standard algorithm names. |
| provider | the name of the provider. |
|---|---|

##### Returns

- the new `KeyGenerator` object.

##### Throws

| [NullPointerException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/NullPointerException.html) | if the specified algorithm is null. |
| [NoSuchAlgorithmException](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/NoSuchAlgorithmException.html) | if a KeyGeneratorSpi implementation for the specified algorithm is not available from the specified provider. |
| [NoSuchProviderException](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/NoSuchProviderException.html) | if the specified provider is not registered in the security provider list. |
| [IllegalArgumentException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/IllegalArgumentException.html) | if the `provider` is null or empty. |
|---|---|

##### See Also

- `https://developers.google.com/j2objc/javadoc/jre/reference/java/security/Provider.html`

#### public static final [KeyGenerator](https://developers.google.com/j2objc/javadoc/jre/reference/javax/crypto/KeyGenerator.html)
**getInstance**
([String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html) algorithm, [Provider](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/Provider.html) provider)

Returns a `KeyGenerator` object that generates secret keys
for the specified algorithm.

A new KeyGenerator object encapsulating the
KeyGeneratorSpi implementation from the specified Provider
object is returned. Note that the specified Provider object
does not have to be registered in the provider list.

##### Parameters

| algorithm | the standard name of the requested key algorithm. See the KeyGenerator section in the [Java Cryptography Architecture Standard Algorithm Name Documentation](https://developers.google.com/j2objc/javadoc/jre/technotes/guides/security/StandardNames.html#KeyGenerator) for information about standard algorithm names. |
| provider | the provider. |
|---|---|

##### Returns

- the new `KeyGenerator` object.

##### Throws

| [NullPointerException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/NullPointerException.html) | if the specified algorithm is null. |
| [NoSuchAlgorithmException](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/NoSuchAlgorithmException.html) | if a KeyGeneratorSpi implementation for the specified algorithm is not available from the specified Provider object. |
| [IllegalArgumentException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/IllegalArgumentException.html) | if the `provider` is null. |
|---|---|

##### See Also

- `https://developers.google.com/j2objc/javadoc/jre/reference/java/security/Provider.html`

#### public final [Provider](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/Provider.html)
**getProvider**
()

Returns the provider of this `KeyGenerator` object.

##### Returns

- the provider of this `KeyGenerator` object

#### public final void
**init**
([AlgorithmParameterSpec](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/spec/AlgorithmParameterSpec.html) params, [SecureRandom](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/SecureRandom.html) random)

Initializes this key generator with the specified parameter
set and a user-provided source of randomness.

##### Parameters

| params | the key generation parameters |
| random | the source of randomness for this key generator |
|---|---|

##### Throws

| [InvalidAlgorithmParameterException](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/InvalidAlgorithmParameterException.html) | if `params` is inappropriate for this key generator |
|---|---|

#### public final void
**init**
([SecureRandom](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/SecureRandom.html) random)

Initializes this key generator.

##### Parameters

| random | the source of randomness for this generator |
|---|---|

#### 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
`https://developers.google.com/j2objc/javadoc/jre/reference/java/security/SecureRandom.html`
implementation of the highest-priority installed
provider as the source of randomness.
(If none of the installed providers supply an implementation of
SecureRandom, a system-provided source of randomness will be used.)

##### Parameters

| keysize | the keysize. This is an algorithm-specific metric, specified in number of bits. |
|---|---|

##### Throws

| [InvalidParameterException](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/InvalidParameterException.html) | if the keysize is wrong or not supported. |
|---|---|

#### public final void
**init**
(int keysize, [SecureRandom](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/SecureRandom.html) random)

Initializes this key generator for a certain keysize, using a
user-provided source of randomness.

##### Parameters

| keysize | the keysize. This is an algorithm-specific metric, specified in number of bits. |
| random | the source of randomness for this key generator |
|---|---|

##### Throws

| [InvalidParameterException](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/InvalidParameterException.html) | if the keysize is wrong or not supported. |
|---|---|

#### public final void
**init**
([AlgorithmParameterSpec](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/spec/AlgorithmParameterSpec.html) params)

Initializes this key generator with the specified parameter set.

If this key generator requires any random bytes, it will get them
using the
`https://developers.google.com/j2objc/javadoc/jre/reference/java/security/SecureRandom.html`
implementation of the highest-priority installed
provider as the source of randomness.
(If none of the installed providers supply an implementation of
SecureRandom, a system-provided source of randomness will be used.)

##### Parameters

| params | the key generation parameters |
|---|---|

##### Throws

| [InvalidAlgorithmParameterException](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/InvalidAlgorithmParameterException.html) | if the given parameters are inappropriate for this key generator |
|---|---|