# Encryption The `Ably.Rest.Crypto``Ably::Util::Crypto``Ably\Utils\Crypto``ably.util.crypto``io.ably.lib.util.crypto``ARTCrypto``IO.Ably.Encryption.Crypto` object exposes the following public methods: ## Methods ### getDefaultParamsget_default_paramsget_default_paramsGetDefaultParamsDefaultCipherParams `Crypto.getDefaultParams(Object params): CipherParams` `CipherParams Crypto.get_default_params(Hash params)` `CipherParams Crypto.get_default_params(Dict params)` `CipherParams Crypto.getDefaultParams(Array params)` `CipherParams Crypto.getDefaultParams(Param[] params)` `CipherParams GetDefaultParams(byte[] key = null, byte[] iv = null, CipherMode? mode = null)` `getDefaultParams(values: [NSObject : AnyObject]) -> ARTCipherParams` `DefaultCipherParams() (*CipherParams, error)` This call obtains a [`CipherParams`](#cipher-params) object using the values passed in (which must be a subset of `CipherParams` fields that at a minimum includes a `key`), filling in any unspecified fields with default values, and checks that the result is a valid and self-consistent.This call takes a key, an initialization vector (iv) and a Cipher mode. There is also on override which accepts the `key` and `iv` as base64 encoded strings. It will validate the passed values and generate `CipherParams`returns a [`CipherParams`](#cipher-params) object with fields set to default values. This generates random secret key and initialization vector (iv) values.

You will rarely need to call this yourself, since the client library will handle it for you if you specify `cipher` params when initializing a channel (as in the example [at the top](https://ably.com/docs/channels/options/encryption.md?source=llms.txt)) or when setting channel options with `channel#setOptions`. #### Parameters | Parameter | Description | Type | |-----------|-------------|------| | paramsarguments | The cipher paramsarguments that you want to specify. It must at a minimum include a `key`, which should be either a binary (`byte[]``ArrayBuffer` or `Uint8Array``Buffer`byte array`NSData`) or a base64-encoded `NS``String`. | Object | #### Returns On success, the method returns a complete [`CipherParams`](#cipher-params) object. Failure will raise an [`AblyException`](https://ably.com/docs/api/rest-sdk/types.md?source=llms.txt#ably-exception)exception. #### Example ##### Javascript ``` const cipherParams = Ably.Rest.Crypto.getDefaultParams({ key: }); const channelOpts = { cipher: cipherParams }; const channel = rest.channels.get('your-channel-name', channelOpts); ``` ##### Nodejs ``` const cipherParams = Ably.Rest.Crypto.getDefaultParams({ key: }); const channelOpts = { cipher: cipherParams }; const channel = rest.channels.get('your-channel-name', channelOpts); ``` ##### Ruby ``` cipher_params = Ably::Util::Crypto.get_default_params({key: }) channel_opts = { cipher: cipher_params } channel = rest.channels.get('your-channel-name', channel_opts) ``` ##### Python ``` cipher_params = ably.util.crypto.get_default_params({'key': }) channel = rest.channels.get('your-channel-name', cipher=cipher_params) ``` ##### Php ``` $cipherParams = Ably\Utils\Crypto->getDefaultParams(array('key' => )); $channelOpts = array('cipher' => $cipherParams); $channel = $rest->channels->get('your-channel-name', $channelOpts); ``` ##### Java ``` CipherParams params = Crypto.getDefaultParams(new Param[]{ new Param("key", ) }); ChannelOptions options = new ChannelOptions(); options.encrypted = true; options.cipherParams = params; Channel channel = rest.channels.get("your-channel-name", options); ``` ##### Csharp ``` CipherParams cipherParams = Crypto.GetDefaultParams(); var channel = rest.Channels.Get("your-channel-name", new ChannelOptions(cipherParams)); ``` ##### Objc ``` ARTCipherParams *params = [ARTCrypto getDefaultParams:@{@"key": }]; ARTChannelOptions *options = [[ARTChannelOptions alloc] initWithCipher:params]; ARTRealtimeChannel *channel = [rest.channels get:@"your-channel-name" options:options]; ``` ##### Swift ``` let params = ARTCrypto.getDefaultParams(["key": ]) let options = ARTChannelOptions(cipher: params) let channel = rest.channels.get("your-channel-name", options: options) ``` ##### Go ``` params, err := Crypto.DefaultCipherParams() ``` ### generateRandomKeygenerate_random_keygenerate_random_keyGenerateRandomKey `Crypto.generateRandomKey(Number keyLength?): Promise` `Crypto.generateRandomKey(Number keyLength?): Promise` `byte array Crypto.generate_random_key(Int key_length?)` `byte array Crypto.generate_random_key(Int key_length?)` `string Crypto.generateRandomKey(Int keyLength?)` `byte[] Crypto.generateRandomKey(Int keyLength?)` `byte[] GenerateRandomKey(int? keyLength = null, CipherMode? mode = null)` `generateRandomKey(length?: UInt) -> NSData` `GenerateRandomKey(keyLength ...int) ([]byte, error)` This call obtains a randomly-generated binary key of the specified key length and optional CipherMode. #### Parameters | Parameter | Description | Type | |-----------|-------------|------| | keyLengthkey_length | Optional with the length of key to generate. For AES, this should be either 128 or 256. If unspecified, defaults to 256. | numberInt | | Parameter | Description | Type | |-----------|-------------|------| | keyLength | Optional with the length of key to generate. For AES, this should be either 128 or 256. If unspecified, defaults to 256. | int? | | mode | Optional AES which is used when the key is generated | `CipherMode` | #### Returns Returns a promise. On success, the promise is fulfilled with the generated key as an `ArrayBuffer`a `Buffer`. On failure, the promise is rejected with an [`ErrorInfo`](https://ably.com/docs/api/rest-sdk/types.md?source=llms.txt#error-info) object that details the reason why it was rejected. #### Returns On success, the method returns the generated key as a `byte[]` array`bytes`byte arraybinary string`NSData``[]byte` array. Failure will raise an [`AblyException`](https://ably.com/docs/api/rest-sdk/types.md?source=llms.txt#ably-exception)Failure will cause error to contain an [`ErrorInfo`](#error-info) object describing the failure reason. #### Example ##### Javascript ``` const key = await Ably.Rest.Crypto.generateRandomKey(256); const channel = rest.channels.get('your-channel-name', { cipher: { key: key } }); ``` ##### Ruby ``` key = Ably::Util::Crypto.generate_random_key(256) channel = rest.channels.get('your-channel-name', { cipher: {key: key}}) ``` ##### Python ``` cipher_params = ably.util.crypto.generate_random_key(256) channel = rest.channels.get('your-channel-name', cipher={'key': key}) ``` ##### Php ``` $key = Ably\Utils\Crypto->generateRandomKey(256); $channel = $rest->channels->get('your-channel-name', array('cipher' => array('key' => $key))); ``` ##### Java ``` byte[] key = Crypto.generateRandomKey(256); ChannelOptions options = ChannelOptions.withCipher(key); Channel channel = rest.channels.get("your-channel-name", options); ``` ##### Csharp ``` byte[] key = Crypto.GenerateRandomKey(256); ChannelOptions options = new ChannelOptions(key); var channel = rest.Channels.Get("your-channel-name", options); ``` ##### Objectivec ``` NSData *key = [ARTCrypto generateRandomKey:256]; ARTChannelOptions *options = [[ARTChannelOptions alloc] initWithCipherKey:key]; ARTRealtimeChannel *channel = [rest.channels get:@"your-channel-name" options:options]; ``` ##### Swift ``` let key = ARTCrypto.generateRandomKey(256) let options = ARTChannelOptions(cipherWithKey: key) let channel = rest.channels.get("your-channel-name", options: options) ``` ##### Go ``` key, err := Crypto.GenerateRandonKey(256) ``` ## Related types ### ChannelOptions ObjectARTChannelOptionsChannelOptions HashChannelOptions DictChannelOptions ArrayIO.Ably.ChannelOptionsio.ably.lib.types.ChannelOptions Channel options are used for setting [channel parameters](https://ably.com/docs/channels/options.md?source=llms.txt) and [configuring encryption](https://ably.com/docs/channels/options/encryption.md?source=llms.txt). `ChannelOptions`, a plain JavaScript object, may optionally be specified when instancing a [`Channel`](https://ably.com/docs/channels.md?source=llms.txt), and this may be used to specify channel-specific options. The following attributes can be defined on the object: `ChannelOptions`, a Hash object, may optionally be specified when instancing a [`Channel`](https://ably.com/docs/channels.md?source=llms.txt), and this may be used to specify channel-specific options. The following key symbol values can be added to the Hash: `ChannelOptions`, an Associative Array, may optionally be specified when instancing a [`Channel`](https://ably.com/docs/channels.md?source=llms.txt), and this may be used to specify channel-specific options. The following named keys and values can be added to the Associated Array: `ART``io.ably.lib.types.``ChannelOptions` may optionally be specified when instancing a [`Channel`](https://ably.com/docs/channels.md?source=llms.txt), and this may be used to specify channel-specific options. `IO.Ably.ChannelOptions` may optionally be specified when instancing a [`Channel`](https://ably.com/docs/channels.md?source=llms.txt), and this may be used to specify channel-specific options. #### PropertiesMembers | Property | Description | Type | |----------|-------------|------| | :cipherCipherParamscipher | Requests encryption for this channel when not null, and specifies encryption-related parameters (such as algorithm, chaining mode, key length and key). See [an example](https://ably.com/docs/channels/options/encryption.md?source=llms.txt) | [`CipherParams`](https://ably.com/docs/api/realtime-sdk/encryption.md?source=llms.txt#cipher-params) or an options hash containing at a minimum a `key` or an Associative Array containing at a minimum a `key` | | Property | Description | Type | |----------|-------------|------| | paramsParams | Optional [parameters](https://ably.com/docs/channels/options.md?source=llms.txt) which specify behaviour of the channel. | `Map``JSON Object` | | cipherCipherParams | Requests encryption for this channel when not null, and specifies encryption-related parameters (such as algorithm, chaining mode, key length and key). See [an example](https://ably.com/docs/api/realtime-sdk/encryption.md?source=llms.txt#getting-started) | [`CipherParams`](https://ably.com/docs/api/realtime-sdk/encryption.md?source=llms.txt#cipher-params) or an options object containing at a minimum a `key` or a [`Param[]`](#param) list containing at a minimum a `key` | #### Static methods ##### withCipherKey ```java static ChannelOptions.withCipherKey(Byte[] or String key) ``` A helper method to generate a `ChannelOptions` for the simple case where you only specify a key. #### Parameters | Parameter | Description | Type | |-----------|-------------|------| | key | A binary `Byte[]` array or a base64-encoded `String`. | `Byte[]` or `String` | #### Returns On success, the method returns a complete `ChannelOptions` object. Failure will raise an [`AblyException`](https://ably.com/docs/api/realtime-sdk/types.md?source=llms.txt#ably-exception). ### CipherParamsARTCipherParamsCipherParams HashCipherParams DictCipherParams ArrayIO.Ably.CipherParamsio.ably.lib.util.Crypto.CipherParams A `CipherParams` contains configuration options for a channel cipher, including algorithm, mode, key length and key. Ably client libraries currently support AES with CBC, PKCS#7 with a default key length of 256 bits. All implementations also support AES128. Individual client libraries may support either instancing a `CipherParams` directly, using [`Crypto.getDefaultParams()`](https://ably.com/docs/api/realtime-sdk/encryption.md?source=llms.txt#get-default-params)[`Crypto.GetDefaultParams()`](https://ably.com/docs/api/realtime-sdk/encryption.md?source=llms.txt#get-default-params)[`Crypto.get_default_params()`](https://ably.com/docs/api/realtime-sdk/encryption.md?source=llms.txt#get-default-params), or generating one automatically when initializing a channel, as in [this example](https://ably.com/docs/channels/options/encryption.md?source=llms.txt). #### PropertiesMembers | Property | Description | Type | |----------|-------------|------| | keyKey:key | A binary (`byte[]``ArrayBuffer` or `Uint8Array``Buffer`byte array`NSData`) or base64-encoded `NS``String` containing the secret key used for encryption and decryption | | | algorithm:algorithmAlgorithm | The name of the algorithm in the default system provider, or the lower-cased version of it; eg "aes" or "AES"
default: _AES_ | `String` | | key_length:key_lengthkeyLengthKeyLength | The key length in bits of the cipher, either 128 or 256
default: _256_ | `Integer` | | modeMode | The cipher mode
default: _CBC_ | `String``CipherMode` |
| Property | Description | Type | |----------|-------------|------| | key | A binary (`byte[]`) or base64-encoded `String` containing the secret key used for encryption and decryption | | | algorithm | The name of the algorithm in the default system provider, or the lower-cased version of it; eg "aes" or "AES"
default: _AES_ | `String` | | keyLength | The key length in bits of the cipher, either 128 or 256
default: _256_ | `Integer` | | mode | The cipher mode
default: _CBC_ | `String` | | keySpec | A `KeySpec` for the cipher key | `SecretKeySpec` |
## Related Topics - [Constructor](https://ably.com/docs/api/rest-sdk.md?source=llms.txt): Client Library SDK REST API Reference constructor documentation. - [Channels](https://ably.com/docs/api/rest-sdk/channels.md?source=llms.txt): Client Library SDK REST API Reference Channels documentation. - [Channel Status](https://ably.com/docs/api/rest-sdk/channel-status.md?source=llms.txt): Client Library SDK REST API Reference Channel Status documentation. - [Messages](https://ably.com/docs/api/rest-sdk/messages.md?source=llms.txt): Client Library SDK REST API Reference Message documentation. - [Presence](https://ably.com/docs/api/rest-sdk/presence.md?source=llms.txt): Presence events provide clients with information about the status of other clients 'present' on a channel - [Authentication](https://ably.com/docs/api/rest-sdk/authentication.md?source=llms.txt): Client Library SDK REST API Reference Authentication documentation. - [History](https://ably.com/docs/api/rest-sdk/history.md?source=llms.txt): Client Library SDK REST API Reference History documentation. - [Push Notifications - Admin](https://ably.com/docs/api/rest-sdk/push-admin.md?source=llms.txt): Client Library SDK REST API Reference Push documentation. - [Statistics](https://ably.com/docs/api/rest-sdk/statistics.md?source=llms.txt): Client Library SDK REST API Reference Statistics documentation. - [Types](https://ably.com/docs/api/rest-sdk/types.md?source=llms.txt): Client Library SDK REST API Reference Types documentation. ## Documentation Index To discover additional Ably documentation: 1. Fetch [llms.txt](https://ably.com/llms.txt?source=llms.txt) for the canonical list of available pages. 2. Identify relevant URLs from that index. 3. Fetch target pages as needed. Avoid using assumed or outdated documentation paths.