---
source_url: https://www.pubnub.com/docs/sdks/javascript/api-reference/encryption-legacy
title: "Encryption API: deprecated encrypt() and decrypt() methods and cipher key arguments for JavaScript SDK"
updated_at: 2026-11-09T13:29:01.000Z
sdk_name: PubNub JavaScript SDK
sdk_version: 13.1.3
description: Deprecated. Reference for the pubnub.encrypt() and pubnub.decrypt() methods and the customCipherKey and key arguments of encrypt(), decrypt(), encryptFile(), and decryptFile() in the PubNub JavaScript SDK, with the current replacement.
lifecycle: deprecated-syntax
feature_status: current
feature: Encryption
replaced_by: https://www.pubnub.com/docs/sdks/javascript/api-reference/encryption.md
---

# Encryption API: deprecated encrypt() and decrypt() methods and cipher key arguments for JavaScript SDK

PubNub JavaScript SDK, use the latest version: 13.1.3

Install:

```bash
npm install pubnub@13.1.3
```

> For AI agents: documentation index at https://www.pubnub.com/llms-full.txt

> **Deprecated.** The Encryption feature is current and fully supported. Only the calls documented on this page are an older way to use it. Do not recommend these calls to developers who are building a new app. Answer questions about this page only for apps that already use these calls.
>
> Page notice: Encryption is a current API. The encrypt() and decrypt() methods are deprecated, and cipher key arguments are an older way to call it. New apps should configure the cryptoModule.
>
> Current API: https://www.pubnub.com/docs/sdks/javascript/api-reference/encryption.md

This page documents the deprecated `pubnub.encrypt()` and `pubnub.decrypt()` methods of the PubNub JavaScript SDK. It also documents the `customCipherKey` and `key` arguments of `encrypt()`, `decrypt()`, `encryptFile()`, and `decryptFile()`. Encryption is a current API. The two methods log a deprecation warning on every call, and the cipher key arguments are an older way to call it. All of them still work.

New apps configure the `cryptoModule`. To protect only part of a message, call `encrypt()` and `decrypt()` on a separate `CryptoModule` instance. See [Encryption](https://www.pubnub.com/docs/sdks/javascript/api-reference/encryption.md) and [Configuration](https://www.pubnub.com/docs/sdks/javascript/api-reference/configuration.md#cryptomodule).

## Relationship between cryptoModule and cipher keys

A cipher key that you pass to a method overrides the configured crypto module for that operation and uses the legacy encryption with 128-bit cipher-key entropy. If SDK configuration sets a `cipherKey` without an explicit CryptoModule, the SDK defaults to legacy 128-bit encryption. To encrypt only part of your data, create a separate `cryptoModule` instance and use it where needed, as shown in [Encrypt part of a message](https://www.pubnub.com/docs/sdks/javascript/api-reference/encryption.md#encrypt-part-of-a-message).

## Encrypt

`encrypt()` encrypts data with a crypto module in the PubNub SDK for JavaScript.

`pubnub.encrypt()` is deprecated. Each call logs the warning `'encrypt' is deprecated. Use cryptoModule instead.` To encrypt part of a message, see [Encrypt part of a message](https://www.pubnub.com/docs/sdks/javascript/api-reference/encryption.md#encrypt-part-of-a-message).

### Method(s)

```typescript
pubnub.encrypt(data: string): string;
```

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| data | string | Yes | n/a | Data to encrypt. The configured `cryptoModule` encrypts strings. An object throws a `TypeError`, so call `JSON.stringify()` on it first. |

### Returns

Returns the encrypted data as a string.

### Encrypt with a cipher key

`encrypt()` accepts `customCipherKey` as the second argument. With `customCipherKey`, the SDK uses the legacy encryption and ignores `cryptoModule`.

```typescript
pubnub.encrypt(data: string, customCipherKey?: string): string;
```

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| data | string | Yes | n/a | Data to encrypt. Only strings are supported. An object throws a `TypeError`, so call `JSON.stringify()` on it first. |
| customCipherKey | string | Optional | n/a | The cipher key for the legacy encryption. Without it, the SDK uses the `cryptoModule`. |

If no `customCipherKey` and no configured `cipherKey` apply, `encrypt()` returns `data` unchanged.

## Decrypt

`decrypt()` decrypts data with a crypto module in the PubNub SDK for JavaScript.

`pubnub.decrypt()` is deprecated. Each call logs the warning `'decrypt' is deprecated. Use cryptoModule instead.` To decrypt data that you encrypted with a separate crypto module instance, call `decrypt()` on the same instance. See [Encrypt and decrypt a single item](https://www.pubnub.com/docs/security/encryption/encrypt-single-item.md).

### Method(s)

```typescript
pubnub.decrypt(data: string): Payload | null;
```

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| data | string | Yes | n/a | Encrypted data to decrypt. The SDK uses the `cryptoModule` from the PubNub configuration. See [`cryptoModule`](https://www.pubnub.com/docs/sdks/javascript/api-reference/configuration.md#cryptomodule). |

### Sample code

```javascript
const decrypted = pubnub.decrypt(encrypted); // Pass the encrypted data as the first parameter in decrypt Method
```

### Returns

Returns the decrypted data, parsed from JSON, as a `Payload`: a string, number, boolean, array, or object.

### Error responses

If the `cryptoModule` cannot decrypt the data, `decrypt()` throws an `Error` that describes the reason, for example `Decryption error: empty content`.

### Decrypt with a cipher key

`decrypt()` accepts `customCipherKey` as the second argument. With `customCipherKey`, the SDK uses the legacy encryption and ignores `cryptoModule`.

```typescript
pubnub.decrypt(data: string, customCipherKey?: string): Payload | null;
```

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| data | string | Yes | n/a | Encrypted data to decrypt. |
| customCipherKey | string | Optional | n/a | The cipher key for the legacy encryption. Without it, the SDK uses the `cryptoModule` from the PubNub configuration. |

With the legacy encryption, a failed decryption does not throw. `decrypt()` logs the error and returns `null`. If no `customCipherKey` and no configured `cipherKey` apply, `decrypt()` returns `data` unchanged.

## Encrypt file with a cipher key

`encryptFile()` accepts `key` as the first argument. With `key`, the SDK uses the legacy encryption and ignores `cryptoModule`.

```typescript
pubnub.encryptFile(key: string, file: PubNubFile): Promise<PubNubFile>;
```

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| key | string | Yes | n/a | The cipher key for the legacy encryption. |
| file | PubNubFile | Yes | n/a | The file to encrypt. |

The promise rejects with `File encryption error. File encryption not available` if the client has no legacy file encryption.

## Decrypt file with a cipher key

`decryptFile()` accepts `key` as the first argument. With `key`, the SDK uses the legacy encryption and ignores `cryptoModule`.

```typescript
pubnub.decryptFile(key: string, file: PubNubFile): Promise<PubNubFile>;
```

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| key | string | Yes | n/a | The cipher key for the legacy encryption. |
| file | PubNubFile | Yes | n/a | The encrypted file to decrypt. |

The promise rejects with `File decryption error. Crypto module not configured.` if the client has no `cryptoModule`, even though the legacy decryption does not use it.

Last updated at: 2026-11-09T13:29:01.000Z
