Encryption API for Unity SDK

PubNub Unity 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 page.

Configuration

CryptoModule configuration

Use these methods to configure the CryptoModule to encrypt all messages and files:


The client can decrypt content from either module. You can read historical messages and messages from older clients, and you can encrypt new messages with 256-bit AES-CBC.

Older SDK versions

Apps built using the SDK versions lower than 7.0.1 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 you use encryption methods, ensure your PubNub client is configured with publish/subscribe keys and a user ID. See the Configuration guide for setup instructions.

Encryption methods

Encrypt

Use this function to encrypt data.

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)

To encrypt the data you can use the following method(s) in Unity SDK.

pubnub.Encrypt(inputString, cipherKey)
* required
ParameterDescription
inputString *
Type: String
The data to encrypt.
cipherKey
Type: String
Cipher key to use for encryption.

Sample code

Reference code
This example is a self-contained code snippet ready to be run. It includes necessary imports and executes methods with console logging. Use it as a reference when working with other examples in this document.
Encrypt part of a message

Encrypt file

Use this function to encrypt file content.

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)

To encrypt the file you can use the following method(s) in Unity SDK.

pubnub.EncryptFile(sourceFile, destinationFile, cipherKey)
* required
ParameterDescription
sourceFile *
Type: String
File to be encrypted.
destinationFile *
Type: String
Path of the encrypted file to be saved.
cipherKey
Type: String
Cipher Key to use for encryption. If provided, the legacy encryption with 128-bit cipher key entropy is used. If not provided, the CryptoModule from PubNub config will be used.

For more information, refer to Crypto module configuration.
byte[] outputBytes = pubnub.EncryptFile(sourceBytes) byte[] outputBytes = pubnub.EncryptFile(sourceBytes, cipherKey)
* required
ParameterDescription
sourceBytes *
Type: byte[]
byte array of the file.
cipherKey
Type: String
Cipher Key to use for encryption. If provided, the legacy encryption with 128-bit cipher key entropy is used. If not provided, the CryptoModule from PubNub config will be used.

For more information, refer to Crypto module configuration.

Sample code



Decryption methods

Decrypt

Use this function to decrypt data.

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)

To decrypt the data you can use the following method(s) in Unity SDK.

pubnub.Decrypt(inputString, cipherKey)
* required
ParameterDescription
inputString *
Type: String
The data to decrypt.
cipherKey
Type: String
Cipher key used for decryption.

Sample code

Decrypt part of a message

Decrypt file

Use this function to decrypt file content.

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)

To decrypt the file you can use the following method(s) in Unity SDK.

pubnub.DecryptFile(sourceFile, destinationFile, cipherKey);
* required
ParameterDescription
sourceFile *
Type: String
File to be decrypted.
destinationFile *
Type: String
Path of the decrypted file to be saved.
cipherKey
Type: String
Cipher Key to use for decryption. If provided, the legacy encryption with 128-bit cipher key entropy is used. If not provided, the CryptoModule from PubNub config will be used.

For more information, refer to Crypto module configuration.
byte[] outputBytes = pubnub.DecryptFile(sourceBytes) byte[] outputBytes = pubnub.DecryptFile(sourceBytes, cipherKey)
* required
ParameterDescription
sourceBytes *
Type: byte[]
byte array of the file.
cipherKey
Type: String
Cipher Key to use for decryption. If provided, the legacy encryption with 128-bit cipher key entropy is used. If not provided, the CryptoModule from PubNub config will be used.

For more information, refer to Crypto module configuration.

Sample code



Last updated on