# KeyFactory

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  
Summary:[Protected Ctors](https://developer.android.com/reference/java/security/KeyFactory#proctors)\|[Methods](https://developer.android.com/reference/java/security/KeyFactory#pubmethods)\|[Inherited Methods](https://developer.android.com/reference/java/security/KeyFactory#inhmethods)  

# KeyFactory

`
public


class
KeyFactory
`  
`

extends `[Object](https://developer.android.com/reference/java/lang/Object)`


``


`

|---|--------------------------|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | java.security.KeyFactory |

<br />

*** ** * ** ***

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                  |
| XDH       | 33+                  |

<br />

**See also:**

- [Key](https://developer.android.com/reference/java/security/Key)
- [PublicKey](https://developer.android.com/reference/java/security/PublicKey)
- [PrivateKey](https://developer.android.com/reference/java/security/PrivateKey)
- [KeySpec](https://developer.android.com/reference/java/security/spec/KeySpec)
- [DSAPublicKeySpec](https://developer.android.com/reference/java/security/spec/DSAPublicKeySpec)
- [X509EncodedKeySpec](https://developer.android.com/reference/java/security/spec/X509EncodedKeySpec)

## Summary

|                                                                                                                                                                                                                             ### Protected constructors                                                                                                                                                                                                                              ||
|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---|
| ` `[KeyFactory](https://developer.android.com/reference/java/security/KeyFactory#KeyFactory(java.security.KeyFactorySpi,%20java.security.Provider,%20java.lang.String))`(`[KeyFactorySpi](https://developer.android.com/reference/java/security/KeyFactorySpi)` keyFacSpi, `[Provider](https://developer.android.com/reference/java/security/Provider)` provider, `[String](https://developer.android.com/reference/java/lang/String)` algorithm) ` Creates a KeyFactory object. |

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

| ### Inherited methods |
|-----------------------|---|
| From class` `[java.lang.Object](https://developer.android.com/reference/java/lang/Object)` ` |--------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` `[Object](https://developer.android.com/reference/java/lang/Object)          | ` `[clone](https://developer.android.com/reference/java/lang/Object#clone())`() ` Creates and returns a copy of this object.                                                                                                                                                      | | ` boolean`                                                                     | ` `[equals](https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object))`(`[Object](https://developer.android.com/reference/java/lang/Object)` obj) ` Indicates whether some other object is "equal to" this one.                                           | | ` void`                                                                        | ` `[finalize](https://developer.android.com/reference/java/lang/Object#finalize())`() ` Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.                                                          | | ` final `[Class](https://developer.android.com/reference/java/lang/Class)`<?>` | ` `[getClass](https://developer.android.com/reference/java/lang/Object#getClass())`() ` Returns the runtime class of this`Object`.                                                                                                                                                | | ` int`                                                                         | ` `[hashCode](https://developer.android.com/reference/java/lang/Object#hashCode())`() ` Returns a hash code value for the object.                                                                                                                                                 | | ` final void`                                                                  | ` `[notify](https://developer.android.com/reference/java/lang/Object#notify())`() ` Wakes up a single thread that is waiting on this object's monitor.                                                                                                                            | | ` final void`                                                                  | ` `[notifyAll](https://developer.android.com/reference/java/lang/Object#notifyAll())`() ` Wakes up all threads that are waiting on this object's monitor.                                                                                                                         | | ` `[String](https://developer.android.com/reference/java/lang/String)          | ` `[toString](https://developer.android.com/reference/java/lang/Object#toString())`() ` Returns a string representation of the object.                                                                                                                                            | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait(long,%20int))`(long timeoutMillis, int nanos) ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*, or until a certain amount of real time has elapsed. | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait(long))`(long timeoutMillis) ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*, or until a certain amount of real time has elapsed.                   | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait())`() ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*.                                                                                             | ||

## Protected constructors

### KeyFactory

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
protected KeyFactory (KeyFactorySpi keyFacSpi, 
                Provider provider, 
                String algorithm)
```

Creates a KeyFactory object.

<br />

|                                         Parameters                                         ||
|-------------|-------------------------------------------------------------------------------|
| `keyFacSpi` | `KeyFactorySpi`: the delegate <br />                                          |
| `provider`  | `Provider`: the provider <br />                                               |
| `algorithm` | `String`: the name of the algorithm to associate with this`KeyFactory` <br /> |

## Public methods

### generatePrivate

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public final PrivateKey generatePrivate (KeySpec keySpec)
```

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

<br />

|                                    Parameters                                     ||
|-----------|------------------------------------------------------------------------|
| `keySpec` | `KeySpec`: the specification (key material) of the private key. <br /> |

|                                                 Returns                                                 ||
|--------------------------------------------------------------------------------|-------------------------|
| [PrivateKey](https://developer.android.com/reference/java/security/PrivateKey) | the private key. <br /> |

|                                                                                                    Throws                                                                                                     ||
|---------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------|
| [InvalidKeySpecException](https://developer.android.com/reference/java/security/spec/InvalidKeySpecException) | if the given key specification is inappropriate for this key factory to produce a private key. |

### generatePublic

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public final PublicKey generatePublic (KeySpec keySpec)
```

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

<br />

|                                    Parameters                                    ||
|-----------|-----------------------------------------------------------------------|
| `keySpec` | `KeySpec`: the specification (key material) of the public key. <br /> |

|                                               Returns                                                ||
|------------------------------------------------------------------------------|------------------------|
| [PublicKey](https://developer.android.com/reference/java/security/PublicKey) | the public key. <br /> |

|                                                                                                    Throws                                                                                                    ||
|---------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------|
| [InvalidKeySpecException](https://developer.android.com/reference/java/security/spec/InvalidKeySpecException) | if the given key specification is inappropriate for this key factory to produce a public key. |

### getAlgorithm

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public final String getAlgorithm ()
```

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

<br />

|                                                                Returns                                                                ||
|--------------------------------------------------------------------|-------------------------------------------------------------------|
| [String](https://developer.android.com/reference/java/lang/String) | the name of the algorithm associated with this`KeyFactory` <br /> |

### getInstance

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public static KeyFactory getInstance (String 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[Security.getProviders()](https://developer.android.com/reference/java/security/Security#getProviders())method.

<br />

|                               Parameters                               ||
|-------------|-----------------------------------------------------------|
| `algorithm` | `String`: the name of the requested key algorithm. <br /> |

|                                                     Returns                                                      ||
|--------------------------------------------------------------------------------|----------------------------------|
| [KeyFactory](https://developer.android.com/reference/java/security/KeyFactory) | the new`KeyFactory`object <br /> |

|                                                                                             Throws                                                                                             ||
|------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------|
| [NoSuchAlgorithmException](https://developer.android.com/reference/java/security/NoSuchAlgorithmException) | if no`Provider`supports a`KeyFactorySpi`implementation for the specified algorithm |
| [NullPointerException](https://developer.android.com/reference/java/lang/NullPointerException)             | if`algorithm`is`null`on Android 14 or above                                        |

**See also:**

- [Provider](https://developer.android.com/reference/java/security/Provider)  

### getInstance

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public static KeyFactory getInstance (String algorithm, 
                String 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[Security.getProviders()](https://developer.android.com/reference/java/security/Security#getProviders())method.

<br />

|                               Parameters                               ||
|-------------|-----------------------------------------------------------|
| `algorithm` | `String`: the name of the requested key algorithm. <br /> |
| `provider`  | `String`: the name of the provider. <br />                |

|                                                     Returns                                                      ||
|--------------------------------------------------------------------------------|----------------------------------|
| [KeyFactory](https://developer.android.com/reference/java/security/KeyFactory) | the new`KeyFactory`object <br /> |

|                                                                                                         Throws                                                                                                         ||
|------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------|
| [IllegalArgumentException](https://developer.android.com/reference/java/lang/IllegalArgumentException)     | if the provider name is`null`or empty                                                                      |
| [NoSuchAlgorithmException](https://developer.android.com/reference/java/security/NoSuchAlgorithmException) | if a`KeyFactorySpi`implementation for the specified algorithm is not available from the specified provider |
| [NoSuchProviderException](https://developer.android.com/reference/java/security/NoSuchProviderException)   | if the specified provider is not registered in the security provider list                                  |
| [NullPointerException](https://developer.android.com/reference/java/lang/NullPointerException)             | if`algorithm`is`null`on Android 14 or above                                                                |

**See also:**

- [Provider](https://developer.android.com/reference/java/security/Provider)  

### getInstance

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public static KeyFactory getInstance (String algorithm, 
                Provider 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.

<br />

|                               Parameters                               ||
|-------------|-----------------------------------------------------------|
| `algorithm` | `String`: the name of the requested key algorithm. <br /> |
| `provider`  | `Provider`: the provider. <br />                          |

|                                                     Returns                                                      ||
|--------------------------------------------------------------------------------|----------------------------------|
| [KeyFactory](https://developer.android.com/reference/java/security/KeyFactory) | the new`KeyFactory`object <br /> |

|                                                                                                            Throws                                                                                                             ||
|------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------|
| [IllegalArgumentException](https://developer.android.com/reference/java/lang/IllegalArgumentException)     | if the specified provider is`null`                                                                                |
| [NoSuchAlgorithmException](https://developer.android.com/reference/java/security/NoSuchAlgorithmException) | if a`KeyFactorySpi`implementation for the specified algorithm is not available from the specified`Provider`object |
| [NullPointerException](https://developer.android.com/reference/java/lang/NullPointerException)             | if`algorithm`is`null`on Android 14 or above                                                                       |

**See also:**

- [Provider](https://developer.android.com/reference/java/security/Provider)  

### getKeySpec

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public final T getKeySpec (Key key, 
                Class<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.

<br />

|                                            Parameters                                            ||
|-----------|---------------------------------------------------------------------------------------|
| `key`     | `Key`: the key. <br />                                                                |
| `keySpec` | `Class`: the specification class in which the key material should be returned. <br /> |

|                                                     Returns                                                      ||
|-----|-------------------------------------------------------------------------------------------------------------|
| `T` | the underlying key specification (key material) in an instance of the requested specification class. <br /> |

|                                                                                                                                          Throws                                                                                                                                           ||
|---------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [InvalidKeySpecException](https://developer.android.com/reference/java/security/spec/InvalidKeySpecException) | 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). |

### getProvider

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public final Provider getProvider ()
```

Returns the provider of this key factory object.

<br />

|                                                          Returns                                                           ||
|----------------------------------------------------------------------------|------------------------------------------------|
| [Provider](https://developer.android.com/reference/java/security/Provider) | the provider of this key factory object <br /> |

### translateKey

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public final Key translateKey (Key key)
```

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

<br />

|                              Parameters                              ||
|-------|---------------------------------------------------------------|
| `key` | `Key`: the key whose provider is unknown or untrusted. <br /> |

|                                           Returns                                            ||
|------------------------------------------------------------------|----------------------------|
| [Key](https://developer.android.com/reference/java/security/Key) | the translated key. <br /> |

|                                                                           Throws                                                                            ||
|--------------------------------------------------------------------------------------------------|-----------------------------------------------------------|
| [InvalidKeyException](https://developer.android.com/reference/java/security/InvalidKeyException) | if the given key cannot be processed by this key factory. |