Skip to content

@pqc-sdk/core / encrypt

Function: encrypt()

encrypt(data, publicKey): Promise<Uint8Array<ArrayBufferLike>>

Hybrid encryption: encapsulates a secret with the public key's KEM and encrypts the data with AES-256-GCM using that secret. The result is a single self-contained Uint8Array that only decrypt can open.

The key chooses the envelope (docs/serialization-format.md §2): an ml-kem-768 key (FIPS 203) produces a v1 envelope, an x-wing key (X25519 + ML-KEM-768 hybrid, draft-connolly-cfrg-xwing-kem-10) a v2 one.

Parameters

data

string | Uint8Array<ArrayBufferLike>

publicKey

PublicKey<KemAlgorithm>

Returns

Promise<Uint8Array<ArrayBufferLike>>

Example

ts
import { pqc } from '@pqc-sdk/core';

const pair = await pqc.keys.generate();
const ciphertext = await pqc.encrypt('sensitive data', pair.publicKey);

const hybrid = await pqc.keys.generate({ algorithm: 'x-wing' });
const v2Ciphertext = await pqc.encrypt('sensitive data', hybrid.publicKey);