Skip to main content
Encryption is the foundation of API security. Without it, passwords stored in databases are readable if the server is compromised, and data sent over networks can be intercepted. This page covers the types of encryption you use in Node.js backend development, with complete working code for every technique.

Key Concepts: Encryption Vocabulary

Hashing vs Encryption

These are often confused. Know the difference.
Passwords must be HASHED, not encrypted. If you encrypt passwords, the encryption key becomes the single point of failure. If hashed with bcrypt, even the server admin cannot read the original password.

Password Hashing with bcrypt

bcrypt is the industry-standard library for hashing passwords in Node.js.

How bcrypt Works

  1. Generates a random salt
  2. Combines salt + password
  3. Runs the combination through a slow hashing algorithm N times (controlled by saltRounds)
  4. Returns a single string containing the algorithm, salt, and hash

Hashing a Password

Comparing a Password

Complete Login Example

Always return the same error message for “user not found” and “wrong password”. Returning different messages reveals whether an email is registered, which attackers exploit for account enumeration.

Salt Rounds: What They Mean

Salt rounds control how many times bcrypt iterates. Each increment roughly doubles the time.

Symmetric Encryption: AES-256 with crypto

Node.js has a built-in crypto module. Use it for encrypting sensitive data at rest (credit card numbers, PII, tokens to store in DB).
Generate a key for your .env:

Asymmetric Encryption: Public/Private Keys

Used in HTTPS (TLS), JWT signing with RS256, and SSL certificates. The public key encrypts or verifies; the private key decrypts or signs.
In JWT with RS256 algorithm, the server signs tokens with the PRIVATE key and clients verify with the PUBLIC key. This allows clients to validate tokens without ever knowing the private key.

Generating Secure Random Tokens

Use crypto.randomBytes() for password reset tokens, email verification tokens, and API keys.

HTTPS/TLS: Encryption in Transit

HTTPS encrypts all data between client and server using TLS. Without HTTPS, passwords sent in login requests can be intercepted on public networks. For development (self-signed cert):
In production, use Certbot with Nginx (covered in LO4) rather than managing TLS in Node.js directly.

Key Terms

Common Mistakes

MD5 and SHA-1 are fast cryptographic hashes, not password hashing algorithms. Fast = bad for passwords. Always use bcrypt, Argon2, or scrypt for password storage.
If an attacker dumps your database and also finds the encryption key there, encryption provides no protection. Store keys in environment variables or a key management service.
AES requires a unique Initialization Vector for every encryption operation. Reusing IVs leaks information about the plaintext. Always generate a new IV with crypto.randomBytes(16).
Without HTTPS, credentials sent in login forms are plaintext on the network. Always configure SSL/TLS before going live.