Cipher Functions
cryptographer.js provides AES, ChaCha20, and legacy DES/3DES encryption and decryption with multiple modes of operation.
Overview
Cipher functions provide symmetric encryption and decryption. They're used for:
Data encryption at rest
Secure communication
File encryption
Database encryption
API payload encryption
Supported Algorithms
AES-128
128 bits
GCM, CCM, CTR, SIV, (CBC/ECB mapped)
✅ Recommended
General purpose
AES-192
192 bits
GCM, CCM, CTR, (CBC/ECB mapped)
✅ Recommended
Higher security
AES-256
256 bits
GCM, CCM, CTR, SIV, (CBC/ECB mapped)
✅ Recommended
Maximum security
ChaCha20
256 bits
CTR (nonce=12B), AEAD
✅ Recommended
Performance/portable
ChaCha20-Poly1305
256 bits
AEAD
✅ Recommended
Authenticated encryption
DES
56-bit
CBC, CTR
❌ Legacy (avoid)
Interop only
3DES (EDE3)
168-bit
CBC, CTR
⚠️ Legacy (avoid)
Interop only
Public-Key (Key Exchange & Asymmetric Encryption)
RSA-OAEP (SHA-256 default)
Asymmetric encryption (small payloads)
✅ Recommended
Use to encrypt keys, not large data
X25519
Key agreement (ECDH over Curve25519)
✅ Recommended
Modern, fast, safe defaults
ECDH secp256r1/P-384
Key agreement
✅ Recommended
Widely supported, choose curve per compliance
RSA-OAEP usage and limits
Key formats: Public (SPKI/PKCS#1 DER), Private (PKCS#8/PKCS#1 DER)
Hash options: 'sha1' | 'sha256' | 'sha384' | 'sha512' (default: 'sha256')
OAEP label: optional; if used, must match on decrypt
Max plaintext: For modulus size k (bytes) and hash hLen (bytes): max = k - 2*hLen - 2
Example: 2048-bit (k=256) with SHA-256 (hLen=32) → 190 bytes
X25519 usage
Generate keypair and derive shared secret; then HKDF → symmetric key
ECDH secp256r1/P-384 usage
Choose curve per compliance/perf; keys uncompressed: secp256r1 (pub 65B), P-384 (pub 97B)
Basic Usage
AES: Simple Encryption/Decryption (GCM)
Different Key Sizes
AES: Different Modes
ChaCha20 / ChaCha20-Poly1305
Notes:
AES-GCM nonce must be 12 bytes; AES-CCM nonce must be 13 bytes; AES-SIV nonce must be 16 bytes.
CBC/ECB/CTR are classic block/stream modes. Prefer AEAD (GCM/CCM/SIV, or ChaCha20-Poly1305) when possible.
DES / 3DES (Legacy)
Warning: DES/3DES are considered deprecated and should not be used for new systems.
Advanced Usage
File Encryption
Streaming Encryption
Secure Communication Protocol
Security Best Practices
Key Management
IV Management
Mode Selection
Authenticated Encryption
Performance
Sample performance (Linux x64 / Node 20; only overlapping modes with crypto-js):
AES-256
CBC
0.052 M
5.6× faster
General purpose
AES-256
CTR
0.072 M
8× faster
Parallel processing
Error Handling
TypeScript Support
API Reference
Function Signature
Types
Available Functions
crypto.cipher.aes.encrypt(data, options)/decryptcrypto.cipher.chacha20.encrypt(data, options)/decryptcrypto.cipher.des.encrypt(data, options)/decrypt
Key Sizes
AES-128: 16 bytes key
AES-192: 24 bytes key
AES-256: 32 bytes key
IV/Nonce Sizes
AES-GCM: 12-byte nonce (required)
AES-CCM: 13-byte nonce (required)
AES-SIV: 16-byte nonce (required). Key must be 32B (AES-128-SIV) or 64B (AES-256-SIV)
AES-CTR: 16-byte IV
AES-CBC: Use GCM instead; if specified, mapped to GCM internally (nonce 12B)
ChaCha20: 12-byte nonce (required)
ChaCha20-Poly1305: 12-byte nonce (required)
DES/3DES CBC/CTR: 8-byte IV/nonce
ECB: No IV (internally emulated using CTR with zero IV; avoid)
Block Size
AES block size: 16 bytes (applies to CTR/CBC). AEAD modes (GCM/CCM/SIV) are message-oriented with tags.
Last updated