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

# KeyStore

` public class KeyStore `   
` extends https://developer.android.com/reference/java/lang/Object ` ` `

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

<br />

*** ** * ** ***

This class represents a storage facility for cryptographic keys and certificates.

A `KeyStore` manages different types of entries. Each type of entry implements the `KeyStore.Entry` interface. Three basic `KeyStore.Entry` implementations are provided:

- **KeyStore.PrivateKeyEntry**

  This type of entry holds a cryptographic `PrivateKey`, which is optionally stored in a protected format to prevent unauthorized access. It is also accompanied by a certificate chain for the corresponding public key.

  Private keys and certificate chains are used by a given entity for self-authentication. Applications for this authentication include software distribution organizations which sign JAR files as part of releasing and/or licensing software.
- **KeyStore.SecretKeyEntry**

  This type of entry holds a cryptographic `SecretKey`, which is optionally stored in a protected format to prevent unauthorized access.
- **KeyStore.TrustedCertificateEntry**

  This type of entry contains a single public key `Certificate` belonging to another party. It is called a *trusted certificate* because the keystore owner trusts that the public key in the certificate indeed belongs to the identity identified by the *subject* (owner) of the certificate.

  This type of entry can be used to authenticate other parties.

Each entry in a keystore is identified by an "alias" string. In the case of private keys and their associated certificate chains, these strings distinguish among the different ways in which the entity may authenticate itself. For example, the entity may authenticate itself using different certificate authorities, or using different public key algorithms.

Whether aliases are case sensitive is implementation dependent. In order to avoid problems, it is recommended not to use aliases in a KeyStore that only differ in case.

Whether keystores are persistent, and the mechanisms used by the keystore if it is persistent, are not specified here. This allows use of a variety of techniques for protecting sensitive (e.g., private or secret) keys. Smart cards or other integrated cryptographic engines (SafeKeyper) are one option, and simpler mechanisms such as files may also be used (in a variety of formats).

Typical ways to request a KeyStore object include specifying an existing keystore file, relying on the default type and providing a specific keystore type.

- To specify an existing keystore file:

  ```
      // get keystore password
      char[] password = getPassword();

      // probe the keystore file and load the keystore entries
      KeyStore ks = KeyStore.getInstance(new File("keyStoreName"), password);
  ```
  The system will probe the specified file to determine its keystore type and return a keystore implementation with its entries already loaded. When this approach is used there is no need to call the keystore's `https://developer.android.com/reference/java/security/KeyStore#load(java.io.InputStream,%20char[])` method.
- To rely on the default type:

  ```
      KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
   
  ```
  The system will return a keystore implementation for the default type.
- To provide a specific keystore type:

  ```
        KeyStore ks = KeyStore.getInstance("JKS");
   
  ```
  The system will return the most preferred implementation of the specified keystore type available in the environment.

Before a keystore can be accessed, it must be `https://developer.android.com/reference/java/security/KeyStore#load(java.io.InputStream,%20char[])`.

```
    KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());

    // get user password and file input stream
    char[] password = getPassword();

    try (FileInputStream fis = new FileInputStream("keyStoreName")) {
        ks.load(fis, password);
    }
 
```
To create an empty keystore using the above `load` method, pass `null` as the `InputStream` argument.

Once the keystore has been loaded, it is possible to read existing entries from the keystore, or to write new entries into the keystore:

```
    KeyStore.ProtectionParameter protParam =
        new KeyStore.PasswordProtection(password);

    // get my private key
    KeyStore.PrivateKeyEntry pkEntry = (KeyStore.PrivateKeyEntry)
        ks.getEntry("privateKeyAlias", protParam);
    PrivateKey myPrivateKey = pkEntry.getPrivateKey();

    // save my secret key
    javax.crypto.SecretKey mySecretKey;
    KeyStore.SecretKeyEntry skEntry =
        new KeyStore.SecretKeyEntry(mySecretKey);
    ks.setEntry("secretKeyAlias", skEntry, protParam);

    // store away the keystore
    try (FileOutputStream fos = new FileOutputStream("newKeyStoreName")) {
        ks.store(fos, password);
    }
 
```
Note that although the same password may be used to load the keystore, to protect the private key entry, to protect the secret key entry, and to store the keystore (as is shown in the sample code above), different passwords or other protection parameters may also be used.

Android provides the following `KeyStore` types:

| Algorithm | Supported API Levels |
|---|---|
| AndroidCAStore | 14+ |
| AndroidKeyStore | 18+ |
| BCPKCS12 | 1-8 |
| BKS | 1+ |
| BouncyCastle | 1+ |
| PKCS12 | 1+ |
| PKCS12-DEF | 1-8 |

These types are described in the [KeyStore section](https://docs.oracle.com/javase/8/docs/technotes/guides/security/StandardNames.html#KeyStore) of the Java Cryptography Architecture Standard Algorithm Name Documentation.

<br />

**See also:**

- `https://developer.android.com/reference/java/security/PrivateKey`
- `https://developer.android.com/reference/javax/crypto/SecretKey`
- `https://developer.android.com/reference/java/security/cert/Certificate`

## Summary

| ### Nested classes ||
|---|---|
| ` class` | `https://developer.android.com/reference/java/security/KeyStore.Builder` A description of a to-be-instantiated KeyStore object. |
| ` class` | `https://developer.android.com/reference/java/security/KeyStore.CallbackHandlerProtection` A ProtectionParameter encapsulating a CallbackHandler. |
| ` interface` | `https://developer.android.com/reference/java/security/KeyStore.Entry` A marker interface for `KeyStore` entry types. |
| ` interface` | `https://developer.android.com/reference/java/security/KeyStore.LoadStoreParameter` A marker interface for `KeyStore` `https://developer.android.com/reference/java/security/KeyStore#load(java.security.KeyStore.LoadStoreParameter)` and `https://developer.android.com/reference/java/security/KeyStore#store(java.security.KeyStore.LoadStoreParameter)` parameters. |
| ` class` | `https://developer.android.com/reference/java/security/KeyStore.PasswordProtection` A password-based implementation of `ProtectionParameter`. |
| ` class` | `https://developer.android.com/reference/java/security/KeyStore.PrivateKeyEntry` A `KeyStore` entry that holds a `PrivateKey` and corresponding certificate chain. |
| ` interface` | `https://developer.android.com/reference/java/security/KeyStore.ProtectionParameter` A marker interface for keystore protection parameters. |
| ` class` | `https://developer.android.com/reference/java/security/KeyStore.SecretKeyEntry` A `KeyStore` entry that holds a `SecretKey`. |
| ` class` | `https://developer.android.com/reference/java/security/KeyStore.TrustedCertificateEntry` A `KeyStore` entry that holds a trusted `Certificate`. |

| ### Protected constructors ||
|---|---|
| ` https://developer.android.com/reference/java/security/KeyStore#KeyStore(java.security.KeyStoreSpi,%20java.security.Provider,%20java.lang.String)(https://developer.android.com/reference/java/security/KeyStoreSpi keyStoreSpi, https://developer.android.com/reference/java/security/Provider provider, https://developer.android.com/reference/java/lang/String type) ` Creates a KeyStore object of the given type, and encapsulates the given provider implementation (SPI object) in it. |

| ### Public methods ||
|---|---|
| ` final https://developer.android.com/reference/java/util/Enumeration<https://developer.android.com/reference/java/lang/String>` | ` https://developer.android.com/reference/java/security/KeyStore#aliases()() ` Lists all the alias names of this keystore. |
| ` final boolean` | ` https://developer.android.com/reference/java/security/KeyStore#containsAlias(java.lang.String)(https://developer.android.com/reference/java/lang/String alias) ` Checks if the given alias exists in this keystore. |
| ` final void` | ` https://developer.android.com/reference/java/security/KeyStore#deleteEntry(java.lang.String)(https://developer.android.com/reference/java/lang/String alias) ` Deletes the entry identified by the given alias from this keystore. |
| ` final boolean` | ` https://developer.android.com/reference/java/security/KeyStore#entryInstanceOf(java.lang.String,%20java.lang.Class<?%20extends%20java.security.KeyStore.Entry>)(https://developer.android.com/reference/java/lang/String alias, https://developer.android.com/reference/java/lang/Class<? extends https://developer.android.com/reference/java/security/KeyStore.Entry> entryClass) ` Determines if the keystore `Entry` for the specified `alias` is an instance or subclass of the specified `entryClass`. |
| ` final https://developer.android.com/reference/java/security/cert/Certificate` | ` https://developer.android.com/reference/java/security/KeyStore#getCertificate(java.lang.String)(https://developer.android.com/reference/java/lang/String alias) ` Returns the certificate associated with the given alias. |
| ` final https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/java/security/KeyStore#getCertificateAlias(java.security.cert.Certificate)(https://developer.android.com/reference/java/security/cert/Certificate cert) ` Returns the (alias) name of the first keystore entry whose certificate matches the given certificate. |
| ` final https://developer.android.com/reference/java/security/cert/Certificate` | ` https://developer.android.com/reference/java/security/KeyStore#getCertificateChain(java.lang.String)(https://developer.android.com/reference/java/lang/String alias) ` Returns the certificate chain associated with the given alias. |
| ` final https://developer.android.com/reference/java/util/Date` | ` https://developer.android.com/reference/java/security/KeyStore#getCreationDate(java.lang.String)(https://developer.android.com/reference/java/lang/String alias) ` Returns the creation date of the entry identified by the given alias. |
| ` static final https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/java/security/KeyStore#getDefaultType()() ` Returns the default keystore type as specified by the `keystore.type` security property, or the string "jks" (acronym for "Java keystore") if no such property exists. |
| ` final https://developer.android.com/reference/java/security/KeyStore.Entry` | ` https://developer.android.com/reference/java/security/KeyStore#getEntry(java.lang.String,%20java.security.KeyStore.ProtectionParameter)(https://developer.android.com/reference/java/lang/String alias, https://developer.android.com/reference/java/security/KeyStore.ProtectionParameter protParam) ` Gets a keystore `Entry` for the specified alias with the specified protection parameter. |
| ` static https://developer.android.com/reference/java/security/KeyStore` | ` https://developer.android.com/reference/java/security/KeyStore#getInstance(java.lang.String)(https://developer.android.com/reference/java/lang/String type) ` Returns a keystore object of the specified type. |
| ` static final https://developer.android.com/reference/java/security/KeyStore` | ` https://developer.android.com/reference/java/security/KeyStore#getInstance(java.io.File,%20char[])(https://developer.android.com/reference/java/io/File file, char[] password) ` Returns a loaded keystore object of the appropriate keystore type. |
| ` static https://developer.android.com/reference/java/security/KeyStore` | ` https://developer.android.com/reference/java/security/KeyStore#getInstance(java.lang.String,%20java.lang.String)(https://developer.android.com/reference/java/lang/String type, https://developer.android.com/reference/java/lang/String provider) ` Returns a keystore object of the specified type. |
| ` static https://developer.android.com/reference/java/security/KeyStore` | ` https://developer.android.com/reference/java/security/KeyStore#getInstance(java.lang.String,%20java.security.Provider)(https://developer.android.com/reference/java/lang/String type, https://developer.android.com/reference/java/security/Provider provider) ` Returns a keystore object of the specified type. |
| ` static final https://developer.android.com/reference/java/security/KeyStore` | ` https://developer.android.com/reference/java/security/KeyStore#getInstance(java.io.File,%20java.security.KeyStore.LoadStoreParameter)(https://developer.android.com/reference/java/io/File file, https://developer.android.com/reference/java/security/KeyStore.LoadStoreParameter param) ` Returns a loaded keystore object of the appropriate keystore type. |
| ` final https://developer.android.com/reference/java/security/Key` | ` https://developer.android.com/reference/java/security/KeyStore#getKey(java.lang.String,%20char[])(https://developer.android.com/reference/java/lang/String alias, char[] password) ` Returns the key associated with the given alias, using the given password to recover it. |
| ` final https://developer.android.com/reference/java/security/Provider` | ` https://developer.android.com/reference/java/security/KeyStore#getProvider()() ` Returns the provider of this keystore. |
| ` final https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/java/security/KeyStore#getType()() ` Returns the type of this keystore. |
| ` final boolean` | ` https://developer.android.com/reference/java/security/KeyStore#isCertificateEntry(java.lang.String)(https://developer.android.com/reference/java/lang/String alias) ` Returns true if the entry identified by the given alias was created by a call to `setCertificateEntry`, or created by a call to `setEntry` with a `TrustedCertificateEntry`. |
| ` final boolean` | ` https://developer.android.com/reference/java/security/KeyStore#isKeyEntry(java.lang.String)(https://developer.android.com/reference/java/lang/String alias) ` Returns true if the entry identified by the given alias was created by a call to `setKeyEntry`, or created by a call to `setEntry` with a `PrivateKeyEntry` or a `SecretKeyEntry`. |
| ` final void` | ` https://developer.android.com/reference/java/security/KeyStore#load(java.security.KeyStore.LoadStoreParameter)(https://developer.android.com/reference/java/security/KeyStore.LoadStoreParameter param) ` Loads this keystore using the given `LoadStoreParameter`. |
| ` final void` | ` https://developer.android.com/reference/java/security/KeyStore#load(java.io.InputStream,%20char[])(https://developer.android.com/reference/java/io/InputStream stream, char[] password) ` Loads this KeyStore from the given input stream. |
| ` final void` | ` https://developer.android.com/reference/java/security/KeyStore#setCertificateEntry(java.lang.String,%20java.security.cert.Certificate)(https://developer.android.com/reference/java/lang/String alias, https://developer.android.com/reference/java/security/cert/Certificate cert) ` Assigns the given trusted certificate to the given alias. |
| ` final void` | ` https://developer.android.com/reference/java/security/KeyStore#setEntry(java.lang.String,%20java.security.KeyStore.Entry,%20java.security.KeyStore.ProtectionParameter)(https://developer.android.com/reference/java/lang/String alias, https://developer.android.com/reference/java/security/KeyStore.Entry entry, https://developer.android.com/reference/java/security/KeyStore.ProtectionParameter protParam) ` Saves a keystore `Entry` under the specified alias. |
| ` final void` | ` https://developer.android.com/reference/java/security/KeyStore#setKeyEntry(java.lang.String,%20java.security.Key,%20char[],%20java.security.cert.Certificate[])(https://developer.android.com/reference/java/lang/String alias, https://developer.android.com/reference/java/security/Key key, char[] password, https://developer.android.com/reference/java/security/cert/Certificate chain) ` Assigns the given key to the given alias, protecting it with the given password. |
| ` final void` | ` https://developer.android.com/reference/java/security/KeyStore#setKeyEntry(java.lang.String,%20byte[],%20java.security.cert.Certificate[])(https://developer.android.com/reference/java/lang/String alias, byte[] key, https://developer.android.com/reference/java/security/cert/Certificate chain) ` Assigns the given key (that has already been protected) to the given alias. |
| ` final int` | ` https://developer.android.com/reference/java/security/KeyStore#size()() ` Retrieves the number of entries in this keystore. |
| ` final void` | ` https://developer.android.com/reference/java/security/KeyStore#store(java.io.OutputStream,%20char[])(https://developer.android.com/reference/java/io/OutputStream stream, char[] password) ` Stores this keystore to the given output stream, and protects its integrity with the given password. |
| ` final void` | ` https://developer.android.com/reference/java/security/KeyStore#store(java.security.KeyStore.LoadStoreParameter)(https://developer.android.com/reference/java/security/KeyStore.LoadStoreParameter param) ` Stores this keystore using the given `LoadStoreParameter`. |

### 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*.

## Protected constructors

### KeyStore

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

```
protected KeyStore (KeyStoreSpi keyStoreSpi, 
                Provider provider, 
                String type)
```

Creates a KeyStore object of the given type, and encapsulates the given provider implementation (SPI object) in it.

<br />

| Parameters ||
|---|---|
| `keyStoreSpi` | `KeyStoreSpi`: the provider implementation. <br /> |
| `provider` | `Provider`: the provider. <br /> |
| `type` | `String`: the keystore type. <br /> |

## Public methods

### aliases

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

```
public final Enumeration<String> aliases ()
```

Lists all the alias names of this keystore.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/util/Enumeration<https://developer.android.com/reference/java/lang/String>` | enumeration of the alias names <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/security/KeyStoreException` | if the keystore has not been initialized (loaded). |

### containsAlias

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

```
public final boolean containsAlias (String alias)
```

Checks if the given alias exists in this keystore.

<br />

| Parameters ||
|---|---|
| `alias` | `String`: the alias name <br /> |

| Returns ||
|---|---|
| `boolean` | true if the alias exists, false otherwise <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/security/KeyStoreException` | if the keystore has not been initialized (loaded). |

### deleteEntry

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

```
public final void deleteEntry (String alias)
```

Deletes the entry identified by the given alias from this keystore.

<br />

| Parameters ||
|---|---|
| `alias` | `String`: the alias name <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/security/KeyStoreException` | if the keystore has not been initialized, or if the entry cannot be removed. |

### entryInstanceOf

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

```
public final boolean entryInstanceOf (String alias, 
                Class<? extends KeyStore.Entry> entryClass)
```

Determines if the keystore `Entry` for the specified `alias` is an instance or subclass of the specified `entryClass`.

<br />

| Parameters ||
|---|---|
| `alias` | `String`: the alias name <br /> |
| `entryClass` | `Class`: the entry class <br /> |

| Returns ||
|---|---|
| `boolean` | true if the keystore `Entry` for the specified `alias` is an instance or subclass of the specified `entryClass`, false otherwise <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/NullPointerException` | if `alias` or `entryClass` is `null` |
| `https://developer.android.com/reference/java/security/KeyStoreException` | if the keystore has not been initialized (loaded) |

### getCertificate

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

```
public final Certificate getCertificate (String alias)
```

Returns the certificate associated with the given alias.

If the given alias name identifies an entry created by a call to `setCertificateEntry`, or created by a call to `setEntry` with a `TrustedCertificateEntry`, then the trusted certificate contained in that entry is returned.

If the given alias name identifies an entry created by a call to `setKeyEntry`, or created by a call to `setEntry` with a `PrivateKeyEntry`, then the first element of the certificate chain in that entry is returned.

<br />

| Parameters ||
|---|---|
| `alias` | `String`: the alias name <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/security/cert/Certificate` | the certificate, or null if the given alias does not exist or does not contain a certificate. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/security/KeyStoreException` | if the keystore has not been initialized (loaded). |

### getCertificateAlias

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

```
public final String getCertificateAlias (Certificate cert)
```

Returns the (alias) name of the first keystore entry whose certificate matches the given certificate.

This method attempts to match the given certificate with each keystore entry. If the entry being considered was created by a call to `setCertificateEntry`, or created by a call to `setEntry` with a `TrustedCertificateEntry`, then the given certificate is compared to that entry's certificate.

If the entry being considered was created by a call to `setKeyEntry`, or created by a call to `setEntry` with a `PrivateKeyEntry`, then the given certificate is compared to the first element of that entry's certificate chain.

<br />

| Parameters ||
|---|---|
| `cert` | `Certificate`: the certificate to match with. <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/lang/String` | the alias name of the first entry with a matching certificate, or null if no such entry exists in this keystore. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/security/KeyStoreException` | if the keystore has not been initialized (loaded). |

### getCertificateChain

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

```
public final Certificate[] getCertificateChain (String alias)
```

Returns the certificate chain associated with the given alias. The certificate chain must have been associated with the alias by a call to `setKeyEntry`, or by a call to `setEntry` with a `PrivateKeyEntry`.

<br />

| Parameters ||
|---|---|
| `alias` | `String`: the alias name <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/security/cert/Certificate` | the certificate chain (ordered with the user's certificate first followed by zero or more certificate authorities), or null if the given alias does not exist or does not contain a certificate chain <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/security/KeyStoreException` | if the keystore has not been initialized (loaded). |

### getCreationDate

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

```
public final Date getCreationDate (String alias)
```

Returns the creation date of the entry identified by the given alias.

<br />

| Parameters ||
|---|---|
| `alias` | `String`: the alias name <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/util/Date` | the creation date of this entry, or null if the given alias does not exist <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/security/KeyStoreException` | if the keystore has not been initialized (loaded). |

### getDefaultType

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

```
public static final String getDefaultType ()
```

Returns the default keystore type as specified by the `keystore.type` security property, or the string "jks" (acronym for "Java keystore") if no such property exists.

The default keystore type can be used by applications that do not want to use a hard-coded keystore type when calling one of the `getInstance` methods, and want to provide a default keystore type in case a user does not specify its own.

The default keystore type can be changed by setting the value of the `keystore.type` security property to the desired keystore type.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/lang/String` | the default keystore type as specified by the `keystore.type` security property, or the string "jks" if no such property exists. <br /> |

**See also:**

- `https://developer.android.com/reference/java/security/Security`

### getEntry

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

```
public final KeyStore.Entry getEntry (String alias, 
                KeyStore.ProtectionParameter protParam)
```

Gets a keystore `Entry` for the specified alias with the specified protection parameter.

<br />

| Parameters ||
|---|---|
| `alias` | `String`: get the keystore `Entry` for this alias <br /> |
| `protParam` | `KeyStore.ProtectionParameter`: the `ProtectionParameter` used to protect the `Entry`, which may be `null` <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/security/KeyStore.Entry` | the keystore `Entry` for the specified alias, or `null` if there is no such entry <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/NullPointerException` | if `alias` is `null` |
| `https://developer.android.com/reference/java/security/KeyStoreException` | if the keystore has not been initialized (loaded). |
| `https://developer.android.com/reference/java/security/NoSuchAlgorithmException` | if the algorithm for recovering the entry cannot be found |
| `https://developer.android.com/reference/java/security/UnrecoverableEntryException` | if the specified `protParam` were insufficient or invalid |
| `https://developer.android.com/reference/java/security/UnrecoverableKeyException` | if the entry is a `PrivateKeyEntry` or `SecretKeyEntry` and the specified `protParam` does not contain the information needed to recover the key (e.g. wrong password) |

**See also:**

- `https://developer.android.com/reference/java/security/KeyStore#setEntry(java.lang.String,%20java.security.KeyStore.Entry,%20java.security.KeyStore.ProtectionParameter)`

### getInstance

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

```
public static KeyStore getInstance (String type)
```

Returns a keystore object of the specified type.

This method traverses the list of registered security Providers, starting with the most preferred Provider. A new KeyStore object encapsulating the KeyStoreSpi implementation from the first Provider that supports the specified type is returned.

Note that the list of registered providers may be retrieved via the `https://developer.android.com/reference/java/security/Security#getProviders()` method.

<br />

| Parameters ||
|---|---|
| `type` | `String`: the type of keystore. See the KeyStore section in the [Java Security Standard Algorithm Names Specification](https://docs.oracle.com/en/java/javase/17/docs/specs/security/standard-names.html#keystore-types) for information about standard keystore types. <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/security/KeyStore` | a keystore object of the specified type <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/security/KeyStoreException` | if no Provider supports a KeyStoreSpi implementation for the specified type. |

**See also:**

- `https://developer.android.com/reference/java/security/Provider`

### getInstance

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

```
public static final KeyStore getInstance (File file, 
                char[] password)
```

Returns a loaded keystore object of the appropriate keystore type. First the keystore type is determined by probing the specified file. Then a keystore object is instantiated and loaded using the data from that file.

A password may be given to unlock the keystore (e.g. the keystore resides on a hardware token device), or to check the integrity of the keystore data. If a password is not given for integrity checking, then integrity checking is not performed.

This method traverses the list of registered security [providers](https://developer.android.com/reference/java/security/Provider), starting with the most preferred Provider. For each `https://developer.android.com/reference/java/security/KeyStoreSpi` implementation supported by a Provider, it invokes the `https://developer.android.com/reference/java/security/KeyStoreSpi#engineProbe(java.io.InputStream)` method to determine if it supports the specified keystore. A new KeyStore object is returned that encapsulates the KeyStoreSpi implementation from the first Provider that supports the specified file.

Note that the list of registered providers may be retrieved via the `https://developer.android.com/reference/java/security/Security#getProviders()` method.

<br />

| Parameters ||
|---|---|
| `file` | `File`: the keystore file <br /> |
| `password` | `char`: the keystore password, which may be `null` <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/security/KeyStore` | a keystore object loaded with keystore data <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` | if there is an I/O or format problem with the keystore data, if a password is required but not given, or if the given password was incorrect. If the error is due to a wrong password, the `https://developer.android.com/reference/java/lang/Throwable#getCause()` of the `IOException` should be an `UnrecoverableKeyException`. |
| `https://developer.android.com/reference/java/lang/IllegalArgumentException` | if file does not exist or does not refer to a normal file. |
| `https://developer.android.com/reference/java/lang/NullPointerException` | if file is `null`. |
| `https://developer.android.com/reference/java/lang/SecurityException` | if a security manager exists and its `https://developer.android.com/reference/java/lang/SecurityManager#checkRead(java.io.FileDescriptor)` method denies read access to the specified file. |
| `https://developer.android.com/reference/java/security/KeyStoreException` | if no Provider supports a KeyStoreSpi implementation for the specified keystore file. |
| `https://developer.android.com/reference/java/security/NoSuchAlgorithmException` | if the algorithm used to check the integrity of the keystore cannot be found. |
| `https://developer.android.com/reference/java/security/cert/CertificateException` | if any of the certificates in the keystore could not be loaded. |

**See also:**

- `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 KeyStore getInstance (String type, 
                String provider)
```

Returns a keystore object of the specified type.

A new KeyStore object encapsulating the KeyStoreSpi 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://developer.android.com/reference/java/security/Security#getProviders()` method.

<br />

| Parameters ||
|---|---|
| `type` | `String`: the type of keystore. See the KeyStore section in the [Java Security Standard Algorithm Names Specification](https://docs.oracle.com/en/java/javase/17/docs/specs/security/standard-names.html#keystore-types) for information about standard keystore types. <br /> |
| `provider` | `String`: the name of the provider. <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/security/KeyStore` | a keystore object of the specified type <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalArgumentException` | if the provider name is null or empty. |
| `https://developer.android.com/reference/java/security/KeyStoreException` | if a KeyStoreSpi implementation for the specified type is not available from the specified provider. |
| `https://developer.android.com/reference/java/security/NoSuchProviderException` | if the specified provider is not registered in the security provider list. |

**See also:**

- `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 KeyStore getInstance (String type, 
                Provider provider)
```

Returns a keystore object of the specified type.

A new KeyStore object encapsulating the KeyStoreSpi 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 ||
|---|---|
| `type` | `String`: the type of keystore. See the KeyStore section in the [Java Security Standard Algorithm Names Specification](https://docs.oracle.com/en/java/javase/17/docs/specs/security/standard-names.html#keystore-types) for information about standard keystore types. <br /> |
| `provider` | `Provider`: the provider. <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/security/KeyStore` | a keystore object of the specified type <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalArgumentException` | if the specified provider is `null` |
| `https://developer.android.com/reference/java/lang/NullPointerException` | if `type` is `null` |
| `https://developer.android.com/reference/java/security/KeyStoreException` | if `KeyStoreSpi` implementation for the specified type is not available from the specified `Provider` object |

**See also:**

- `https://developer.android.com/reference/java/security/Provider`

### getInstance

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

```
public static final KeyStore getInstance (File file, 
                KeyStore.LoadStoreParameter param)
```

Returns a loaded keystore object of the appropriate keystore type. First the keystore type is determined by probing the specified file. Then a keystore object is instantiated and loaded using the data from that file. A `LoadStoreParameter` may be supplied which specifies how to unlock the keystore data or perform an integrity check.

This method traverses the list of registered security [providers](https://developer.android.com/reference/java/security/Provider), starting with the most preferred Provider. For each `https://developer.android.com/reference/java/security/KeyStoreSpi` implementation supported by a Provider, it invokes the `https://developer.android.com/reference/java/security/KeyStoreSpi#engineProbe(java.io.InputStream)` method to determine if it supports the specified keystore. A new KeyStore object is returned that encapsulates the KeyStoreSpi implementation from the first Provider that supports the specified file.

Note that the list of registered providers may be retrieved via the `https://developer.android.com/reference/java/security/Security#getProviders()` method.

<br />

| Parameters ||
|---|---|
| `file` | `File`: the keystore file <br /> |
| `param` | `KeyStore.LoadStoreParameter`: the `LoadStoreParameter` that specifies how to load the keystore, which may be `null` <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/security/KeyStore` | a keystore object loaded with keystore data <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` | if there is an I/O or format problem with the keystore data. If the error is due to an incorrect `ProtectionParameter` (e.g. wrong password) the `https://developer.android.com/reference/java/lang/Throwable#getCause()` of the `IOException` should be an `UnrecoverableKeyException`. |
| `https://developer.android.com/reference/java/lang/IllegalArgumentException` | if file does not exist or does not refer to a normal file, or if param is not recognized. |
| `https://developer.android.com/reference/java/lang/NullPointerException` | if file is `null`. |
| `https://developer.android.com/reference/java/lang/SecurityException` | if a security manager exists and its `https://developer.android.com/reference/java/lang/SecurityManager#checkRead(java.io.FileDescriptor)` method denies read access to the specified file. |
| `https://developer.android.com/reference/java/security/KeyStoreException` | if no Provider supports a KeyStoreSpi implementation for the specified keystore file. |
| `https://developer.android.com/reference/java/security/NoSuchAlgorithmException` | if the algorithm used to check the integrity of the keystore cannot be found. |
| `https://developer.android.com/reference/java/security/cert/CertificateException` | if any of the certificates in the keystore could not be loaded. |

**See also:**

- `https://developer.android.com/reference/java/security/Provider`

### getKey

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

```
public final Key getKey (String alias, 
                char[] password)
```

Returns the key associated with the given alias, using the given password to recover it. The key must have been associated with the alias by a call to `setKeyEntry`, or by a call to `setEntry` with a `PrivateKeyEntry` or `SecretKeyEntry`.

<br />

| Parameters ||
|---|---|
| `alias` | `String`: the alias name <br /> |
| `password` | `char`: the password for recovering the key <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/security/Key` | the requested key, or null if the given alias does not exist or does not identify a key-related entry. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/security/KeyStoreException` | if the keystore has not been initialized (loaded). |
| `https://developer.android.com/reference/java/security/NoSuchAlgorithmException` | if the algorithm for recovering the key cannot be found |
| `https://developer.android.com/reference/java/security/UnrecoverableKeyException` | if the key cannot be recovered (e.g., the given password is wrong). |

### 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 keystore.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/security/Provider` | the provider of this keystore. <br /> |

### getType

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

```
public final String getType ()
```

Returns the type of this keystore.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/lang/String` | the type of this keystore. <br /> |

### isCertificateEntry

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

```
public final boolean isCertificateEntry (String alias)
```

Returns true if the entry identified by the given alias was created by a call to `setCertificateEntry`, or created by a call to `setEntry` with a `TrustedCertificateEntry`.

<br />

| Parameters ||
|---|---|
| `alias` | `String`: the alias for the keystore entry to be checked <br /> |

| Returns ||
|---|---|
| `boolean` | true if the entry identified by the given alias contains a trusted certificate, false otherwise. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/security/KeyStoreException` | if the keystore has not been initialized (loaded). |

### isKeyEntry

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

```
public final boolean isKeyEntry (String alias)
```

Returns true if the entry identified by the given alias was created by a call to `setKeyEntry`, or created by a call to `setEntry` with a `PrivateKeyEntry` or a `SecretKeyEntry`.

<br />

| Parameters ||
|---|---|
| `alias` | `String`: the alias for the keystore entry to be checked <br /> |

| Returns ||
|---|---|
| `boolean` | true if the entry identified by the given alias is a key-related entry, false otherwise. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/security/KeyStoreException` | if the keystore has not been initialized (loaded). |

### load

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

```
public final void load (KeyStore.LoadStoreParameter param)
```

Loads this keystore using the given `LoadStoreParameter`.

Note that if this KeyStore has already been loaded, it is reinitialized and loaded again from the given parameter.

<br />

| Parameters ||
|---|---|
| `param` | `KeyStore.LoadStoreParameter`: the `LoadStoreParameter` that specifies how to load the keystore, which may be `null` <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` | if there is an I/O or format problem with the keystore data. If the error is due to an incorrect `ProtectionParameter` (e.g. wrong password) the `https://developer.android.com/reference/java/lang/Throwable#getCause()` of the `IOException` should be an `UnrecoverableKeyException` |
| `https://developer.android.com/reference/java/lang/IllegalArgumentException` | if the given `LoadStoreParameter` input is not recognized |
| `https://developer.android.com/reference/java/security/NoSuchAlgorithmException` | if the algorithm used to check the integrity of the keystore cannot be found |
| `https://developer.android.com/reference/java/security/cert/CertificateException` | if any of the certificates in the keystore could not be loaded |

### load

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

```
public final void load (InputStream stream, 
                char[] password)
```

Loads this KeyStore from the given input stream.

A password may be given to unlock the keystore (e.g. the keystore resides on a hardware token device), or to check the integrity of the keystore data. If a password is not given for integrity checking, then integrity checking is not performed.

In order to create an empty keystore, or if the keystore cannot be initialized from a stream, pass `null` as the `stream` argument.

Note that if this keystore has already been loaded, it is reinitialized and loaded again from the given input stream.

<br />

| Parameters ||
|---|---|
| `stream` | `InputStream`: the input stream from which the keystore is loaded, or `null` <br /> |
| `password` | `char`: the password used to check the integrity of the keystore, the password used to unlock the keystore, or `null` <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` | if there is an I/O or format problem with the keystore data, if a password is required but not given, or if the given password was incorrect. If the error is due to a wrong password, the `https://developer.android.com/reference/java/lang/Throwable#getCause()` of the `IOException` should be an `UnrecoverableKeyException` |
| `https://developer.android.com/reference/java/security/NoSuchAlgorithmException` | if the algorithm used to check the integrity of the keystore cannot be found |
| `https://developer.android.com/reference/java/security/cert/CertificateException` | if any of the certificates in the keystore could not be loaded |

### setCertificateEntry

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

```
public final void setCertificateEntry (String alias, 
                Certificate cert)
```

Assigns the given trusted certificate to the given alias.

If the given alias identifies an existing entry created by a call to `setCertificateEntry`, or created by a call to `setEntry` with a `TrustedCertificateEntry`, the trusted certificate in the existing entry is overridden by the given certificate.

<br />

| Parameters ||
|---|---|
| `alias` | `String`: the alias name <br /> |
| `cert` | `Certificate`: the certificate <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/security/KeyStoreException` | if the keystore has not been initialized, or the given alias already exists and does not identify an entry containing a trusted certificate, or this operation fails for some other reason. |

### setEntry

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

```
public final void setEntry (String alias, 
                KeyStore.Entry entry, 
                KeyStore.ProtectionParameter protParam)
```

Saves a keystore `Entry` under the specified alias. The protection parameter is used to protect the `Entry`.

If an entry already exists for the specified alias, it is overridden.

<br />

| Parameters ||
|---|---|
| `alias` | `String`: save the keystore `Entry` under this alias <br /> |
| `entry` | `KeyStore.Entry`: the `Entry` to save <br /> |
| `protParam` | `KeyStore.ProtectionParameter`: the `ProtectionParameter` used to protect the `Entry`, which may be `null` <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/NullPointerException` | if `alias` or `entry` is `null` |
| `https://developer.android.com/reference/java/security/KeyStoreException` | if the keystore has not been initialized (loaded), or if this operation fails for some other reason |

**See also:**

- `https://developer.android.com/reference/java/security/KeyStore#getEntry(java.lang.String,%20java.security.KeyStore.ProtectionParameter)`

### setKeyEntry

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

```
public final void setKeyEntry (String alias, 
                Key key, 
                char[] password, 
                Certificate[] chain)
```

Assigns the given key to the given alias, protecting it with the given password.

If the given key is of type `java.security.PrivateKey`, it must be accompanied by a certificate chain certifying the corresponding public key.

If the given alias already exists, the keystore information associated with it is overridden by the given key (and possibly certificate chain).

<br />

| Parameters ||
|---|---|
| `alias` | `String`: the alias name <br /> |
| `key` | `Key`: the key to be associated with the alias <br /> |
| `password` | `char`: the password to protect the key <br /> |
| `chain` | `Certificate`: the certificate chain for the corresponding public key (only required if the given key is of type `java.security.PrivateKey`). <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/security/KeyStoreException` | if the keystore has not been initialized (loaded), the given key cannot be protected, or this operation fails for some other reason |

### setKeyEntry

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

```
public final void setKeyEntry (String alias, 
                byte[] key, 
                Certificate[] chain)
```

Assigns the given key (that has already been protected) to the given alias.

If the protected key is of type `java.security.PrivateKey`, it must be accompanied by a certificate chain certifying the corresponding public key. If the underlying keystore implementation is of type `jks`, `key` must be encoded as an `EncryptedPrivateKeyInfo` as defined in the PKCS #8 standard.

If the given alias already exists, the keystore information associated with it is overridden by the given key (and possibly certificate chain).

<br />

| Parameters ||
|---|---|
| `alias` | `String`: the alias name <br /> |
| `key` | `byte`: the key (in protected format) to be associated with the alias <br /> |
| `chain` | `Certificate`: the certificate chain for the corresponding public key (only useful if the protected key is of type `java.security.PrivateKey`). <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/security/KeyStoreException` | if the keystore has not been initialized (loaded), or if this operation fails for some other reason. |

### size

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

```
public final int size ()
```

Retrieves the number of entries in this keystore.

<br />

| Returns ||
|---|---|
| `int` | the number of entries in this keystore <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/security/KeyStoreException` | if the keystore has not been initialized (loaded). |

### store

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

```
public final void store (OutputStream stream, 
                char[] password)
```

Stores this keystore to the given output stream, and protects its integrity with the given password.

<br />

| Parameters ||
|---|---|
| `stream` | `OutputStream`: the output stream to which this keystore is written. <br /> |
| `password` | `char`: the password to generate the keystore integrity check <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` | if there was an I/O problem with data |
| `https://developer.android.com/reference/java/security/KeyStoreException` | if the keystore has not been initialized (loaded). |
| `https://developer.android.com/reference/java/security/NoSuchAlgorithmException` | if the appropriate data integrity algorithm could not be found |
| `https://developer.android.com/reference/java/security/cert/CertificateException` | if any of the certificates included in the keystore data could not be stored |

### store

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

```
public final void store (KeyStore.LoadStoreParameter param)
```

Stores this keystore using the given `LoadStoreParameter`.

<br />

| Parameters ||
|---|---|
| `param` | `KeyStore.LoadStoreParameter`: the `LoadStoreParameter` that specifies how to store the keystore, which may be `null` <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` | if there was an I/O problem with data |
| `https://developer.android.com/reference/java/lang/IllegalArgumentException` | if the given `LoadStoreParameter` input is not recognized |
| `https://developer.android.com/reference/java/security/KeyStoreException` | if the keystore has not been initialized (loaded) |
| `https://developer.android.com/reference/java/security/NoSuchAlgorithmException` | if the appropriate data integrity algorithm could not be found |
| `https://developer.android.com/reference/java/security/cert/CertificateException` | if any of the certificates included in the keystore data could not be stored |