# KeyPairGeneratorSpec.Builder

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

# Builder

*** ** * ** ***

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

```
class Builder
```

|---|------------------------------------------------------------------------------------------------------------------------------------------------|
| [kotlin.Any](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html)                                                                 ||
| ↳ | [android.security.KeyPairGeneratorSpec.Builder](https://developer.android.com/reference/kotlin/android/security/KeyPairGeneratorSpec.Builder#) |

*** ** * ** ***

| **This class was deprecated in API level 23.**
|
| Use[KeyGenParameterSpec.Builder](https://developer.android.com/reference/kotlin/android/security/keystore/KeyGenParameterSpec.Builder)instead.

Builder class for[KeyPairGeneratorSpec](https://developer.android.com/reference/kotlin/android/security/KeyPairGeneratorSpec)objects.

This will build a parameter spec for use with the[Android KeyStore facility](https://developer.android.com/training/articles/keystore.html).

The required fields must be filled in with the builder.

Example:  

```kotlin
Calendar start = Calendar.getInstance();
  Calendar end = Calendar.getInstance();
  end.add(Calendar.YEAR, 1);
 
  KeyPairGeneratorSpec spec =
          new KeyPairGeneratorSpec.Builder(mContext).setAlias("myKey")
                  .setSubject(new X500Principal("CN=myKey")).setSerialNumber(BigInteger.valueOf(1337))
                  .setStartDate(start.getTime()).setEndDate(end.getTime()).build();
  
```

## Summary

|                                                                                                                                                Public constructors                                                                                                                                                 ||
|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---|
| [Builder](https://developer.android.com/reference/kotlin/android/security/KeyPairGeneratorSpec.Builder#Builder(android.content.Context))`(`context:` `[Context](https://developer.android.com/reference/kotlin/android/content/Context.html#)`)` Creates a new instance of the`Builder`with the given`context`. |

|                                                                                                                                                                                                                                                                               Public methods                                                                                                                                                                                                                                                                                ||
|--------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [KeyPairGeneratorSpec](https://developer.android.com/reference/kotlin/android/security/KeyPairGeneratorSpec)                   | [build](https://developer.android.com/reference/kotlin/android/security/KeyPairGeneratorSpec.Builder#build())`()` Builds the instance of the`KeyPairGeneratorSpec`.                                                                                                                                                                                                                                                                         |
| [KeyPairGeneratorSpec.Builder](https://developer.android.com/reference/kotlin/android/security/KeyPairGeneratorSpec.Builder#)! | [setAlgorithmParameterSpec](https://developer.android.com/reference/kotlin/android/security/KeyPairGeneratorSpec.Builder#setAlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec))`(`spec:` `[AlgorithmParameterSpec](https://developer.android.com/reference/kotlin/java/security/spec/AlgorithmParameterSpec.html#)`)` Sets the algorithm-specific key generation parameters.                                                 |
| [KeyPairGeneratorSpec.Builder](https://developer.android.com/reference/kotlin/android/security/KeyPairGeneratorSpec.Builder#)  | [setAlias](https://developer.android.com/reference/kotlin/android/security/KeyPairGeneratorSpec.Builder#setAlias(kotlin.String))`(`alias:` `[String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)`)` Sets the alias to be used to retrieve the key later from a[java.security.KeyStore](https://developer.android.com/reference/kotlin/java/security/KeyStore.html#)instance using the`AndroidKeyStore`provider. |
| [KeyPairGeneratorSpec.Builder](https://developer.android.com/reference/kotlin/android/security/KeyPairGeneratorSpec.Builder#)  | [setEncryptionRequired](https://developer.android.com/reference/kotlin/android/security/KeyPairGeneratorSpec.Builder#setEncryptionRequired())`()` Indicates that this key pair must be encrypted at rest.                                                                                                                                                                                                                                   |
| [KeyPairGeneratorSpec.Builder](https://developer.android.com/reference/kotlin/android/security/KeyPairGeneratorSpec.Builder#)  | [setEndDate](https://developer.android.com/reference/kotlin/android/security/KeyPairGeneratorSpec.Builder#setEndDate(java.util.Date))`(`endDate:` `[Date](https://developer.android.com/reference/kotlin/java/util/Date.html#)`)` Sets the end of the validity period for the self-signed certificate of the generated key pair.                                                                                                            |
| [KeyPairGeneratorSpec.Builder](https://developer.android.com/reference/kotlin/android/security/KeyPairGeneratorSpec.Builder#)  | [setKeySize](https://developer.android.com/reference/kotlin/android/security/KeyPairGeneratorSpec.Builder#setKeySize(kotlin.Int))`(`keySize:` `[Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html)`)` Sets the key size for the keypair to be created.                                                                                                                                                               |
| [KeyPairGeneratorSpec.Builder](https://developer.android.com/reference/kotlin/android/security/KeyPairGeneratorSpec.Builder#)  | [setKeyType](https://developer.android.com/reference/kotlin/android/security/KeyPairGeneratorSpec.Builder#setKeyType(kotlin.String))`(`keyType:` `[String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)`)` Sets the type of key pair (e.g.,                                                                                                                                                                      |
| [KeyPairGeneratorSpec.Builder](https://developer.android.com/reference/kotlin/android/security/KeyPairGeneratorSpec.Builder#)  | [setSerialNumber](https://developer.android.com/reference/kotlin/android/security/KeyPairGeneratorSpec.Builder#setSerialNumber(java.math.BigInteger))`(`serialNumber:` `[BigInteger](https://developer.android.com/reference/kotlin/java/math/BigInteger.html#)`)` Sets the serial number used for the self-signed certificate of the generated key pair.                                                                                   |
| [KeyPairGeneratorSpec.Builder](https://developer.android.com/reference/kotlin/android/security/KeyPairGeneratorSpec.Builder#)  | [setStartDate](https://developer.android.com/reference/kotlin/android/security/KeyPairGeneratorSpec.Builder#setStartDate(java.util.Date))`(`startDate:` `[Date](https://developer.android.com/reference/kotlin/java/util/Date.html#)`)` Sets the start of the validity period for the self-signed certificate of the generated key pair.                                                                                                    |
| [KeyPairGeneratorSpec.Builder](https://developer.android.com/reference/kotlin/android/security/KeyPairGeneratorSpec.Builder#)  | [setSubject](https://developer.android.com/reference/kotlin/android/security/KeyPairGeneratorSpec.Builder#setSubject(javax.security.auth.x500.X500Principal))`(`subject:` `[X500Principal](https://developer.android.com/reference/kotlin/javax/security/auth/x500/X500Principal.html#)`)` Sets the subject used for the self-signed certificate of the generated key pair.                                                                 |

## Public constructors

### Builder

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

```
Builder(context: Context)
```

Creates a new instance of the`Builder`with the given`context`. The`context`passed in may be used to pop up some UI to ask the user to unlock or initialize the Android KeyStore facility.

|                                                           Parameters                                                           ||
|-----------|---------------------------------------------------------------------------------------------------------------------|
| `context` | [Context](https://developer.android.com/reference/kotlin/android/content/Context.html#):This value cannot be`null`. |

## Public methods

### build

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

```
fun build(): KeyPairGeneratorSpec
```

**Deprecated:** *Deprecated in Java.*

Builds the instance of the`KeyPairGeneratorSpec`.

|                                                                                      Return                                                                                      ||
|--------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------|
| [KeyPairGeneratorSpec](https://developer.android.com/reference/kotlin/android/security/KeyPairGeneratorSpec) | built instance of`KeyPairGeneratorSpec`This value cannot be`null`. |

|                              Exceptions                              ||
|--------------------------------------|--------------------------------|
| `java.lang.IllegalArgumentException` | if a required field is missing |

### setAlgorithmParameterSpec

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

```
fun setAlgorithmParameterSpec(spec: AlgorithmParameterSpec): KeyPairGeneratorSpec.Builder!
```

**Deprecated:** *Deprecated in Java.*

Sets the algorithm-specific key generation parameters. For example, for RSA keys this may be an instance of[java.security.spec.RSAKeyGenParameterSpec](https://developer.android.com/reference/kotlin/java/security/spec/RSAKeyGenParameterSpec.html#).

|                                                                          Parameters                                                                          ||
|--------|------------------------------------------------------------------------------------------------------------------------------------------------------|
| `spec` | [AlgorithmParameterSpec](https://developer.android.com/reference/kotlin/java/security/spec/AlgorithmParameterSpec.html#):This value cannot be`null`. |

### setAlias

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

```
fun setAlias(alias: String): KeyPairGeneratorSpec.Builder
```

**Deprecated:** *Deprecated in Java.*

Sets the alias to be used to retrieve the key later from a[java.security.KeyStore](https://developer.android.com/reference/kotlin/java/security/KeyStore.html#)instance using the`AndroidKeyStore`provider.

|                                                      Parameters                                                       ||
|---------|--------------------------------------------------------------------------------------------------------------|
| `alias` | [String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html):This value cannot be`null`. |

|                                                                           Return                                                                           ||
|-------------------------------------------------------------------------------------------------------------------------------|-----------------------------|
| [KeyPairGeneratorSpec.Builder](https://developer.android.com/reference/kotlin/android/security/KeyPairGeneratorSpec.Builder#) | This value cannot be`null`. |

### setEncryptionRequired

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

```
fun setEncryptionRequired(): KeyPairGeneratorSpec.Builder
```

**Deprecated:** *Data at rest encryption is enabled by default. If extra binding to the lockscreen credential is desired, use[KeyGenParameterSpec.Builder.setUserAuthenticationRequired(boolean)](https://developer.android.com/reference/kotlin/android/security/keystore/KeyGenParameterSpec.Builder#setUserAuthenticationRequired(kotlin.Boolean)). This flag will be ignored from Android S.*

Indicates that this key pair 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 this feature requires that the secure lock screen (e.g., password, PIN, pattern) is set up, otherwise key pair generation will fail. Moreover, this key pair will be deleted when the secure lock screen is disabled or reset (e.g., by the user or a Device Administrator). Finally, this key pair cannot be used until the user unlocks the secure lock screen after boot.

|                                                                           Return                                                                           ||
|-------------------------------------------------------------------------------------------------------------------------------|-----------------------------|
| [KeyPairGeneratorSpec.Builder](https://developer.android.com/reference/kotlin/android/security/KeyPairGeneratorSpec.Builder#) | This value cannot be`null`. |

**See Also**

- [android.app.KeyguardManager#isDeviceSecure()](https://developer.android.com/reference/kotlin/android/app/KeyguardManager.html#isDeviceSecure())  

### setEndDate

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

```
fun setEndDate(endDate: Date): KeyPairGeneratorSpec.Builder
```

**Deprecated:** *Deprecated in Java.*

Sets the end of the validity period for the self-signed certificate of the generated key pair.

|                                                     Parameters                                                     ||
|-----------|---------------------------------------------------------------------------------------------------------|
| `endDate` | [Date](https://developer.android.com/reference/kotlin/java/util/Date.html#):This value cannot be`null`. |

|                                                                           Return                                                                           ||
|-------------------------------------------------------------------------------------------------------------------------------|-----------------------------|
| [KeyPairGeneratorSpec.Builder](https://developer.android.com/reference/kotlin/android/security/KeyPairGeneratorSpec.Builder#) | This value cannot be`null`. |

### setKeySize

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

```
fun setKeySize(keySize: Int): KeyPairGeneratorSpec.Builder
```

**Deprecated:** *Deprecated in Java.*

Sets the key size for the keypair to be created. For instance, for a key type of RSA this will set the modulus size and for a key type of EC it will select a curve with a matching field size.

|                                                                           Return                                                                           ||
|-------------------------------------------------------------------------------------------------------------------------------|-----------------------------|
| [KeyPairGeneratorSpec.Builder](https://developer.android.com/reference/kotlin/android/security/KeyPairGeneratorSpec.Builder#) | This value cannot be`null`. |

### setKeyType

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

```
fun setKeyType(keyType: String): KeyPairGeneratorSpec.Builder
```

**Deprecated:** *Deprecated in Java.*

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

|                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               Parameters                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                ||
|-----------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `keyType` | [String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html):This value cannot be`null`. Value is[android.security.keystore.KeyProperties#KEY_ALGORITHM_RSA](https://developer.android.com/reference/kotlin/android/security/keystore/KeyProperties#KEY_ALGORITHM_RSA:kotlin.String),[android.security.keystore.KeyProperties#KEY_ALGORITHM_EC](https://developer.android.com/reference/kotlin/android/security/keystore/KeyProperties#KEY_ALGORITHM_EC:kotlin.String), android.security.keystore.KeyProperties.KEY_ALGORITHM_XDH,[android.security.keystore.KeyProperties#KEY_ALGORITHM_AES](https://developer.android.com/reference/kotlin/android/security/keystore/KeyProperties#KEY_ALGORITHM_AES:kotlin.String),[android.security.keystore.KeyProperties#KEY_ALGORITHM_HMAC_SHA1](https://developer.android.com/reference/kotlin/android/security/keystore/KeyProperties#KEY_ALGORITHM_HMAC_SHA1:kotlin.String),[android.security.keystore.KeyProperties#KEY_ALGORITHM_HMAC_SHA224](https://developer.android.com/reference/kotlin/android/security/keystore/KeyProperties#KEY_ALGORITHM_HMAC_SHA224:kotlin.String),[android.security.keystore.KeyProperties#KEY_ALGORITHM_HMAC_SHA256](https://developer.android.com/reference/kotlin/android/security/keystore/KeyProperties#KEY_ALGORITHM_HMAC_SHA256:kotlin.String),[android.security.keystore.KeyProperties#KEY_ALGORITHM_HMAC_SHA384](https://developer.android.com/reference/kotlin/android/security/keystore/KeyProperties#KEY_ALGORITHM_HMAC_SHA384:kotlin.String), or[android.security.keystore.KeyProperties#KEY_ALGORITHM_HMAC_SHA512](https://developer.android.com/reference/kotlin/android/security/keystore/KeyProperties#KEY_ALGORITHM_HMAC_SHA512:kotlin.String) |

|                                                                           Return                                                                           ||
|-------------------------------------------------------------------------------------------------------------------------------|-----------------------------|
| [KeyPairGeneratorSpec.Builder](https://developer.android.com/reference/kotlin/android/security/KeyPairGeneratorSpec.Builder#) | This value cannot be`null`. |

### setSerialNumber

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

```
fun setSerialNumber(serialNumber: BigInteger): KeyPairGeneratorSpec.Builder
```

**Deprecated:** *Deprecated in Java.*

Sets the serial number used for the self-signed certificate of the generated key pair.

|                                                             Parameters                                                              ||
|----------------|---------------------------------------------------------------------------------------------------------------------|
| `serialNumber` | [BigInteger](https://developer.android.com/reference/kotlin/java/math/BigInteger.html#):This value cannot be`null`. |

|                                                                           Return                                                                           ||
|-------------------------------------------------------------------------------------------------------------------------------|-----------------------------|
| [KeyPairGeneratorSpec.Builder](https://developer.android.com/reference/kotlin/android/security/KeyPairGeneratorSpec.Builder#) | This value cannot be`null`. |

### setStartDate

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

```
fun setStartDate(startDate: Date): KeyPairGeneratorSpec.Builder
```

**Deprecated:** *Deprecated in Java.*

Sets the start of the validity period for the self-signed certificate of the generated key pair.

|                                                      Parameters                                                      ||
|-------------|---------------------------------------------------------------------------------------------------------|
| `startDate` | [Date](https://developer.android.com/reference/kotlin/java/util/Date.html#):This value cannot be`null`. |

|                                                                           Return                                                                           ||
|-------------------------------------------------------------------------------------------------------------------------------|-----------------------------|
| [KeyPairGeneratorSpec.Builder](https://developer.android.com/reference/kotlin/android/security/KeyPairGeneratorSpec.Builder#) | This value cannot be`null`. |

### setSubject

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

```
fun setSubject(subject: X500Principal): KeyPairGeneratorSpec.Builder
```

**Deprecated:** *Deprecated in Java.*

Sets the subject used for the self-signed certificate of the generated key pair.

|                                                                     Parameters                                                                      ||
|-----------|------------------------------------------------------------------------------------------------------------------------------------------|
| `subject` | [X500Principal](https://developer.android.com/reference/kotlin/javax/security/auth/x500/X500Principal.html#):This value cannot be`null`. |

|                                                                           Return                                                                           ||
|-------------------------------------------------------------------------------------------------------------------------------|-----------------------------|
| [KeyPairGeneratorSpec.Builder](https://developer.android.com/reference/kotlin/android/security/KeyPairGeneratorSpec.Builder#) | This value cannot be`null`. |