# KeyFactorySpi

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

# KeyFactorySpi

`
public


abstract
class
KeyFactorySpi
`  
`

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


``


`

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

<br />

*** ** * ** ***

This class defines the*Service Provider Interface* (**SPI** ) for the`KeyFactory`class. All the abstract methods in this class must be implemented by each cryptographic service provider who wishes to supply the implementation of a key factory for a particular algorithm.

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.

A provider should document all the key specifications supported by its key factory.  
**See also:**

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

|                                             ### Public constructors                                             ||
|--------------------------------------------------------------------------------------------------------------|---|
| ` `[KeyFactorySpi](https://developer.android.com/reference/java/security/KeyFactorySpi#KeyFactorySpi())`() ` |

|                                                                                                                                                                                                                                   ### Protected methods                                                                                                                                                                                                                                   ||
|-----------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` abstract `[PrivateKey](https://developer.android.com/reference/java/security/PrivateKey)                | ` `[engineGeneratePrivate](https://developer.android.com/reference/java/security/KeyFactorySpi#engineGeneratePrivate(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).                                               |
| ` abstract `[PublicKey](https://developer.android.com/reference/java/security/PublicKey)                  | ` `[engineGeneratePublic](https://developer.android.com/reference/java/security/KeyFactorySpi#engineGeneratePublic(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).                                                  |
| ` abstract <T extends `[KeySpec](https://developer.android.com/reference/java/security/spec/KeySpec)`> T` | ` `[engineGetKeySpec](https://developer.android.com/reference/java/security/KeyFactorySpi#engineGetKeySpec(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. |
| ` abstract `[Key](https://developer.android.com/reference/java/security/Key)                              | ` `[engineTranslateKey](https://developer.android.com/reference/java/security/KeyFactorySpi#engineTranslateKey(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*.                                                                                             | ||

## Public constructors

### KeyFactorySpi

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

```
public KeyFactorySpi ()
```

<br />

<br />

## Protected methods

### engineGeneratePrivate

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

```
protected abstract PrivateKey engineGeneratePrivate (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. |

### engineGeneratePublic

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

```
protected abstract PublicKey engineGeneratePublic (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. |

### engineGetKeySpec

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

```
protected abstract T engineGetKeySpec (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 dealt with (e.g., the given key has an unrecognized format). |

### engineTranslateKey

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

```
protected abstract Key engineTranslateKey (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. |