# MediaDrm.CryptoSession

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

# CryptoSession

*** ** * ** ***

Kotlin\|[Java](https://developer.android.com/reference/android/media/MediaDrm.CryptoSession "View this page in Java")  

```
class CryptoSession
```

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

In addition to supporting decryption of DASH Common Encrypted Media, the MediaDrm APIs provide the ability to securely deliver session keys from an operator's session key server to a client device, based on the factory-installed root of trust, and then perform encrypt, decrypt, sign and verify operations with the session key on arbitrary user data.

The CryptoSession class implements generic encrypt/decrypt/sign/verify methods based on the established session keys. These keys are exchanged using the getKeyRequest/provideKeyResponse methods.

Applications of this capability could include securing various types of purchased or private content, such as applications, books and other media, photos or media delivery protocols.

Operators can create session key servers that are functionally similar to a license key server, except that instead of receiving license key requests and providing encrypted content keys which are used specifically to decrypt A/V media content, the session key server receives session key requests and provides encrypted session keys which can be used for general purpose crypto operations.

A CryptoSession is obtained using[getCryptoSession](https://developer.android.com/reference/kotlin/android/media/MediaDrm#getCryptoSession(kotlin.ByteArray,%20kotlin.String,%20kotlin.String))

## Summary

|                                                                                                                                                                                                                                                                                                              Public methods                                                                                                                                                                                                                                                                                                              ||
|-----------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [ByteArray](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-byte-array/index.html) | [decrypt](https://developer.android.com/reference/kotlin/android/media/MediaDrm.CryptoSession#decrypt(kotlin.ByteArray,%20kotlin.ByteArray,%20kotlin.ByteArray))`(`keyid:` `[ByteArray](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-byte-array/index.html)`, `input:` `[ByteArray](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-byte-array/index.html)`, `iv:` `[ByteArray](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-byte-array/index.html)`)` Decrypt data using the CryptoSessions's cipher algorithm            |
| [ByteArray](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-byte-array/index.html) | [encrypt](https://developer.android.com/reference/kotlin/android/media/MediaDrm.CryptoSession#encrypt(kotlin.ByteArray,%20kotlin.ByteArray,%20kotlin.ByteArray))`(`keyid:` `[ByteArray](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-byte-array/index.html)`, `input:` `[ByteArray](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-byte-array/index.html)`, `iv:` `[ByteArray](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-byte-array/index.html)`)` Encrypt data using the CryptoSession's cipher algorithm             |
| [ByteArray](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-byte-array/index.html) | [sign](https://developer.android.com/reference/kotlin/android/media/MediaDrm.CryptoSession#sign(kotlin.ByteArray,%20kotlin.ByteArray))`(`keyid:` `[ByteArray](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-byte-array/index.html)`, `message:` `[ByteArray](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-byte-array/index.html)`)` Sign data using the CryptoSessions's mac algorithm.                                                                                                                                          |
| [Boolean](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html)      | [verify](https://developer.android.com/reference/kotlin/android/media/MediaDrm.CryptoSession#verify(kotlin.ByteArray,%20kotlin.ByteArray,%20kotlin.ByteArray))`(`keyid:` `[ByteArray](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-byte-array/index.html)`, `message:` `[ByteArray](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-byte-array/index.html)`, `signature:` `[ByteArray](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-byte-array/index.html)`)` Verify a signature using the CryptoSessions's mac algorithm. |

## Public methods

### decrypt

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

```
fun decrypt(
    keyid: ByteArray, 
    input: ByteArray, 
    iv: ByteArray
): ByteArray
```

Decrypt data using the CryptoSessions's cipher algorithm

|                                                                                  Parameters                                                                                  ||
|---------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `keyid` | [ByteArray](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-byte-array/index.html):specifies which key to use This value cannot be`null`.                      |
| `input` | [ByteArray](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-byte-array/index.html):the data to encrypt This value cannot be`null`.                             |
| `iv`    | [ByteArray](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-byte-array/index.html):the initialization vector to use for the cipher This value cannot be`null`. |

|                                                        Return                                                        ||
|-----------------------------------------------------------------------------------------|-----------------------------|
| [ByteArray](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-byte-array/index.html) | This value cannot be`null`. |

### encrypt

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

```
fun encrypt(
    keyid: ByteArray, 
    input: ByteArray, 
    iv: ByteArray
): ByteArray
```

Encrypt data using the CryptoSession's cipher algorithm

|                                                                                  Parameters                                                                                  ||
|---------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `keyid` | [ByteArray](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-byte-array/index.html):specifies which key to use This value cannot be`null`.                      |
| `input` | [ByteArray](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-byte-array/index.html):the data to encrypt This value cannot be`null`.                             |
| `iv`    | [ByteArray](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-byte-array/index.html):the initialization vector to use for the cipher This value cannot be`null`. |

|                                                        Return                                                        ||
|-----------------------------------------------------------------------------------------|-----------------------------|
| [ByteArray](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-byte-array/index.html) | This value cannot be`null`. |

### sign

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

```
fun sign(
    keyid: ByteArray, 
    message: ByteArray
): ByteArray
```

Sign data using the CryptoSessions's mac algorithm.

|                                                                                   Parameters                                                                                    ||
|-----------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `keyid`   | [ByteArray](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-byte-array/index.html):specifies which key to use This value cannot be`null`.                       |
| `message` | [ByteArray](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-byte-array/index.html):the data for which a signature is to be computed This value cannot be`null`. |

|                                                        Return                                                        ||
|-----------------------------------------------------------------------------------------|-----------------------------|
| [ByteArray](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-byte-array/index.html) | This value cannot be`null`. |

### verify

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

```
fun verify(
    keyid: ByteArray, 
    message: ByteArray, 
    signature: ByteArray
): Boolean
```

Verify a signature using the CryptoSessions's mac algorithm. Return true if the signatures match, false if they do no.

|                                                                                                 Parameters                                                                                                  ||
|-------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `keyid`     | [ByteArray](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-byte-array/index.html):specifies which key to use This value cannot be`null`.                                                 |
| `message`   | [ByteArray](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-byte-array/index.html):the data to verify This value cannot be`null`.                                                         |
| `signature` | [ByteArray](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-byte-array/index.html):the reference signature which will be compared with the computed signature This value cannot be`null`. |