Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels) Summary: [Ctors](https://developer.android.com/reference/javax/crypto/spec/PBEKeySpec#pubctors) \| [Methods](https://developer.android.com/reference/javax/crypto/spec/PBEKeySpec#pubmethods) \| [Inherited Methods](https://developer.android.com/reference/javax/crypto/spec/PBEKeySpec#inhmethods)

# PBEKeySpec

` public class PBEKeySpec `   
` extends https://developer.android.com/reference/java/lang/Object ` ` implements https://developer.android.com/reference/java/security/spec/KeySpec `

|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | javax.crypto.spec.PBEKeySpec |

<br />

*** ** * ** ***

A user-chosen password that can be used with password-based encryption (*PBE*).

The password can be viewed as some kind of raw key material, from which the encryption mechanism that uses it derives a cryptographic key.

Different PBE mechanisms may consume different bits of each password character. For example, the PBE mechanism defined in [PKCS #5](http://www.ietf.org/rfc/rfc2898.txt) looks at only the low order 8 bits of each character, whereas PKCS #12 looks at all 16 bits of each character.

You convert the password characters to a PBE key by creating an instance of the appropriate secret-key factory. For example, a secret-key factory for PKCS #5 will construct a PBE key from only the low order 8 bits of each password character, whereas a secret-key factory for PKCS #12 will take all 16 bits of each character.

Also note that this class stores passwords as char arrays instead of `String` objects (which would seem more logical), because the String class is immutable and there is no way to overwrite its internal value when the password stored in it is no longer needed. Hence, this class requests the password as a char array, so it can be overwritten when done.
**See also:**

- `https://developer.android.com/reference/javax/crypto/SecretKeyFactory`
- `https://developer.android.com/reference/javax/crypto/spec/PBEParameterSpec`

## Summary

| ### Public constructors ||
|---|---|
| ` https://developer.android.com/reference/javax/crypto/spec/PBEKeySpec#PBEKeySpec(char[])(char[] password) ` Constructor that takes a password. |
| ` https://developer.android.com/reference/javax/crypto/spec/PBEKeySpec#PBEKeySpec(char[],%20byte[],%20int)(char[] password, byte[] salt, int iterationCount) ` Constructor that takes a password, salt, iteration count for generating PBEKey of fixed-key-size PBE ciphers. |
| ` https://developer.android.com/reference/javax/crypto/spec/PBEKeySpec#PBEKeySpec(char[],%20byte[],%20int,%20int)(char[] password, byte[] salt, int iterationCount, int keyLength) ` Constructor that takes a password, salt, iteration count, and to-be-derived key length for generating PBEKey of variable-key-size PBE ciphers. |

| ### Public methods ||
|---|---|
| ` final void` | ` https://developer.android.com/reference/javax/crypto/spec/PBEKeySpec#clearPassword()() ` Clears the internal copy of the password. |
| ` final int` | ` https://developer.android.com/reference/javax/crypto/spec/PBEKeySpec#getIterationCount()() ` Returns the iteration count or 0 if not specified. |
| ` final int` | ` https://developer.android.com/reference/javax/crypto/spec/PBEKeySpec#getKeyLength()() ` Returns the to-be-derived key length or 0 if not specified. |
| ` final char[]` | ` https://developer.android.com/reference/javax/crypto/spec/PBEKeySpec#getPassword()() ` Returns a copy of the password. |
| ` final byte[]` | ` https://developer.android.com/reference/javax/crypto/spec/PBEKeySpec#getSalt()() ` Returns a copy of the salt or null if not specified. |

### 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 constructors

### PBEKeySpec

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

```
public PBEKeySpec (char[] password)
```

Constructor that takes a password. An empty char\[\] is used if null is specified.

Note: `password` is cloned before it is stored in the new `PBEKeySpec` object.

<br />

| Parameters ||
|---|---|
| `password` | `char`: the password. <br /> |

### PBEKeySpec

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

```
public PBEKeySpec (char[] password, 
                byte[] salt, 
                int iterationCount)
```

Constructor that takes a password, salt, iteration count for generating PBEKey of fixed-key-size PBE ciphers. An empty char\[\] is used if null is specified for `password`.

Note: the `password` and `salt` are cloned before they are stored in the new `PBEKeySpec` object.

<br />

| Parameters ||
|---|---|
| `password` | `char`: the password. <br /> |
| `salt` | `byte`: the salt. <br /> |
| `iterationCount` | `int`: the iteration count. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalArgumentException` | if `salt` is empty, i.e. 0-length, or `iterationCount` is not positive. |
| `https://developer.android.com/reference/java/lang/NullPointerException` | if `salt` is null. |

### PBEKeySpec

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

```
public PBEKeySpec (char[] password, 
                byte[] salt, 
                int iterationCount, 
                int keyLength)
```

Constructor that takes a password, salt, iteration count, and to-be-derived key length for generating PBEKey of variable-key-size PBE ciphers. An empty char\[\] is used if null is specified for `password`.

Note: the `password` and `salt` are cloned before they are stored in the new `PBEKeySpec` object.

<br />

| Parameters ||
|---|---|
| `password` | `char`: the password. <br /> |
| `salt` | `byte`: the salt. <br /> |
| `iterationCount` | `int`: the iteration count. <br /> |
| `keyLength` | `int`: the to-be-derived key length. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalArgumentException` | if `salt` is empty, i.e. 0-length, `iterationCount` or `keyLength` is not positive. |
| `https://developer.android.com/reference/java/lang/NullPointerException` | if `salt` is null. |

## Public methods

### clearPassword

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

```
public final void clearPassword ()
```

Clears the internal copy of the password.

<br />

### getIterationCount

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

```
public final int getIterationCount ()
```

Returns the iteration count or 0 if not specified.

<br />

| Returns ||
|---|---|
| `int` | the iteration count. <br /> |

### getKeyLength

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

```
public final int getKeyLength ()
```

Returns the to-be-derived key length or 0 if not specified.

Note: this is used to indicate the preference on key length for variable-key-size ciphers. The actual key size depends on each provider's implementation.

<br />

| Returns ||
|---|---|
| `int` | the to-be-derived key length. <br /> |

### getPassword

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

```
public final char[] getPassword ()
```

Returns a copy of the password.

Note: this method returns a copy of the password. It is the caller's responsibility to zero out the password information after it is no longer needed.

<br />

| Returns ||
|---|---|
| `char[]` | the password. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalStateException` | if password has been cleared by calling `clearPassword` method. |

### getSalt

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

```
public final byte[] getSalt ()
```

Returns a copy of the salt or null if not specified.

Note: this method should return a copy of the salt. It is the caller's responsibility to zero out the salt information after it is no longer needed.

<br />

| Returns ||
|---|---|
| `byte[]` | the salt. <br /> |