Skip to main content

SEA.encrypt()

Encrypt data using AES-GCM symmetric encryption. The data can be decrypted by anyone with the same encryption key.

Syntax

Parameters

  • data (any): Data to encrypt (strings, objects, arrays, numbers)
  • key (string|object): Encryption key or key pair with epriv property
  • callback (function, optional): Called with encrypted result
  • options (object, optional):
    • encode: Encoding for output (default: ‘base64’)
    • name: Encryption algorithm (default: ‘AES-GCM’)
    • raw: Return raw object instead of SEA-prefixed string (default: false)

Returns

A Promise that resolves to an encrypted string:
Or if raw: true, returns an object:

Basic Usage

Encrypt with Password

Encrypt with Key Pair

With Callback

How It Works

Reference: ~/workspace/source/sea/encrypt.js:9-38
  1. Key Derivation: Derives AES-GCM key from input key + random salt
  2. Random Values: Generates random salt (9 bytes) and IV (15 bytes)
  3. Stringify: Converts data to JSON string if not already a string
  4. Encryption: Encrypts using AES-GCM-256
  5. Output: Returns base64-encoded ciphertext, IV, and salt

Encryption Process

Encrypted Data Structure

Default Format (String)

Raw Format (Object)

With raw: true option:

Field Descriptions

  • ct: Encrypted ciphertext (AES-GCM output)
  • iv: Initialization vector (random, unique per encryption)
  • s: Salt (random, used for key derivation)

Encryption Key Types

Using a Password

Using Key Pair’s epriv

Using epriv Directly

Using Shared Secret

Data Types

Strings

Objects

Arrays

Numbers

Undefined values are not allowed. Attempting to encrypt undefined will throw an error: “undefined not allowed.”

Options

Raw Output

Return object instead of string:

Custom Encoding

Custom Algorithm

Use Cases

Private User Data

End-to-End Encrypted Messages

Encrypted Backups

Security Considerations

AES-GCM

SEA uses AES-GCM (Galois/Counter Mode):
  • Key Size: 256 bits
  • IV Size: 15 bytes (120 bits)
  • Authentication: Built-in authentication tag
  • Security: Authenticated encryption (confidentiality + integrity)

Key Derivation

Keys are derived using SHA-256 hash:
Reference: ~/workspace/source/sea/aeskey.js (imported in encrypt.js)

Random Values

  • Salt: 9 bytes of cryptographically secure random data
  • IV: 15 bytes of cryptographically secure random data
  • Source: crypto.getRandomValues() (browser) or crypto.randomBytes() (Node.js)

IV Uniqueness

Never reuse an IV with the same key. SEA generates a fresh random IV for every encryption operation, ensuring uniqueness.

Password Strength

When using passwords as keys:
  • Use strong, unique passwords
  • Consider using SEA.work() for password-based encryption (adds PBKDF2)
  • Minimum 12+ characters recommended
  • Mix uppercase, lowercase, numbers, symbols

Error Handling

With Callbacks

Performance

  • Speed: Very fast (hardware-accelerated AES-GCM)
  • Size Overhead: ~30-40 bytes (IV + salt + JSON structure)
  • Async: Always asynchronous (uses Web Crypto API)

Common Patterns

Encrypt Before Storing

Conditional Encryption

Batch Encryption

Integration with GUN