Added in [API level 18](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)   
Deprecated in [API level
23](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels) Summary: [Nested Classes](https://developer.android.com/reference/android/security/KeyPairGeneratorSpec#nestedclasses) \| [Methods](https://developer.android.com/reference/android/security/KeyPairGeneratorSpec#pubmethods) \| [Inherited Methods](https://developer.android.com/reference/android/security/KeyPairGeneratorSpec#inhmethods)

# KeyPairGeneratorSpec

*** ** * ** ***

[Kotlin](https://developer.android.com/reference/kotlin/android/security/KeyPairGeneratorSpec "View this page in Kotlin") \|Java


`
public

final

class
KeyPairGeneratorSpec
`


`

extends https://developer.android.com/reference/java/lang/Object


`

`


implements

https://developer.android.com/reference/java/security/spec/AlgorithmParameterSpec


`

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

<br />

*** ** * ** ***


**This class was deprecated
in API level 23.**   

Use `https://developer.android.com/reference/android/security/keystore/KeyGenParameterSpec` instead.

This provides the required parameters needed for initializing the
`KeyPairGenerator` that works with
[Android KeyStore
facility](https://developer.android.com/training/articles/keystore). The Android KeyStore facility is accessed through a
`https://developer.android.com/reference/java/security/KeyPairGenerator` API using the `AndroidKeyStore`
provider. The `context` passed in may be used to pop up some UI to ask
the user to unlock or initialize the Android KeyStore facility.


After generation, the `keyStoreAlias` is used with the
`https://developer.android.com/reference/java/security/KeyStore#getEntry(java.lang.String,%20java.security.KeyStore.ProtectionParameter)`
interface to retrieve the `https://developer.android.com/reference/java/security/PrivateKey` and its associated
`https://developer.android.com/reference/java/security/cert/Certificate` chain.


The KeyPair generator will create a self-signed certificate with the subject
as its X.509v3 Subject Distinguished Name and as its X.509v3 Issuer
Distinguished Name along with the other parameters specified with the
`https://developer.android.com/reference/android/security/KeyPairGeneratorSpec.Builder`.


The self-signed X.509 certificate may be replaced at a later time by a
certificate signed by a real Certificate Authority.

## Summary

| ### Nested classes ||
|---|---|
| ` class` | `https://developer.android.com/reference/android/security/KeyPairGeneratorSpec.Builder` *This class was deprecated in API level 23. Use `https://developer.android.com/reference/android/security/keystore/KeyGenParameterSpec.Builder` instead.* |

| ### Public methods ||
|---|---|
| ` https://developer.android.com/reference/java/security/spec/AlgorithmParameterSpec` | ` https://developer.android.com/reference/android/security/KeyPairGeneratorSpec#getAlgorithmParameterSpec()() ` Returns the `https://developer.android.com/reference/java/security/spec/AlgorithmParameterSpec` that will be used for creation of the key pair. |
| ` https://developer.android.com/reference/android/content/Context` | ` https://developer.android.com/reference/android/security/KeyPairGeneratorSpec#getContext()() ` Gets the Android context used for operations with this instance. |
| ` https://developer.android.com/reference/java/util/Date` | ` https://developer.android.com/reference/android/security/KeyPairGeneratorSpec#getEndDate()() ` Gets the end date to be used on the X.509 certificate that will be put in the `https://developer.android.com/reference/java/security/KeyStore`. |
| ` int` | ` https://developer.android.com/reference/android/security/KeyPairGeneratorSpec#getKeySize()() ` Returns the key size specified by this parameter. |
| ` https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/android/security/KeyPairGeneratorSpec#getKeyType()() ` Returns the type of key pair (e.g., `EC`, `RSA`) to be generated. |
| ` https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/android/security/KeyPairGeneratorSpec#getKeystoreAlias()() ` Returns the alias that will be used in the `java.security.KeyStore` in conjunction with the `AndroidKeyStore`. |
| ` https://developer.android.com/reference/java/math/BigInteger` | ` https://developer.android.com/reference/android/security/KeyPairGeneratorSpec#getSerialNumber()() ` Gets the serial number to be used on the X.509 certificate that will be put in the `https://developer.android.com/reference/java/security/KeyStore`. |
| ` https://developer.android.com/reference/java/util/Date` | ` https://developer.android.com/reference/android/security/KeyPairGeneratorSpec#getStartDate()() ` Gets the start date to be used on the X.509 certificate that will be put in the `https://developer.android.com/reference/java/security/KeyStore`. |
| ` https://developer.android.com/reference/javax/security/auth/x500/X500Principal` | ` https://developer.android.com/reference/android/security/KeyPairGeneratorSpec#getSubjectDN()() ` Gets the subject distinguished name to be used on the X.509 certificate that will be put in the `https://developer.android.com/reference/java/security/KeyStore`. |
| ` boolean` | ` https://developer.android.com/reference/android/security/KeyPairGeneratorSpec#isEncryptionRequired()() ` *This method is deprecated. Encryption at rest is on by default. If extra binding to the lockscreen screen credential is desired use `https://developer.android.com/reference/android/security/keystore/KeyGenParameterSpec.Builder#setUserAuthenticationRequired(boolean)`. This flag will be ignored from Android S.* |

| ### Inherited methods |
|---|---|
| From class ` https://developer.android.com/reference/java/lang/Object ` |---|---| | ` https://developer.android.com/reference/java/lang/Object` | ` https://developer.android.com/reference/java/lang/Object#clone()() ` Creates and returns a copy of this object. | | ` boolean` | ` https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object obj) ` Indicates whether some other object is "equal to" this one. | | ` void` | ` 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 https://developer.android.com/reference/java/lang/Class<?>` | ` https://developer.android.com/reference/java/lang/Object#getClass()() ` Returns the runtime class of this `Object`. | | ` int` | ` https://developer.android.com/reference/java/lang/Object#hashCode()() ` Returns a hash code value for the object. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#notify()() ` Wakes up a single thread that is waiting on this object's monitor. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#notifyAll()() ` Wakes up all threads that are waiting on this object's monitor. | | ` https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/java/lang/Object#toString()() ` Returns a string representation of the object. | | ` final void` | ` 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` | ` 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` | ` 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 methods

### getAlgorithmParameterSpec

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

```
public AlgorithmParameterSpec getAlgorithmParameterSpec ()
```

Returns the `https://developer.android.com/reference/java/security/spec/AlgorithmParameterSpec` that will be used for creation
of the key pair.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/security/spec/AlgorithmParameterSpec` | This value cannot be `null`. <br /> |

### getContext

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

```
public Context getContext ()
```

Gets the Android context used for operations with this instance.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/content/Context` | <br /> |

### getEndDate

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

```
public Date getEndDate ()
```

Gets the end date to be used on the X.509 certificate that will be put in
the `https://developer.android.com/reference/java/security/KeyStore`.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/util/Date` | This value cannot be `null`. <br /> |

### getKeySize

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

```
public int getKeySize ()
```

Returns the key size specified by this parameter. For instance, for RSA
this will return the modulus size and for EC it will return the field
size.

<br />

| Returns ||
|---|---|
| `int` | <br /> |

### getKeyType

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

```
public String getKeyType ()
```

Returns the type of key pair (e.g., `EC`, `RSA`) to be generated. See
`https://developer.android.com/reference/android/security/keystore/KeyProperties`.`KEY_ALGORITHM` constants.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/lang/String` | This value may be `null`. Value is one of the following: - `https://developer.android.com/reference/android/security/keystore/KeyProperties#KEY_ALGORITHM_RSA` - `https://developer.android.com/reference/android/security/keystore/KeyProperties#KEY_ALGORITHM_EC` - `https://developer.android.com/reference/android/security/keystore/KeyProperties#KEY_ALGORITHM_AES` - `https://developer.android.com/reference/android/security/keystore/KeyProperties#KEY_ALGORITHM_HMAC_SHA1` - `https://developer.android.com/reference/android/security/keystore/KeyProperties#KEY_ALGORITHM_HMAC_SHA224` - `https://developer.android.com/reference/android/security/keystore/KeyProperties#KEY_ALGORITHM_HMAC_SHA256` - `https://developer.android.com/reference/android/security/keystore/KeyProperties#KEY_ALGORITHM_HMAC_SHA384` - `https://developer.android.com/reference/android/security/keystore/KeyProperties#KEY_ALGORITHM_HMAC_SHA512` |

### getKeystoreAlias

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

```
public String getKeystoreAlias ()
```

Returns the alias that will be used in the `java.security.KeyStore`
in conjunction with the `AndroidKeyStore`.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/lang/String` | <br /> |

### getSerialNumber

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

```
public BigInteger getSerialNumber ()
```

Gets the serial number to be used on the X.509 certificate that will be
put in the `https://developer.android.com/reference/java/security/KeyStore`.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/math/BigInteger` | This value cannot be `null`. <br /> |

### getStartDate

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

```
public Date getStartDate ()
```

Gets the start date to be used on the X.509 certificate that will be put
in the `https://developer.android.com/reference/java/security/KeyStore`.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/util/Date` | This value cannot be `null`. <br /> |

### getSubjectDN

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

```
public X500Principal getSubjectDN ()
```

Gets the subject distinguished name to be used on the X.509 certificate
that will be put in the `https://developer.android.com/reference/java/security/KeyStore`.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/javax/security/auth/x500/X500Principal` | This value cannot be `null`. <br /> |

### isEncryptionRequired

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

```
public boolean isEncryptionRequired ()
```


**This method is deprecated.**   

Encryption at rest is on by default. If extra binding to the lockscreen screen
credential is desired use
`https://developer.android.com/reference/android/security/keystore/KeyGenParameterSpec.Builder#setUserAuthenticationRequired(boolean)`.
This flag will be ignored from Android S.

Returns `true` if the key must be encrypted at rest. This will protect the key pair
with the secure lock screen credential (e.g., password, PIN, or pattern).

Note that encrypting the key at rest requires that the secure lock screen (e.g., password,
PIN, pattern) is set up, otherwise key generation will fail. Moreover, this key will be
deleted when the secure lock screen is disabled or reset (e.g., by the user or a Device
Administrator). Finally, this key cannot be used until the user unlocks the secure lock
screen after boot.

<br />

| Returns ||
|---|---|
| `boolean` | <br /> |

**See also:**

- `https://developer.android.com/reference/android/app/KeyguardManager#isDeviceSecure()`