---
source_url: https://www.pubnub.com/docs/sdks/dart/api-reference/encryption
title: Encryption API for Dart SDK
updated_at: 2026-06-26T11:05:35.443Z
sdk_name: PubNub Dart SDK
sdk_version: 7.1.0
---

> Documentation Index
> For a curated overview of PubNub documentation, see: https://www.pubnub.com/docs/llms.txt
> For the full list of all documentation pages, see: https://www.pubnub.com/docs/llms-full.txt


# Encryption API for Dart SDK

PubNub Dart SDK, use the latest version: 7.1.0

Install:

```bash
dart pub add pubnub@7.1.0
```

PubNub Dart SDK includes message and file encryption. This page shows how to set up the crypto module and how to encrypt and decrypt data. The SDK supports 128-bit Advanced Encryption Standard (AES) and 256-bit AES in Cipher Block Chaining (CBC) mode.

For general SDK configuration and initialization, refer to the [Configuration](https://www.pubnub.com/docs/sdks/dart/api-reference/configuration) page.

## Configuration

### cryptoModule configuration

To configure the `cryptoModule` to encrypt all messages/files, you can use the following methods in the Dart SDK:

```dart
// encrypts using 256-bit AES-CBC cipher (recommended)
// decrypts data encrypted with the legacy and the 256-bit AES-CBC ciphers
var cryptoModule = CryptoModule.aescbcCryptoModule(CipherKey.fromUtf8('enigma'));

// encrypts with 128-bit cipher key entropy (legacy)
// decrypts data encrypted with the legacy and the 256-bit AES-CBC ciphers
var cryptoModule = CryptoModule.legacyCryptoModule(CipherKey.fromUtf8('enigma'));

// partial encryption
// create cryptoModule
var cryptoModule = CryptoModule.aesCbcCryptoModule(CipherKey.fromUtf8('abcd'));

// encrypt normal message
var encrypted = cryptoModule.encrypt('Hello'.codeUnits);

// decrypt encrypted message
var plainBytes = cryptoModule.decrypt(encrypted);
```

Your client can decrypt content encrypted using either of the modules. This way, you can interact with historical messages or messages sent from older clients while encoding new messages using the more secure 256-bit AES-CBC cipher.

:::warning Older SDK versions
Apps built using the SDK versions lower than 4.2.4 will **not** be able to decrypt data encrypted using the 256-bit AES-CBC cipher. Make sure to update your clients or encrypt data using the legacy algorithm.
:::

:::note SDK initialization required
Before you use encryption methods, ensure your PubNub client is configured with a subscribe key and a user ID. See the [Configuration guide](https://www.pubnub.com/docs/sdks/dart/api-reference/configuration#initialization) for setup instructions.
:::

## Encryption methods

### Encrypt file

Encrypt file content in bytes format.

:::warning Deprecated
The `cipherKey` parameter in this method is deprecated. We recommend that you configure a separate instance of the [crypto module](https://www.pubnub.com/docs/sdks/dart/api-reference/configuration#cryptomodule) and use it for partial encryption. If you pass `cipherKey` as an argument, it overrides the crypto module configuration and the legacy encryption with 128-bit cipher key entropy is used.
:::

#### Method(s)

```dart
pubnub.files.encryptFile(
  List<int> bytes,
  {CipherKey? cipherKey,
  Keyset? keyset,
  String? using}
)
```

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| bytes | List<int> | Yes |  | File content in bytes. |
| cipherKey | cipherKey | Optional |  | A `cipherKey` to override `Keyset.cipherKey`. For more information, refer to [Configuration](https://www.pubnub.com/docs/sdks/dart/api-reference/configuration). |
| keyset | Keyset | Optional |  | Override for the PubNub default keyset configuration. |
| using | String | Optional |  | Keyset name from the `keysetStore` to be used for this method call. |

#### Sample code

```dart
// create cryptoModule
var cryptoModule = CryptoModule.aesCbcCryptoModule(CipherKey.fromUtf8('abcd'));

// encrypt file data   NOTE: same method because it works at byte level
var encryptedFileData = cryptoModule.encrypt(fileData);
```

#### Returns

Byte List of encrypted data.

## Decryption methods

### Decrypt file

Decrypt file content in bytes format.

:::warning Deprecated
The `cipherKey` parameter in this method is deprecated. We recommend that you configure a separate instance of the [crypto module](https://www.pubnub.com/docs/sdks/dart/api-reference/configuration#cryptomodule) and use it for partial encryption. If you pass `cipherKey` as an argument, it overrides the crypto module configuration and the legacy encryption with 128-bit cipher key entropy is used.
:::

#### Method(s)

```dart
pubnub.files.decryptFile(
  List<int> bytes,
  {CipherKey? cipherKey,
  Keyset? keyset,
  String? using})
```

| Parameter | Description |
| --- | --- |
| `bytes` *Type: List`<int>` | File content in bytes. |
| `cipherKey`Type: cipherKey | A `cipherKey` to override `Keyset.cipherKey`. For more information, refer to [Configuration](https://www.pubnub.com/docs/sdks/dart/api-reference/configuration). |
| `keyset`Type: `Keyset` | Override for the PubNub default keyset configuration. |
| `using`Type: `String` | Keyset name from the `keysetStore` to be used for this method call. |

#### Sample code

```dart
// create cryptoModule
var cryptoModule = CryptoModule.aesCbcCryptoModule(CipherKey.fromUtf8('abcd'));

// encrypt file data   NOTE: same method because it works at byte level
var encryptedFileData = cryptoModule.encrypt(fileData);

// decrypt file data 
var decryptedFileData = cryptoModule.decrypt(encryptedFileData);

// create file again from decrypted file bytes
File('decryptedFile.jpg').writeAsBytesSync(decryptedFileData);
```

#### Returns

Byte list of decrypted data.

## Terms in this document

* **Subscribe Key** - A unique identifier that allows your application to receive messages from PubNub channels. It's part of your app's credentials and should be kept secure.
* **User** - An individual or entity that interacts with a system, application, or service. In PubNub, a user typically refers to someone who sends or receives messages through the platform, identified by a unique user ID or username.
* **User ID** - UTF-8 encoded, unique string of up to 92 characters used to identify a single client (end user, device, or server) that connects to PubNub.