Encryption API

Encryption API

The APIs in this section were added in Profound.js / Profound API version 7. They require a 32-byte encryption key, supplied either as an API parameter or through the encryptionKey configuration setting.

Overview

The Encryption API contains the Profound.js APIs used to encrypt and decrypt data in memory. Use these APIs to protect sensitive values – passwords, API tokens, account numbers, or personal data – before writing them to a database column, an IFS file, or an external service, and to read those values back afterward.

  • pjs.encrypt() - Encrypts a Node.js Buffer and returns the encrypted data as a new Buffer.

  • pjs.decrypt() - Decrypts a Buffer produced by pjs.encrypt() and returns the original data.

Both APIs use the AES-256-GCM algorithm – the same algorithm used by the built-in File Encryption features. Encryption is performed all at once on data already loaded into memory, so these APIs are best suited to small and moderate amounts of data rather than to streaming large objects.

Encryption Key

Both APIs accept an optional encryption key as their second parameter. The key must be a Buffer of exactly 32 bytes. When the parameter is omitted, the key from the encryptionKey configuration setting is used instead.

If the parameter is omitted and no encryptionKey setting is configured, the API throws an error containing this text:

This feature requires an encryption key.

Keys are generated with the gen_key.js command-line utility supplied in the product installation directory, which uses the Node.js crypto.randomBytes() API to produce cryptographically strong random bytes (see File Encryption for the full procedure).

Data can only be decrypted with the key it was encrypted with. Keep a backup of the key in a secure location – if the key is lost, any data encrypted with it is unrecoverable. If you rotate keys, decrypt existing data with the old key and re-encrypt it with the new one before retiring the old key.

Encrypted Data Format

pjs.encrypt() returns a single Buffer that carries everything pjs.decrypt() needs except the key. Store or transmit that Buffer as a unit; do not split, pad, or truncate it.

Position

Length

Contents

Position

Length

Contents

First

12 bytes

Initialization vector (IV). Randomly generated for every call. Not secret.

Middle

Same length as the original data

Ciphertext.

Last

16 bytes

GCM authentication tag, used to detect a wrong key or tampered data.

The encrypted Buffer is therefore always exactly 28 bytes longer than the data passed in.

Because a new random IV is generated on every call, encrypting the same value twice produces two different results. This is expected and is what keeps the encryption secure, but it also means an encrypted value cannot be used as a database key, compared to another encrypted value, or used to detect duplicates. To decrypt, always start from the exact Buffer that pjs.encrypt() returned.

Storing Encrypted Data

The encrypted Buffer contains raw binary, including null bytes. Write it to a binary column such as BLOB or VARBINARY, or to an IFS file opened in binary mode. To store it in a character column or send it in a JSON payload, convert it to Base64 first:

// Encrypt, then Base64-encode so the value is safe for a character column. const stored = pjs.encrypt(Buffer.from("4111111111111111")).toString("base64"); // Reverse the conversion before decrypting. const original = pjs.decrypt(Buffer.from(stored, "base64")).toString();

Base64 expands the value by roughly one third, so size the column accordingly: a n-byte value becomes a 4 × ⌈(n + 28) ÷ 3⌉ character string.


Related Pages