# KeyAgreementSpi

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/KeyAgreementSpi#pubctors)\|[Protected Methods](https://developer.android.com/reference/javax/crypto/KeyAgreementSpi#promethods)\|[Inherited Methods](https://developer.android.com/reference/javax/crypto/KeyAgreementSpi#inhmethods)  

# KeyAgreementSpi

`
public


abstract
class
KeyAgreementSpi
`  
`

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


``


`

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

<br />

*** ** * ** ***

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

The keys involved in establishing a shared secret are created by one of the key generators (`KeyPairGenerator`or`KeyGenerator`), a`KeyFactory`, or as a result from an intermediate phase of the key agreement protocol ([engineDoPhase](https://developer.android.com/reference/javax/crypto/KeyAgreementSpi#engineDoPhase(java.security.Key,%20boolean))).

For each of the correspondents in the key exchange,`engineDoPhase`needs to be called. For example, if the key exchange is with one other party,`engineDoPhase`needs to be called once, with the`lastPhase`flag set to`true`. If the key exchange is with two other parties,`engineDoPhase`needs to be called twice, the first time setting the`lastPhase`flag to`false`, and the second time setting it to`true`. There may be any number of parties involved in a key exchange.  
**See also:**

- [KeyGenerator](https://developer.android.com/reference/javax/crypto/KeyGenerator)
- [SecretKey](https://developer.android.com/reference/javax/crypto/SecretKey)

## Summary

|                                               ### Public constructors                                                ||
|-------------------------------------------------------------------------------------------------------------------|---|
| ` `[KeyAgreementSpi](https://developer.android.com/reference/javax/crypto/KeyAgreementSpi#KeyAgreementSpi())`() ` |

|                                                                                                                                                                                                                                                                                                                                      ### Protected methods                                                                                                                                                                                                                                                                                                                                      ||
|-----------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` abstract `[Key](https://developer.android.com/reference/java/security/Key)            | ` `[engineDoPhase](https://developer.android.com/reference/javax/crypto/KeyAgreementSpi#engineDoPhase(java.security.Key,%20boolean))`(`[Key](https://developer.android.com/reference/java/security/Key)` key, boolean lastPhase) ` Executes the next phase of this key agreement with the given key that was received from one of the other parties involved in this key agreement.                                                                                                                                                                                                                    |
| ` abstract byte[]`                                                                      | ` `[engineGenerateSecret](https://developer.android.com/reference/javax/crypto/KeyAgreementSpi#engineGenerateSecret())`() ` Generates the shared secret and returns it in a new buffer.                                                                                                                                                                                                                                                                                                                                                                                                                |
| ` abstract `[SecretKey](https://developer.android.com/reference/javax/crypto/SecretKey) | ` `[engineGenerateSecret](https://developer.android.com/reference/javax/crypto/KeyAgreementSpi#engineGenerateSecret(java.lang.String))`(`[String](https://developer.android.com/reference/java/lang/String)` algorithm) ` Creates the shared secret and returns it as a secret key object of the requested algorithm type.                                                                                                                                                                                                                                                                             |
| ` abstract int`                                                                         | ` `[engineGenerateSecret](https://developer.android.com/reference/javax/crypto/KeyAgreementSpi#engineGenerateSecret(byte[],%20int))`(byte[] sharedSecret, int offset) ` Generates the shared secret, and places it into the buffer`sharedSecret`, beginning at`offset`inclusive.                                                                                                                                                                                                                                                                                                                       |
| ` abstract void`                                                                        | ` `[engineInit](https://developer.android.com/reference/javax/crypto/KeyAgreementSpi#engineInit(java.security.Key,%20java.security.SecureRandom))`(`[Key](https://developer.android.com/reference/java/security/Key)` key, `[SecureRandom](https://developer.android.com/reference/java/security/SecureRandom)` random) ` Initializes this key agreement with the given key and source of randomness.                                                                                                                                                                                                  |
| ` abstract void`                                                                        | ` `[engineInit](https://developer.android.com/reference/javax/crypto/KeyAgreementSpi#engineInit(java.security.Key,%20java.security.spec.AlgorithmParameterSpec,%20java.security.SecureRandom))`(`[Key](https://developer.android.com/reference/java/security/Key)` key, `[AlgorithmParameterSpec](https://developer.android.com/reference/java/security/spec/AlgorithmParameterSpec)` params, `[SecureRandom](https://developer.android.com/reference/java/security/SecureRandom)` random) ` Initializes this key agreement with the given key, set of algorithm parameters, and source of randomness. |

| ### Inherited methods |
|-----------------------|---|
| From class` `[java.lang.Object](https://developer.android.com/reference/java/lang/Object)` ` |--------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` `[Object](https://developer.android.com/reference/java/lang/Object)          | ` `[clone](https://developer.android.com/reference/java/lang/Object#clone())`() ` Creates and returns a copy of this object.                                                                                                                                                      | | ` boolean`                                                                     | ` `[equals](https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object))`(`[Object](https://developer.android.com/reference/java/lang/Object)` obj) ` Indicates whether some other object is "equal to" this one.                                           | | ` void`                                                                        | ` `[finalize](https://developer.android.com/reference/java/lang/Object#finalize())`() ` Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.                                                          | | ` final `[Class](https://developer.android.com/reference/java/lang/Class)`<?>` | ` `[getClass](https://developer.android.com/reference/java/lang/Object#getClass())`() ` Returns the runtime class of this`Object`.                                                                                                                                                | | ` int`                                                                         | ` `[hashCode](https://developer.android.com/reference/java/lang/Object#hashCode())`() ` Returns a hash code value for the object.                                                                                                                                                 | | ` final void`                                                                  | ` `[notify](https://developer.android.com/reference/java/lang/Object#notify())`() ` Wakes up a single thread that is waiting on this object's monitor.                                                                                                                            | | ` final void`                                                                  | ` `[notifyAll](https://developer.android.com/reference/java/lang/Object#notifyAll())`() ` Wakes up all threads that are waiting on this object's monitor.                                                                                                                         | | ` `[String](https://developer.android.com/reference/java/lang/String)          | ` `[toString](https://developer.android.com/reference/java/lang/Object#toString())`() ` Returns a string representation of the object.                                                                                                                                            | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait(long,%20int))`(long timeoutMillis, int nanos) ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*, or until a certain amount of real time has elapsed. | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait(long))`(long timeoutMillis) ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*, or until a certain amount of real time has elapsed.                   | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait())`() ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*.                                                                                             | ||

## Public constructors

### KeyAgreementSpi

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

```
public KeyAgreementSpi ()
```

<br />

<br />

## Protected methods

### engineDoPhase

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

```
protected abstract Key engineDoPhase (Key key, 
                boolean lastPhase)
```

Executes the next phase of this key agreement with the given key that was received from one of the other parties involved in this key agreement.

<br />

|                                                                                 Parameters                                                                                  ||
|-------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `key`       | `Key`: the key for this phase. For example, in the case of Diffie-Hellman between 2 parties, this would be the other party's Diffie-Hellman public key. <br /> |
| `lastPhase` | `boolean`: flag which indicates whether or not this is the last phase of this key agreement. <br />                                                            |

|                                                                                Returns                                                                                ||
|------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------|
| [Key](https://developer.android.com/reference/java/security/Key) | the (intermediate) key resulting from this phase, or null if this phase does not yield a key <br /> |

|                                                                       Throws                                                                        ||
|--------------------------------------------------------------------------------------------------|---------------------------------------------------|
| [InvalidKeyException](https://developer.android.com/reference/java/security/InvalidKeyException) | if the given key is inappropriate for this phase. |
| [IllegalStateException](https://developer.android.com/reference/java/lang/IllegalStateException) | if this key agreement has not been initialized.   |

### engineGenerateSecret

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

```
protected abstract byte[] engineGenerateSecret ()
```

Generates the shared secret and returns it in a new buffer.

This method resets this`KeyAgreementSpi`object, so that it can be reused for further key agreements. Unless this key agreement is reinitialized with one of the`engineInit`methods, the same private information and algorithm parameters will be used for subsequent key agreements.

<br />

|                        Returns                         ||
|----------|----------------------------------------------|
| `byte[]` | the new buffer with the shared secret <br /> |

|                                                                       Throws                                                                       ||
|--------------------------------------------------------------------------------------------------|--------------------------------------------------|
| [IllegalStateException](https://developer.android.com/reference/java/lang/IllegalStateException) | if this key agreement has not been completed yet |

### engineGenerateSecret

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

```
protected abstract SecretKey engineGenerateSecret (String algorithm)
```

Creates the shared secret and returns it as a secret key object of the requested algorithm type.

This method resets this`KeyAgreementSpi`object, so that it can be reused for further key agreements. Unless this key agreement is reinitialized with one of the`engineInit`methods, the same private information and algorithm parameters will be used for subsequent key agreements.

<br />

|                            Parameters                            ||
|-------------|-----------------------------------------------------|
| `algorithm` | `String`: the requested secret key algorithm <br /> |

|                                                  Returns                                                  ||
|-----------------------------------------------------------------------------|------------------------------|
| [SecretKey](https://developer.android.com/reference/javax/crypto/SecretKey) | the shared secret key <br /> |

|                                                                                                                           Throws                                                                                                                            ||
|------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------|
| [IllegalStateException](https://developer.android.com/reference/java/lang/IllegalStateException)           | if this key agreement has not been completed yet                                                                                                |
| [NoSuchAlgorithmException](https://developer.android.com/reference/java/security/NoSuchAlgorithmException) | if the requested secret key algorithm is not available                                                                                          |
| [InvalidKeyException](https://developer.android.com/reference/java/security/InvalidKeyException)           | if the shared secret key material cannot be used to generate a secret key of the requested algorithm type (e.g., the key material is too short) |

### engineGenerateSecret

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

```
protected abstract int engineGenerateSecret (byte[] sharedSecret, 
                int offset)
```

Generates the shared secret, and places it into the buffer`sharedSecret`, beginning at`offset`inclusive.

If the`sharedSecret`buffer is too small to hold the result, a`ShortBufferException`is thrown. In this case, this call should be repeated with a larger output buffer.

This method resets this`KeyAgreementSpi`object, so that it can be reused for further key agreements. Unless this key agreement is reinitialized with one of the`engineInit`methods, the same private information and algorithm parameters will be used for subsequent key agreements.

<br />

|                                           Parameters                                            ||
|----------------|---------------------------------------------------------------------------------|
| `sharedSecret` | `byte`: the buffer for the shared secret <br />                                 |
| `offset`       | `int`: the offset in`sharedSecret`where the shared secret will be stored <br /> |

|                           Returns                           ||
|-------|------------------------------------------------------|
| `int` | the number of bytes placed into`sharedSecret` <br /> |

|                                                                            Throws                                                                             ||
|---------------------------------------------------------------------------------------------------|------------------------------------------------------------|
| [IllegalStateException](https://developer.android.com/reference/java/lang/IllegalStateException)  | if this key agreement has not been completed yet           |
| [ShortBufferException](https://developer.android.com/reference/javax/crypto/ShortBufferException) | if the given output buffer is too small to hold the secret |

### engineInit

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

```
protected abstract void engineInit (Key key, 
                SecureRandom random)
```

Initializes this key agreement with the given key and source of randomness. The given key is required to contain all the algorithm parameters required for this key agreement.

If the key agreement algorithm requires random bytes, it gets them from the given source of randomness,`random`. However, if the underlying algorithm implementation does not require any random bytes,`random`is ignored.

<br />

|                                                                                    Parameters                                                                                    ||
|----------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `key`    | `Key`: the party's private information. For example, in the case of the Diffie-Hellman key agreement, this would be the party's own Diffie-Hellman private key. <br /> |
| `random` | `SecureRandom`: the source of randomness <br />                                                                                                                        |

|                                                                                                            Throws                                                                                                             ||
|--------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------|
| [InvalidKeyException](https://developer.android.com/reference/java/security/InvalidKeyException) | if the given key is inappropriate for this key agreement, e.g., is of the wrong type or has an incompatible algorithm type. |

### engineInit

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

```
protected abstract void engineInit (Key key, 
                AlgorithmParameterSpec params, 
                SecureRandom random)
```

Initializes this key agreement with the given key, set of algorithm parameters, and source of randomness.

<br />

|                                                                                    Parameters                                                                                    ||
|----------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `key`    | `Key`: the party's private information. For example, in the case of the Diffie-Hellman key agreement, this would be the party's own Diffie-Hellman private key. <br /> |
| `params` | `AlgorithmParameterSpec`: the key agreement parameters <br />                                                                                                          |
| `random` | `SecureRandom`: the source of randomness <br />                                                                                                                        |

|                                                                                                                           Throws                                                                                                                            ||
|--------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------|
| [InvalidKeyException](https://developer.android.com/reference/java/security/InvalidKeyException)                               | if the given key is inappropriate for this key agreement, e.g., is of the wrong type or has an incompatible algorithm type. |
| [InvalidAlgorithmParameterException](https://developer.android.com/reference/java/security/InvalidAlgorithmParameterException) | if the given parameters are inappropriate for this key agreement.                                                           |