Encryption API for Dart SDK
PubNub Dart SDK provides built-in message and file encryption to secure your real-time communications. This documentation covers crypto module configuration and utility methods for encrypting and decrypting files using both legacy 128-bit and enhanced 256-bit AES-CBC encryption.
For general SDK configuration and initialization, refer to the Configuration page.
Configuration
cryptoModule
configuration
To configure the cryptoModule
to encrypt all messages/files, you can use the following methods in the Dart SDK:
// 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);
show all 17 linesYour 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.
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.
SDK Initialization required
Before using encryption methods, ensure your PubNub client is properly configured with subscribe key and user ID. See the Configuration guide for setup instructions.
Encryption methods
Encrypt file
Encrypts file content in bytes format.
Deprecated
The cipherKey
parameter in this method is deprecated. We recommend that you configure a separate instance of the crypto module 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)
pubnub.files.encryptFile(
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. |
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
// 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
Decrypts file content in bytes format.
Deprecated
The cipherKey
parameter in this method is deprecated. We recommend that you configure a separate instance of the crypto module 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)
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. |
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
// 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.