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

# ExemptionMechanism

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

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

<br />

*** ** * ** ***

This class provides the functionality of an exemption mechanism, examples of which are *key recovery*, *key weakening*, and *key escrow*.

Applications or applets that use an exemption mechanism may be granted stronger encryption capabilities than those which don't.

## Summary

| ### Protected constructors ||
|---|---|
| ` https://developer.android.com/reference/javax/crypto/ExemptionMechanism#ExemptionMechanism(javax.crypto.ExemptionMechanismSpi,%20java.security.Provider,%20java.lang.String)(https://developer.android.com/reference/javax/crypto/ExemptionMechanismSpi exmechSpi, https://developer.android.com/reference/java/security/Provider provider, https://developer.android.com/reference/java/lang/String mechanism) ` Creates a ExemptionMechanism object. |

| ### Public methods ||
|---|---|
| ` final int` | ` https://developer.android.com/reference/javax/crypto/ExemptionMechanism#genExemptionBlob(byte[])(byte[] output) ` Generates the exemption mechanism key blob, and stores the result in the `output` buffer. |
| ` final byte[]` | ` https://developer.android.com/reference/javax/crypto/ExemptionMechanism#genExemptionBlob()() ` Generates the exemption mechanism key blob. |
| ` final int` | ` https://developer.android.com/reference/javax/crypto/ExemptionMechanism#genExemptionBlob(byte[],%20int)(byte[] output, int outputOffset) ` Generates the exemption mechanism key blob, and stores the result in the `output` buffer, starting at `outputOffset` inclusive. |
| ` static final https://developer.android.com/reference/javax/crypto/ExemptionMechanism` | ` https://developer.android.com/reference/javax/crypto/ExemptionMechanism#getInstance(java.lang.String)(https://developer.android.com/reference/java/lang/String algorithm) ` Returns an `ExemptionMechanism` object that implements the specified exemption mechanism algorithm. |
| ` static final https://developer.android.com/reference/javax/crypto/ExemptionMechanism` | ` https://developer.android.com/reference/javax/crypto/ExemptionMechanism#getInstance(java.lang.String,%20java.lang.String)(https://developer.android.com/reference/java/lang/String algorithm, https://developer.android.com/reference/java/lang/String provider) ` Returns an `ExemptionMechanism` object that implements the specified exemption mechanism algorithm. |
| ` static final https://developer.android.com/reference/javax/crypto/ExemptionMechanism` | ` https://developer.android.com/reference/javax/crypto/ExemptionMechanism#getInstance(java.lang.String,%20java.security.Provider)(https://developer.android.com/reference/java/lang/String algorithm, https://developer.android.com/reference/java/security/Provider provider) ` Returns an `ExemptionMechanism` object that implements the specified exemption mechanism algorithm. |
| ` final https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/javax/crypto/ExemptionMechanism#getName()() ` Returns the exemption mechanism name of this `ExemptionMechanism` object. |
| ` final int` | ` https://developer.android.com/reference/javax/crypto/ExemptionMechanism#getOutputSize(int)(int inputLen) ` Returns the length in bytes that an output buffer would need to be in order to hold the result of the next `https://developer.android.com/reference/javax/crypto/ExemptionMechanism#genExemptionBlob(byte[])` operation, given the input length `inputLen` (in bytes). |
| ` final https://developer.android.com/reference/java/security/Provider` | ` https://developer.android.com/reference/javax/crypto/ExemptionMechanism#getProvider()() ` Returns the provider of this `ExemptionMechanism` object. |
| ` final void` | ` https://developer.android.com/reference/javax/crypto/ExemptionMechanism#init(java.security.Key,%20java.security.AlgorithmParameters)(https://developer.android.com/reference/java/security/Key key, https://developer.android.com/reference/java/security/AlgorithmParameters params) ` Initializes this exemption mechanism with a key and a set of algorithm parameters. |
| ` final void` | ` https://developer.android.com/reference/javax/crypto/ExemptionMechanism#init(java.security.Key,%20java.security.spec.AlgorithmParameterSpec)(https://developer.android.com/reference/java/security/Key key, https://developer.android.com/reference/java/security/spec/AlgorithmParameterSpec params) ` Initializes this exemption mechanism with a key and a set of algorithm parameters. |
| ` final void` | ` https://developer.android.com/reference/javax/crypto/ExemptionMechanism#init(java.security.Key)(https://developer.android.com/reference/java/security/Key key) ` Initializes this exemption mechanism with a key. |
| ` final boolean` | ` https://developer.android.com/reference/javax/crypto/ExemptionMechanism#isCryptoAllowed(java.security.Key)(https://developer.android.com/reference/java/security/Key key) ` Returns whether the result blob has been generated successfully by this exemption mechanism. |

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

### ExemptionMechanism

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

```
protected ExemptionMechanism (ExemptionMechanismSpi exmechSpi, 
                Provider provider, 
                String mechanism)
```

Creates a ExemptionMechanism object.

<br />

| Parameters ||
|---|---|
| `exmechSpi` | `ExemptionMechanismSpi`: the delegate <br /> |
| `provider` | `Provider`: the provider <br /> |
| `mechanism` | `String`: the exemption mechanism <br /> |

## Public methods

### genExemptionBlob

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

```
public final int genExemptionBlob (byte[] output)
```

Generates the exemption mechanism key blob, and stores the result in the `output` buffer.

If the `output` buffer is too small to hold the result, a `ShortBufferException` is thrown. In this case, repeat this call with a larger output buffer. Use `https://developer.android.com/reference/javax/crypto/ExemptionMechanism#getOutputSize(int)` to determine how big the output buffer should be.

<br />

| Parameters ||
|---|---|
| `output` | `byte`: the buffer for the result <br /> |

| Returns ||
|---|---|
| `int` | the number of bytes stored in `output` <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalStateException` | if this exemption mechanism is in a wrong state (e.g., has not been initialized). |
| `https://developer.android.com/reference/javax/crypto/ExemptionMechanismException` | if problem(s) encountered in the process of generating. |
| `https://developer.android.com/reference/javax/crypto/ShortBufferException` | if the given output buffer is too small to hold the result. |

### genExemptionBlob

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

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

Generates the exemption mechanism key blob.

<br />

| Returns ||
|---|---|
| `byte[]` | the new buffer with the result key blob. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalStateException` | if this exemption mechanism is in a wrong state (e.g., has not been initialized). |
| `https://developer.android.com/reference/javax/crypto/ExemptionMechanismException` | if problem(s) encountered in the process of generating. |

### genExemptionBlob

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

```
public final int genExemptionBlob (byte[] output, 
                int outputOffset)
```

Generates the exemption mechanism key blob, and stores the result in the `output` buffer, starting at `outputOffset` inclusive.

If the `output` buffer is too small to hold the result, a `ShortBufferException` is thrown. In this case, repeat this call with a larger output buffer. Use `https://developer.android.com/reference/javax/crypto/ExemptionMechanism#getOutputSize(int)` to determine how big the output buffer should be.

<br />

| Parameters ||
|---|---|
| `output` | `byte`: the buffer for the result <br /> |
| `outputOffset` | `int`: the offset in `output` where the result is stored <br /> |

| Returns ||
|---|---|
| `int` | the number of bytes stored in `output` <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalStateException` | if this exemption mechanism is in a wrong state (e.g., has not been initialized). |
| `https://developer.android.com/reference/javax/crypto/ExemptionMechanismException` | if problem(s) encountered in the process of generating. |
| `https://developer.android.com/reference/javax/crypto/ShortBufferException` | if the given output buffer is too small to hold the result. |

### getInstance

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

```
public static final ExemptionMechanism getInstance (String algorithm)
```

Returns an `ExemptionMechanism` object that implements the specified exemption mechanism algorithm.

This method traverses the list of registered security Providers, starting with the most preferred Provider. A new ExemptionMechanism object encapsulating the ExemptionMechanismSpi implementation from the first Provider that supports the specified algorithm 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 ||
|---|---|
| `algorithm` | `String`: the standard name of the requested exemption mechanism. See the ExemptionMechanism section in the [Java Cryptography Architecture Standard Algorithm Name Documentation](https://docs.oracle.com/javase/8/docs/technotes/guides/security/StandardNames.html#Exemption) for information about standard exemption mechanism names. <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/javax/crypto/ExemptionMechanism` | the new `ExemptionMechanism` object. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/NullPointerException` | if `algorithm` is null. |
| `https://developer.android.com/reference/java/security/NoSuchAlgorithmException` | if no Provider supports an ExemptionMechanismSpi implementation for the specified algorithm. |

**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 final ExemptionMechanism getInstance (String algorithm, 
                String provider)
```

Returns an `ExemptionMechanism` object that implements the specified exemption mechanism algorithm.

A new ExemptionMechanism object encapsulating the ExemptionMechanismSpi 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 ||
|---|---|
| `algorithm` | `String`: the standard name of the requested exemption mechanism. See the ExemptionMechanism section in the [Java Cryptography Architecture Standard Algorithm Name Documentation](https://docs.oracle.com/javase/8/docs/technotes/guides/security/StandardNames.html#Exemption) for information about standard exemption mechanism names. <br /> |
| `provider` | `String`: the name of the provider. <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/javax/crypto/ExemptionMechanism` | the new `ExemptionMechanism` object. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalArgumentException` | if the `provider` is null or empty. |
| `https://developer.android.com/reference/java/lang/NullPointerException` | if `algorithm` is null. |
| `https://developer.android.com/reference/java/security/NoSuchAlgorithmException` | if an ExemptionMechanismSpi implementation for the specified algorithm 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 final ExemptionMechanism getInstance (String algorithm, 
                Provider provider)
```

Returns an `ExemptionMechanism` object that implements the specified exemption mechanism algorithm.

A new ExemptionMechanism object encapsulating the ExemptionMechanismSpi 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 ||
|---|---|
| `algorithm` | `String`: the standard name of the requested exemption mechanism. See the ExemptionMechanism section in the [Java Cryptography Architecture Standard Algorithm Name Documentation](https://docs.oracle.com/javase/8/docs/technotes/guides/security/StandardNames.html#Exemption) for information about standard exemption mechanism names. <br /> |
| `provider` | `Provider`: the provider. <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/javax/crypto/ExemptionMechanism` | the new `ExemptionMechanism` object. <br /> |

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

**See also:**

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

### getName

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

```
public final String getName ()
```

Returns the exemption mechanism name of this `ExemptionMechanism` object.

This is the same name that was specified in one of the `getInstance` calls that created this `ExemptionMechanism` object.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/lang/String` | the exemption mechanism name of this `ExemptionMechanism` object. <br /> |

### getOutputSize

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

```
public final int getOutputSize (int inputLen)
```

Returns the length in bytes that an output buffer would need to be in order to hold the result of the next `https://developer.android.com/reference/javax/crypto/ExemptionMechanism#genExemptionBlob(byte[])` operation, given the input length `inputLen` (in bytes).

The actual output length of the next `https://developer.android.com/reference/javax/crypto/ExemptionMechanism#genExemptionBlob(byte[])` call may be smaller than the length returned by this method.

<br />

| Parameters ||
|---|---|
| `inputLen` | `int`: the input length (in bytes) <br /> |

| Returns ||
|---|---|
| `int` | the required output buffer size (in bytes) <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalStateException` | if this exemption mechanism is in a wrong state (e.g., has not yet been initialized) |

### 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 `ExemptionMechanism` object.

<br />

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

### init

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

```
public final void init (Key key, 
                AlgorithmParameters params)
```

Initializes this exemption mechanism with a key and a set of algorithm parameters.

If this exemption mechanism requires any algorithm parameters and `params` is null, the underlying exemption mechanism implementation is supposed to generate the required parameters itself (using provider-specific default values); in the case that algorithm parameters must be specified by the caller, an `InvalidAlgorithmParameterException` is raised.

<br />

| Parameters ||
|---|---|
| `key` | `Key`: the key for this exemption mechanism <br /> |
| `params` | `AlgorithmParameters`: the algorithm parameters <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/security/InvalidAlgorithmParameterException` | if the given algorithm parameters are inappropriate for this exemption mechanism. |
| `https://developer.android.com/reference/java/security/InvalidKeyException` | if the given key is inappropriate for this exemption mechanism. |
| `https://developer.android.com/reference/javax/crypto/ExemptionMechanismException` | if problem(s) encountered in the process of initializing. |

### init

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

```
public final void init (Key key, 
                AlgorithmParameterSpec params)
```

Initializes this exemption mechanism with a key and a set of algorithm parameters.

If this exemption mechanism requires any algorithm parameters and `params` is null, the underlying exemption mechanism implementation is supposed to generate the required parameters itself (using provider-specific default values); in the case that algorithm parameters must be specified by the caller, an `InvalidAlgorithmParameterException` is raised.

<br />

| Parameters ||
|---|---|
| `key` | `Key`: the key for this exemption mechanism <br /> |
| `params` | `AlgorithmParameterSpec`: the algorithm parameters <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/security/InvalidAlgorithmParameterException` | if the given algorithm parameters are inappropriate for this exemption mechanism. |
| `https://developer.android.com/reference/java/security/InvalidKeyException` | if the given key is inappropriate for this exemption mechanism. |
| `https://developer.android.com/reference/javax/crypto/ExemptionMechanismException` | if problem(s) encountered in the process of initializing. |

### init

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

```
public final void init (Key key)
```

Initializes this exemption mechanism with a key.

If this exemption mechanism requires any algorithm parameters that cannot be derived from the given `key`, the underlying exemption mechanism implementation is supposed to generate the required parameters itself (using provider-specific default values); in the case that algorithm parameters must be specified by the caller, an `InvalidKeyException` is raised.

<br />

| Parameters ||
|---|---|
| `key` | `Key`: the key for this exemption mechanism <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/security/InvalidKeyException` | if the given key is inappropriate for this exemption mechanism. |
| `https://developer.android.com/reference/javax/crypto/ExemptionMechanismException` | if problem(s) encountered in the process of initializing. |

### isCryptoAllowed

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

```
public final boolean isCryptoAllowed (Key key)
```

Returns whether the result blob has been generated successfully by this exemption mechanism.

The method also makes sure that the key passed in is the same as the one this exemption mechanism used in initializing and generating phases.

<br />

| Parameters ||
|---|---|
| `key` | `Key`: the key the crypto is going to use. <br /> |

| Returns ||
|---|---|
| `boolean` | whether the result blob of the same key has been generated successfully by this exemption mechanism; false if `key` is null. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/javax/crypto/ExemptionMechanismException` | if problem(s) encountered while determining whether the result blob has been generated successfully by this exemption mechanism object. |