# KeyFactory

public class **KeyFactory** extends [Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html)  
Key factories are used to convert *keys* (opaque
cryptographic keys of type `Key`) into *key specifications*
(transparent representations of the underlying key material), and vice
versa.

Key factories are bi-directional. That is, they allow you to build an
opaque key object from a given key specification (key material), or to
retrieve the underlying key material of a key object in a suitable format.

Multiple compatible key specifications may exist for the same key.
For example, a DSA public key may be specified using
`DSAPublicKeySpec` or
`X509EncodedKeySpec`. A key factory can be used to translate
between compatible key specifications.

The following is an example of how to use a key factory in order to
instantiate a DSA public key from its encoding.
Assume Alice has received a digital signature from Bob.
Bob also sent her his public key (in encoded format) to verify
his signature. Alice then performs the following actions:

```
 X509EncodedKeySpec bobPubKeySpec = new X509EncodedKeySpec(bobEncodedPubKey);
 KeyFactory keyFactory = KeyFactory.getInstance("DSA");
 PublicKey bobPubKey = keyFactory.generatePublic(bobPubKeySpec);
 Signature sig = Signature.getInstance("DSA");
 sig.initVerify(bobPubKey);
 sig.update(data);
 sig.verify(signature);
 
```

Android provides the following `KeyFactory` algorithms:

| Algorithm | Supported API Levels |
|---|---|
| DH | 1+ |
| DSA | 1+ |
| EC | 11+ |
| RSA | 1+ |
| X.509 | 1-8 |

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

<br />

##### See Also

- `https://developers.google.com/j2objc/javadoc/jre/reference/java/security/Key.html`
- `https://developers.google.com/j2objc/javadoc/jre/reference/java/security/PublicKey.html`
- `https://developers.google.com/j2objc/javadoc/jre/reference/java/security/PrivateKey.html`
- `https://developers.google.com/j2objc/javadoc/jre/reference/java/security/spec/KeySpec.html`
- `https://developers.google.com/j2objc/javadoc/jre/reference/java/security/spec/DSAPublicKeySpec.html`
- `https://developers.google.com/j2objc/javadoc/jre/reference/java/security/spec/X509EncodedKeySpec.html`

### Protected Constructor Summary

|---|---|
|   | [KeyFactory](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/KeyFactory.html#KeyFactory(java.security.KeyFactorySpi,%20java.security.Provider,%20java.lang.String))([KeyFactorySpi](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/KeyFactorySpi.html) keyFacSpi, [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 KeyFactory object. |

### Public Method Summary

|---|---|
| final [PrivateKey](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/PrivateKey.html) | [generatePrivate](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/KeyFactory.html#generatePrivate(java.security.spec.KeySpec))([KeySpec](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/spec/KeySpec.html) keySpec) Generates a private key object from the provided key specification (key material). |
| final [PublicKey](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/PublicKey.html) | [generatePublic](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/KeyFactory.html#generatePublic(java.security.spec.KeySpec))([KeySpec](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/spec/KeySpec.html) keySpec) Generates a public key object from the provided key specification (key material). |
| final [String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html) | [getAlgorithm](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/KeyFactory.html#getAlgorithm())() Gets the name of the algorithm associated with this `KeyFactory`. |
| static [KeyFactory](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/KeyFactory.html) | [getInstance](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/KeyFactory.html#getInstance(java.lang.String))([String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html) algorithm) Returns a KeyFactory object that converts public/private keys of the specified algorithm. |
| static [KeyFactory](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/KeyFactory.html) | [getInstance](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/KeyFactory.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 KeyFactory object that converts public/private keys of the specified algorithm. |
| static [KeyFactory](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/KeyFactory.html) | [getInstance](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/KeyFactory.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 KeyFactory object that converts public/private keys of the specified algorithm. |
| final \<T extends [KeySpec](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/spec/KeySpec.html)\> T | [getKeySpec](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/KeyFactory.html#getKeySpec(java.security.Key,%20java.lang.Class<T>))([Key](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/Key.html) key, [Class](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Class.html)\<T\> keySpec) Returns a specification (key material) of the given key object. |
| final [Provider](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/Provider.html) | [getProvider](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/KeyFactory.html#getProvider())() Returns the provider of this key factory object. |
| final [Key](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/Key.html) | [translateKey](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/KeyFactory.html#translateKey(java.security.Key))([Key](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/Key.html) key) Translates a key object, whose provider may be unknown or potentially untrusted, into a corresponding key object of this key factory. |

### 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
**KeyFactory**
([KeyFactorySpi](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/KeyFactorySpi.html) keyFacSpi, [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 KeyFactory object.

##### Parameters

| keyFacSpi | the delegate |
| provider | the provider |
| algorithm | the name of the algorithm to associate with this `KeyFactory` |
|---|---|

## Public Methods

#### public final [PrivateKey](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/PrivateKey.html)
**generatePrivate**
([KeySpec](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/spec/KeySpec.html) keySpec)

Generates a private key object from the provided key specification
(key material).

##### Parameters

| keySpec | the specification (key material) of the private key. |
|---|---|

##### Returns

- the private key.

##### Throws

| [InvalidKeySpecException](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/spec/InvalidKeySpecException.html) | if the given key specification is inappropriate for this key factory to produce a private key. |
|---|---|

#### public final [PublicKey](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/PublicKey.html)
**generatePublic**
([KeySpec](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/spec/KeySpec.html) keySpec)

Generates a public key object from the provided key specification
(key material).

##### Parameters

| keySpec | the specification (key material) of the public key. |
|---|---|

##### Returns

- the public key.

##### Throws

| [InvalidKeySpecException](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/spec/InvalidKeySpecException.html) | if the given key specification is inappropriate for this key factory to produce a public key. |
|---|---|

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

Gets the name of the algorithm
associated with this `KeyFactory`.

##### Returns

- the name of the algorithm associated with this `KeyFactory`

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

Returns a KeyFactory object that converts
public/private keys of the specified algorithm.

This method traverses the list of registered security Providers,
starting with the most preferred Provider.
A new KeyFactory object encapsulating the
KeyFactorySpi 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 name of the requested key algorithm. See the KeyFactory section in the [Java Cryptography Architecture Standard Algorithm Name Documentation](https://developers.google.com/j2objc/javadoc/jre/technotes/guides/security/StandardNames.html#KeyFactory) for information about standard algorithm names. |
|---|---|

##### Returns

- the new KeyFactory object.

##### Throws

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

##### See Also

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

#### public static [KeyFactory](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/KeyFactory.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 KeyFactory object that converts
public/private keys of the specified algorithm.

A new KeyFactory object encapsulating the
KeyFactorySpi 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 name of the requested key algorithm. See the KeyFactory section in the [Java Cryptography Architecture Standard Algorithm Name Documentation](https://developers.google.com/j2objc/javadoc/jre/technotes/guides/security/StandardNames.html#KeyFactory) for information about standard algorithm names. |
| provider | the name of the provider. |
|---|---|

##### Returns

- the new KeyFactory object.

##### Throws

| [NoSuchAlgorithmException](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/NoSuchAlgorithmException.html) | if a KeyFactorySpi 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 name is null or empty. |
|---|---|

##### See Also

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

#### public static [KeyFactory](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/KeyFactory.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 KeyFactory object that converts
public/private keys of the specified algorithm.

A new KeyFactory object encapsulating the
KeyFactorySpi 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 name of the requested key algorithm. See the KeyFactory section in the [Java Cryptography Architecture Standard Algorithm Name Documentation](https://developers.google.com/j2objc/javadoc/jre/technotes/guides/security/StandardNames.html#KeyFactory) for information about standard algorithm names. |
| provider | the provider. |
|---|---|

##### Returns

- the new KeyFactory object.

##### Throws

| [NoSuchAlgorithmException](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/NoSuchAlgorithmException.html) | if a KeyFactorySpi 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 specified provider is null. |
|---|---|

##### See Also

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

#### public final T
**getKeySpec**
([Key](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/Key.html) key, [Class](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Class.html)\<T\> keySpec)

Returns a specification (key material) of the given key object.
`keySpec` identifies the specification class in which
the key material should be returned. It could, for example, be
`DSAPublicKeySpec.class`, to indicate that the
key material should be returned in an instance of the
`DSAPublicKeySpec` class.

##### Parameters

| key | the key. |
| keySpec | the specification class in which the key material should be returned. |
|---|---|

##### Returns

- the underlying key specification (key material) in an instance of the requested specification class.

##### Throws

| [InvalidKeySpecException](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/spec/InvalidKeySpecException.html) | if the requested key specification is inappropriate for the given key, or the given key cannot be processed (e.g., the given key has an unrecognized algorithm or format). |
|---|---|

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

Returns the provider of this key factory object.

##### Returns

- the provider of this key factory object

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

Translates a key object, whose provider may be unknown or potentially
untrusted, into a corresponding key object of this key factory.

##### Parameters

| key | the key whose provider is unknown or untrusted. |
|---|---|

##### Returns

- the translated key.

##### Throws

| [InvalidKeyException](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/InvalidKeyException.html) | if the given key cannot be processed by this key factory. |
|---|---|