# MediaCodec.CryptoInfo

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

# CryptoInfo

*** ** * ** ***

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

```
class CryptoInfo
```

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

Metadata describing the structure of an encrypted input sample.

A buffer's data is considered to be partitioned into "subSamples". Each subSample starts with a run of plain, unencrypted bytes followed by a run of encrypted bytes. Either of these runs may be empty. If pattern encryption applies, each of the encrypted runs is encrypted only partly, according to a repeating pattern of "encrypt" and "skip" blocks.[numBytesOfClearData](https://developer.android.com/reference/kotlin/android/media/MediaCodec.CryptoInfo#numBytesOfClearData:kotlin.IntArray)can be null to indicate that all data is encrypted, and[numBytesOfEncryptedData](https://developer.android.com/reference/kotlin/android/media/MediaCodec.CryptoInfo#numBytesOfEncryptedData:kotlin.IntArray)can be null to indicate that all data is clear. At least one of[numBytesOfClearData](https://developer.android.com/reference/kotlin/android/media/MediaCodec.CryptoInfo#numBytesOfClearData:kotlin.IntArray)and[numBytesOfEncryptedData](https://developer.android.com/reference/kotlin/android/media/MediaCodec.CryptoInfo#numBytesOfEncryptedData:kotlin.IntArray)must be non-null.

This information encapsulates per-sample metadata as outlined in ISO/IEC FDIS 23001-7:2016 "Common encryption in ISO base media file format files".

<br />

### ISO-CENC Schemes

ISO/IEC FDIS 23001-7:2016 defines four possible schemes by which media may be encrypted, corresponding to each possible combination of an AES mode with the presence or absence of patterned encryption.

|                  | AES-CTR | AES-CBC |
| Without Patterns |  cenc   |  cbc1   |
|  With Patterns   |  cens   |  cbcs   |
|------------------|---------|---------|

For`CryptoInfo`, the scheme is selected implicitly by the combination of the[mode](https://developer.android.com/reference/kotlin/android/media/MediaCodec.CryptoInfo#mode:kotlin.Int)field and the value set with[setPattern](https://developer.android.com/reference/kotlin/android/media/MediaCodec.CryptoInfo#setPattern(android.media.MediaCodec.CryptoInfo.Pattern)). For the pattern, setting the pattern to all zeroes (that is, both`blocksToEncrypt`and`blocksToSkip`are zero) is interpreted as turning patterns off completely. A scheme that does not use patterns will be selected, either cenc or cbc1. Setting the pattern to any nonzero value will choose one of the pattern-supporting schemes, cens or cbcs. The default pattern if[setPattern](https://developer.android.com/reference/kotlin/android/media/MediaCodec.CryptoInfo#setPattern(android.media.MediaCodec.CryptoInfo.Pattern))is never called is all zeroes.

<br />

#### HLS SAMPLE-AES Audio

HLS SAMPLE-AES audio is encrypted in a manner compatible with the cbcs scheme, except that it does not use patterned encryption. However, if[setPattern](https://developer.android.com/reference/kotlin/android/media/MediaCodec.CryptoInfo#setPattern(android.media.MediaCodec.CryptoInfo.Pattern))is used to set the pattern to all zeroes, this will be interpreted as selecting the cbc1 scheme. The cbc1 scheme cannot successfully decrypt HLS SAMPLE-AES audio because of differences in how the IVs are handled. For this reason, it is recommended that a pattern of`1`encrypted block and`0`skip blocks be used with HLS SAMPLE-AES audio. This will trigger decryption to use cbcs mode while still decrypting every block.

## Summary

|                                                                                       Nested classes                                                                                       ||
|---|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|   | [Pattern](https://developer.android.com/reference/kotlin/android/media/MediaCodec.CryptoInfo.Pattern) Metadata describing an encryption pattern for the protected bytes in a subsample. |

|                                                     Public constructors                                                     ||
|--------------------------------------------------------------------------------------------------------------------------|---|
| [CryptoInfo](https://developer.android.com/reference/kotlin/android/media/MediaCodec.CryptoInfo#CryptoInfo())`()` <br /> |

|                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    Public methods                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    ||
|-----------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [MediaCodec.CryptoInfo.Pattern](https://developer.android.com/reference/kotlin/android/media/MediaCodec.CryptoInfo.Pattern) | [getPattern](https://developer.android.com/reference/kotlin/android/media/MediaCodec.CryptoInfo#getPattern())`()` Returns the[encryption pattern](https://developer.android.com/reference/kotlin/android/media/MediaCodec.CryptoInfo.Pattern).                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
| [Unit](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html)                                                | [set](https://developer.android.com/reference/kotlin/android/media/MediaCodec.CryptoInfo#set(kotlin.Int,%20kotlin.IntArray,%20kotlin.IntArray,%20kotlin.ByteArray,%20kotlin.ByteArray,%20kotlin.Int))`(`newNumSubSamples:` `[Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html)`, `newNumBytesOfClearData:` `[IntArray](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int-array/index.html)`, `newNumBytesOfEncryptedData:` `[IntArray](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int-array/index.html)`, `newKey:` `[ByteArray](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-byte-array/index.html)`, `newIV:` `[ByteArray](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-byte-array/index.html)`, `newMode:` `[Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html)`)` Set the subsample count, clear/encrypted sizes, key, IV and mode fields of a[MediaCodec.CryptoInfo](https://developer.android.com/reference/kotlin/android/media/MediaCodec.CryptoInfo#)instance. |
| [Unit](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html)                                                | [setPattern](https://developer.android.com/reference/kotlin/android/media/MediaCodec.CryptoInfo#setPattern(android.media.MediaCodec.CryptoInfo.Pattern))`(`newPattern:` `[MediaCodec.CryptoInfo.Pattern](https://developer.android.com/reference/kotlin/android/media/MediaCodec.CryptoInfo.Pattern)!`)` Set the encryption pattern on a[MediaCodec.CryptoInfo](https://developer.android.com/reference/kotlin/android/media/MediaCodec.CryptoInfo#)instance.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| [String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)                                            | [toString](https://developer.android.com/reference/kotlin/android/media/MediaCodec.CryptoInfo#toString())`()` <br />                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    |

|                                                                                                                                                                                                                                                                                                                        Properties                                                                                                                                                                                                                                                                                                                         ||
|------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [ByteArray](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-byte-array/index.html)! | [iv](https://developer.android.com/reference/kotlin/android/media/MediaCodec.CryptoInfo#iv:kotlin.ByteArray) A 16-byte initialization vector                                                                                                                                                                                                                                                                                                                                                                                                                    |
| [ByteArray](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-byte-array/index.html)! | [key](https://developer.android.com/reference/kotlin/android/media/MediaCodec.CryptoInfo#key:kotlin.ByteArray) A 16-byte key id                                                                                                                                                                                                                                                                                                                                                                                                                                 |
| [Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html)               | [mode](https://developer.android.com/reference/kotlin/android/media/MediaCodec.CryptoInfo#mode:kotlin.Int) The type of encryption that has been applied, see[CRYPTO_MODE_UNENCRYPTED](https://developer.android.com/reference/kotlin/android/media/MediaCodec#CRYPTO_MODE_UNENCRYPTED:kotlin.Int),[CRYPTO_MODE_AES_CTR](https://developer.android.com/reference/kotlin/android/media/MediaCodec#CRYPTO_MODE_AES_CTR:kotlin.Int)and[CRYPTO_MODE_AES_CBC](https://developer.android.com/reference/kotlin/android/media/MediaCodec#CRYPTO_MODE_AES_CBC:kotlin.Int) |
| [IntArray](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int-array/index.html)!   | [numBytesOfClearData](https://developer.android.com/reference/kotlin/android/media/MediaCodec.CryptoInfo#numBytesOfClearData:kotlin.IntArray) The number of leading unencrypted bytes in each subSample.                                                                                                                                                                                                                                                                                                                                                        |
| [IntArray](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int-array/index.html)!   | [numBytesOfEncryptedData](https://developer.android.com/reference/kotlin/android/media/MediaCodec.CryptoInfo#numBytesOfEncryptedData:kotlin.IntArray) The number of trailing encrypted bytes in each subSample.                                                                                                                                                                                                                                                                                                                                                 |
| [Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html)               | [numSubSamples](https://developer.android.com/reference/kotlin/android/media/MediaCodec.CryptoInfo#numSubSamples:kotlin.Int) The number of subSamples that make up the buffer's contents.                                                                                                                                                                                                                                                                                                                                                                       |

## Public constructors

### CryptoInfo

```
CryptoInfo()
```

## Public methods

### getPattern

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

```
fun getPattern(): MediaCodec.CryptoInfo.Pattern
```

Returns the[encryption pattern](https://developer.android.com/reference/kotlin/android/media/MediaCodec.CryptoInfo.Pattern).

|                                                                          Return                                                                          ||
|-----------------------------------------------------------------------------------------------------------------------------|-----------------------------|
| [MediaCodec.CryptoInfo.Pattern](https://developer.android.com/reference/kotlin/android/media/MediaCodec.CryptoInfo.Pattern) | This value cannot be`null`. |

### set

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

```
fun set(
    newNumSubSamples: Int, 
    newNumBytesOfClearData: IntArray, 
    newNumBytesOfEncryptedData: IntArray, 
    newKey: ByteArray, 
    newIV: ByteArray, 
    newMode: Int
): Unit
```

Set the subsample count, clear/encrypted sizes, key, IV and mode fields of a[MediaCodec.CryptoInfo](https://developer.android.com/reference/kotlin/android/media/MediaCodec.CryptoInfo#)instance.

|                                                                    Parameters                                                                     ||
|------------------------------|---------------------------------------------------------------------------------------------------------------------|
| `newNumBytesOfClearData`     | [IntArray](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int-array/index.html):This value cannot be`null`.   |
| `newNumBytesOfEncryptedData` | [IntArray](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int-array/index.html):This value cannot be`null`.   |
| `newKey`                     | [ByteArray](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-byte-array/index.html):This value cannot be`null`. |
| `newIV`                      | [ByteArray](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-byte-array/index.html):This value cannot be`null`. |

### setPattern

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

```
fun setPattern(newPattern: MediaCodec.CryptoInfo.Pattern!): Unit
```

Set the encryption pattern on a[MediaCodec.CryptoInfo](https://developer.android.com/reference/kotlin/android/media/MediaCodec.CryptoInfo#)instance. See[Pattern](https://developer.android.com/reference/kotlin/android/media/MediaCodec.CryptoInfo.Pattern).  

### toString

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

```
fun toString(): String
```

|                                                          Return                                                          ||
|----------------------------------------------------------------------------------|----------------------------------------|
| [String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html) | a string representation of the object. |

## Properties

### iv

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

```
var iv: ByteArray!
```

A 16-byte initialization vector  

### key

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

```
var key: ByteArray!
```

A 16-byte key id  

### mode

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

```
var mode: Int
```

The type of encryption that has been applied, see[CRYPTO_MODE_UNENCRYPTED](https://developer.android.com/reference/kotlin/android/media/MediaCodec#CRYPTO_MODE_UNENCRYPTED:kotlin.Int),[CRYPTO_MODE_AES_CTR](https://developer.android.com/reference/kotlin/android/media/MediaCodec#CRYPTO_MODE_AES_CTR:kotlin.Int)and[CRYPTO_MODE_AES_CBC](https://developer.android.com/reference/kotlin/android/media/MediaCodec#CRYPTO_MODE_AES_CBC:kotlin.Int)  

### numBytesOfClearData

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

```
var numBytesOfClearData: IntArray!
```

The number of leading unencrypted bytes in each subSample. If null, all bytes are treated as encrypted and[numBytesOfEncryptedData](https://developer.android.com/reference/kotlin/android/media/MediaCodec.CryptoInfo#numBytesOfEncryptedData:kotlin.IntArray)must be specified.  

### numBytesOfEncryptedData

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

```
var numBytesOfEncryptedData: IntArray!
```

The number of trailing encrypted bytes in each subSample. If null, all bytes are treated as clear and[numBytesOfClearData](https://developer.android.com/reference/kotlin/android/media/MediaCodec.CryptoInfo#numBytesOfClearData:kotlin.IntArray)must be specified.  

### numSubSamples

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

```
var numSubSamples: Int
```

The number of subSamples that make up the buffer's contents.