Node.js applications handle credentials, PII, API keys, and payment data daily. Choosing the wrong cryptographic approach can create serious, exploitable vulnerabilities quickly. This guide maps each data-protection need to the right technical method.
Match your use case to the right method
Algorithms like AES-GCM or ChaCha20-Poly1305 provide both confidentiality and integrity. But not every sensitive value needs symmetric encryption.
Use case | Recommended method |
|---|---|
Passwords | Argon2id, scrypt, or bcrypt (hashing only) |
Database fields / PII | AES-256-GCM (AEAD) |
File or blob storage | Envelope encryption via KMS |
Service-to-service traffic | TLS 1.3 or mutual TLS |
Sharing secrets between parties | Asymmetric public-key encryption |
Passwords should never be stored in plain text. They must be protected using strong, slow hashing algorithms such as Argon2id, bcrypt, or PBKDF2. Hashing is one-way, and encryption is not, so never conflate the two.
Encrypt sensitive fields with AES-256-GCM
AES-256-GCM provides both confidentiality (AES counter mode) and integrity (Galois/Counter Mode 128-bit authentication tag). Use Node's built-in crypto module with createCipheriv to implement it correctly.
Always use crypto.randomBytes() to generate keys and IVs. AES-GCM requires a 96-bit (12-byte) initialization vector. The critical rule: never reuse an IV with the same key.
Store the IV alongside the ciphertext, keep the auth tag with GCM payloads, and treat any key that cannot be rotated as a liability.
The old crypto.createCipher derived keys with MD5 and no salt; it was deprecated and removed in Node.js 22. Use createCipheriv exclusively.
Store keys in KMS, not environment variables
Environment variables are common, but they leak through logs, container snapshots, and crash dumps. Cryptographic keys, secrets, and API keys should never be committed to source code repositories or embedded in build artifacts.
Secrets and keys should be stored in a dedicated secrets-management solution or key vault.) Cloud KMS services from AWS, Google Cloud, and Azure support envelope encryption: encrypt each record with a unique data key, then protect that data key with a KMS master key.
Rotation processes should be in place before they are required, so keys can be quickly rotated in the event of a compromise.
Enforce TLS 1.3 for data in transit
Enforce TLS 1.3, standardized as RFC 8446, across all service-to-service communication. For internal microservices, mutual TLS authenticates both sides of a connection. Even with TLS active, encrypt sensitive payload fields at the application layer, since proxies, brokers, and logs can expose plaintext.
Avoid these anti-patterns
Avoid unauthenticated modes such as CBC for new code
Reusing or hardcoding initialization vectors in any cipher mode
Storing master keys in the same location as ciphertext
Relying only on environment variables for cryptographic secrets
Allowing algorithms outside AES-256-GCM or ChaCha20-Poly1305 for symmetric encryption
The OWASP Cryptographic Storage Cheat Sheet is the authoritative reference for aligning Node.js implementations with current security standards. Teams that need expert engineers to implement, audit, and operationalize these controls can accelerate hiring through Proxify, which connects companies with vetted senior developers experienced in security-critical Node.js systems.