Crypto-js for JavaScript (Node.js): Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Crypto-js for JavaScript (Node.js): Complete Guide with Examples

\n\n

In modern web development, data security is a critically important aspect. Whether it's storing user passwords, transmitting confidential information over a network, or protecting files from unauthorized access — cryptography is at the core of these processes. The Crypto-js library provides a powerful and convenient set of cryptographic algorithms for the JavaScript ecosystem, working both in the browser and in the Node.js environment.

\n\n

This library implements a wide range of standard cryptographic functions: symmetric encryption (AES, DES, TripleDES), hashing (MD5, SHA-1, SHA-256, SHA-512), HMAC computation, as well as random number generation and encoding support (Base64, Hex, UTF-8). Crypto-js does not depend on native operating system modules, making it an ideal choice for isomorphic applications (code running both on the client and server).

\n\n

In this article, we will thoroughly cover the library installation, main methods with code examples, and also look at a real practical example of using Crypto-js for data protection in a web application. The material will be useful for both beginner developers and experienced professionals looking to systematize their knowledge of cryptography in JavaScript.

\n\n

Installation

\n\n

To install Crypto-js in a Node.js project, run the following command in the terminal:

\n\n
npm install crypto-js
\n\n

After installation, you can import the library into your code:

\n\n
// For Node.js (CommonJS)\nconst CryptoJS = require("crypto-js");\n\n// For ES Modules (if "type": "module" is used in package.json)\nimport CryptoJS from "crypto-js";
\n\n

Quick Start: Minimal Working Example

\n\n

Let's start with a simple example — encrypting and decrypting a string using the AES algorithm:

\n\n
const CryptoJS = require("crypto-js");\n\n// Original message\nconst message = "Hello, Crypto-js!";\n\n// Secret key (should be strong in a real application)\nconst secretKey = "my-secret-key-123";\n\n// Encryption\nconst ciphertext = CryptoJS.AES.encrypt(message, secretKey).toString();\nconsole.log("Encrypted message:", ciphertext);\n\n// Decryption\nconst bytes = CryptoJS.AES.decrypt(ciphertext, secretKey);\nconst decryptedMessage = bytes.toString(CryptoJS.enc.Utf8);\nconsole.log("Decrypted message:", decryptedMessage);
\n\n

This example demonstrates the basic cycle of working with symmetric encryption: we transform the original text into an encrypted string (Base64), and then restore the original message using the same key.

\n\n

Main Methods/Functions/Classes

\n\n

Let's look at the key capabilities of Crypto-js. For each method, the signature, a brief description, and a working example are provided.

\n\n

1. CryptoJS.AES.encrypt() / CryptoJS.AES.decrypt()

\n\n

Signature: CryptoJS.AES.encrypt(message, key, [options]) and CryptoJS.AES.decrypt(ciphertext, key, [options])

\n\n

What it does: Implements symmetric encryption and decryption using the AES (Advanced Encryption Standard) algorithm. Supports ECB, CBC, CFB, OFB, CTR modes and key sizes of 128/192/256 bits.

\n\n
const CryptoJS = require("crypto-js");\n\nconst message = "Secret data";\nconst key = "supersecretkey123";\n\n// Encryption with specified mode and IV (initialization vector)\nconst iv = CryptoJS.lib.WordArray.random(16); // 16 bytes for AES-128\nconst encrypted = CryptoJS.AES.encrypt(message, key, {\n    iv: iv,\n    mode: CryptoJS.mode.CBC,\n    padding: CryptoJS.pad.Pkcs7\n}).toString();\n\nconsole.log("Encrypted (CBC):", encrypted);\n\n// Decryption\nconst decrypted = CryptoJS.AES.decrypt(encrypted, key, {\n    iv: iv,\n                    

Recommendations