Crypto-js for JavaScript (Node.js): Complete Guide with Examples
\n\nIn 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\nThis 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\nIn 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\nInstallation
\n\nTo install Crypto-js in a Node.js project, run the following command in the terminal:
\n\nnpm install crypto-js\n\nAfter 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\nQuick Start: Minimal Working Example
\n\nLet's start with a simple example — encrypting and decrypting a string using the AES algorithm:
\n\nconst 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\nThis 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\nMain Methods/Functions/Classes
\n\nLet's look at the key capabilities of Crypto-js. For each method, the signature, a brief description, and a working example are provided.
\n\n1. CryptoJS.AES.encrypt() / CryptoJS.AES.decrypt()
\n\nSignature: CryptoJS.AES.encrypt(message, key, [options]) and CryptoJS.AES.decrypt(ciphertext, key, [options])
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\nconst 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