Encryption API for JavaScript SDK

The encryption API of the PubNub SDK for JavaScript configures how a PubNub client encrypts messages and files, and provides methods that encrypt and decrypt data.

Before you call the encryption methods, create a PubNub client with a subscribeKey and a userId. See Initialization for the setup. For all client settings, see Configuration.

Configuration​

cryptoModule sets the algorithm that a PubNub client for JavaScript uses to encrypt and decrypt messages and files.

PubNub SDKs include two crypto modules: a legacy module with 128-bit cipher key entropy, and the recommended AES-CBC module. The AES-CBC module uses 256-bit Advanced Encryption Standard (AES) encryption in cipher block chaining (CBC) mode. See cryptoModule in Configuration for the parameter details.

cryptoModule configuration​

The legacy encryption keeps working without configuration changes. To use 256-bit Advanced Encryption Standard cipher block chaining (AES-CBC) encryption, set the crypto module to the AES-CBC module. The sample sets cryptoModule for all messages and files:

Both built-in crypto modules of the PubNub SDK for JavaScript decrypt content that either module encrypted.With the AES-CBC module, a client keeps reading historical messages and messages from older clients, and encrypts new messages with 256-bit AES-CBC.

In Node.js and browsers, the PubNub.CryptoModule.aesCbcCryptoModule() and PubNub.CryptoModule.legacyCryptoModule() helpers also decrypt AES-GCM (ACGM) data that other PubNub SDKs write. React Native can't decrypt AES-GCM data. The SDK never encrypts with AES-GCM. New data from the AES-CBC module stays AES-CBC. See Decrypt AES-GCM data.

Older SDK versions

Apps built with versions of the PubNub SDK for JavaScript before 7.4.0 cannot decrypt data that 256-bit AES-CBC encrypted. Update your clients or encrypt data using the legacy module.

cryptoModule configuration

1// encrypts using 256-bit AES-CBC cipher (recommended)
2// decrypts data encrypted with the legacy and the 256 bit AES-CBC ciphers
3const pubnub = new PubNub({
4 subscribeKey: 'YOUR_SUBSCRIBE_KEY',
5 userId: 'YOUR_USER_ID',
6 cryptoModule: PubNub.CryptoModule.aesCbcCryptoModule({ cipherKey: 'pubnubenigma' }),
7});
8
9// encrypts with 128-bit cipher key entropy (legacy)
10// decrypts data encrypted with the legacy and the 256-bit AES-CBC ciphers
11const pubnubLegacy = new PubNub({
12 subscribeKey: 'YOUR_SUBSCRIBE_KEY',
13 userId: 'YOUR_USER_ID',
14 cryptoModule: PubNub.CryptoModule.legacyCryptoModule({ cipherKey: 'pubnubenigma' }),
15});

Encrypt part of a message​

encrypt() on a separate CryptoModule instance in the PubNub SDK for JavaScript encrypts only the data that you pass to it.

To encrypt only part of a message, create a separate crypto module instance and use it only for that part. Both encrypt() and decrypt() return a promise, so await each result. To read the data back, call decrypt() on the same instance. The promise resolves to an ArrayBuffer with the bytes of the string you encrypted, so decode it with TextDecoder. See Encrypt and decrypt a single item.

Example

1import PubNub from 'pubnub';
2// Initialize PubNub with your keys
3const pubnub = new PubNub({
4 publishKey: 'YOUR_PUBLISH_KEY',
5 subscribeKey: 'YOUR_SUBSCRIBE_KEY',
6 userId: 'YOUR_USER_ID',
7});
1// Create a crypto module instance with AES-CBC encryption
2const cryptoModule = PubNub.CryptoModule.aesCbcCryptoModule({
3 cipherKey: 'pubnubenigma',
4});
5
6const msgContent = 'This is the data I wish to encrypt.';
7console.log(`Original Message: ${msgContent}`);
8
9// Encrypt the message
10const encryptedMessage = await cryptoModule.encrypt(JSON.stringify(msgContent));
11console.log('message encrypted successfully');
12
13// Decrypt with the same instance. decrypt() returns a Promise that resolves to an ArrayBuffer with the bytes of the JSON string
14const decryptedBytes = (await cryptoModule.decrypt(encryptedMessage)) as ArrayBuffer;
15const decryptedMessage = JSON.parse(new TextDecoder().decode(decryptedBytes));
show all 16 lines

Encrypt file​

encryptFile() encrypts file content with a crypto module in the PubNub SDK for JavaScript.

required
file required#
Type: PubNubFile
Default: no default
The file to encrypt. Create it with pubnub.File.create().

Returns​

Returns a promise that resolves to PubNubFile.

Error responses​

The promise rejects with an Error in these cases:

MessageCause
File encryption error. Source file is missing.file is not set.
File encryption error. File constructor not configured.The client has no PubNubFile constructor.
File encryption error. Crypto module not configured.The client has no cryptoModule.

Syntax

1pubnub.encryptFile(file: PubNubFile): Promise<PubNubFile>;

Decrypt file​

decryptFile() decrypts file content with a crypto module in the PubNub SDK for JavaScript.

required
file required#
Type: PubNubFile
Default: no default
The encrypted file to decrypt. Create it with pubnub.File.create().

Returns​

Returns a promise that resolves to PubNubFile. If the client has no cryptoModule, the promise resolves to undefined.

Error responses​

The promise rejects with an Error in these cases:

MessageCause
File encryption error. Source file is missing.file is not set.
File decryption error. File constructor not configured.The client has no PubNubFile constructor.

Syntax

1pubnub.decryptFile(file: PubNubFile): Promise<PubNubFile>;

Decrypt AES-GCM data​

PubNub.CryptoModule.aesGcmCryptor() creates a cryptor that decrypts AES-GCM (ACGM) data. Add it to the cryptors list of a custom crypto module that must read AES-GCM data. The aesCbcCryptoModule() and legacyCryptoModule() helpers, and modules created with cipherKey, already include it. The method is available in Node.js and browsers only.

The cryptor can't encrypt. Encryption with it fails with Encryption error: AES-GCM cryptor (ACGM) supports decryption only. It can't be the default cryptor. Setting it as default fails with Crypto module error: AES-GCM cryptor (ACGM) can't be used as default cryptor.

required
config.cipherKey required#
Type: string
Default: no default
The cipher key that encrypted the AES-GCM data. Without it, the method throws Crypto module error: cipher key not set.

Returns​

The method returns an ICryptor. Pass it in the cryptors list of the crypto module.

Syntax

1PubNub.CryptoModule.aesGcmCryptor(config: CryptorConfiguration): ICryptor;

Deprecated encrypt and decrypt methods​

pubnub.encrypt() and pubnub.decrypt() are deprecated methods of the PubNub JavaScript SDK that log a deprecation warning on every call, and a crypto module replaces them.

Deprecated JavaScript cipher key arguments still work, and each one has a current replacement. Apps that still call pubnub.encrypt() or pubnub.decrypt() find the reference in Encryption API: deprecated encrypt() and decrypt() methods and cipher key arguments for JavaScript SDK. That page also covers the cipher key arguments of encrypt(), decrypt(), encryptFile(), and decryptFile(). New apps set a cryptoModule so the client encrypts and decrypts messages and files. To protect only some data, follow Encrypt part of a message.

Was this page useful?

Last updated on