Symmetric encryption algorithms for data confidentiality with support for various cipher modes and padding schemes. All encryption functions follow a consistent pattern with encrypt/decrypt methods that accept configuration options.
Advanced Encryption Standard (AES) with support for 128, 192, and 256-bit keys.
/**
* Encrypts a message using AES algorithm
* @param message - String or WordArray to encrypt
* @param key - String or WordArray encryption key
* @param cfg - Optional configuration object
* @returns CipherParams object containing encrypted data
*/
CryptoJS.AES.encrypt(message, key, cfg)
/**
* Decrypts AES encrypted data
* @param ciphertext - CipherParams object or string to decrypt
* @param key - String or WordArray decryption key
* @param cfg - Optional configuration object
* @returns WordArray containing decrypted data
*/
CryptoJS.AES.decrypt(ciphertext, key, cfg)Usage Examples:
var CryptoJS = require("crypto-js");
// Basic AES encryption/decryption
var message = "Hello World";
var key = "secret key 123";
var encrypted = CryptoJS.AES.encrypt(message, key);
console.log(encrypted.toString()); // Base64 encoded result
var decrypted = CryptoJS.AES.decrypt(encrypted, key);
console.log(decrypted.toString(CryptoJS.enc.Utf8)); // "Hello World"
// AES with custom configuration
var encrypted = CryptoJS.AES.encrypt(message, key, {
mode: CryptoJS.mode.CTR,
padding: CryptoJS.pad.NoPadding,
iv: CryptoJS.enc.Hex.parse("101112131415161718191a1b1c1d1e1f")
});
// Object encryption
var data = [{id: 1}, {id: 2}];
var encrypted = CryptoJS.AES.encrypt(JSON.stringify(data), key);
var bytes = CryptoJS.AES.decrypt(encrypted, key);
var decrypted = JSON.parse(bytes.toString(CryptoJS.enc.Utf8));Data Encryption Standard (DES) - not recommended for new applications due to small key size.
/**
* Encrypts a message using DES algorithm
* @param message - String or WordArray to encrypt
* @param key - String or WordArray encryption key
* @param cfg - Optional configuration object
* @returns CipherParams object containing encrypted data
*/
CryptoJS.DES.encrypt(message, key, cfg)
/**
* Decrypts DES encrypted data
* @param ciphertext - CipherParams object or string to decrypt
* @param key - String or WordArray decryption key
* @param cfg - Optional configuration object
* @returns WordArray containing decrypted data
*/
CryptoJS.DES.decrypt(ciphertext, key, cfg)Triple Data Encryption Standard (3DES) providing better security than DES.
/**
* Encrypts a message using Triple DES algorithm
* @param message - String or WordArray to encrypt
* @param key - String or WordArray encryption key
* @param cfg - Optional configuration object
* @returns CipherParams object containing encrypted data
*/
CryptoJS.TripleDES.encrypt(message, key, cfg)
/**
* Decrypts Triple DES encrypted data
* @param ciphertext - CipherParams object or string to decrypt
* @param key - String or WordArray decryption key
* @param cfg - Optional configuration object
* @returns WordArray containing decrypted data
*/
CryptoJS.TripleDES.decrypt(ciphertext, key, cfg)RC4 stream cipher with optional key-drop configuration.
/**
* Encrypts a message using RC4 stream cipher
* @param message - String or WordArray to encrypt
* @param key - String or WordArray encryption key
* @param cfg - Optional configuration object
* @returns CipherParams object containing encrypted data
*/
CryptoJS.RC4.encrypt(message, key, cfg)
/**
* Decrypts RC4 encrypted data
* @param ciphertext - CipherParams object or string to decrypt
* @param key - String or WordArray decryption key
* @param cfg - Optional configuration object
* @returns WordArray containing decrypted data
*/
CryptoJS.RC4.decrypt(ciphertext, key, cfg)
/**
* RC4 with configurable key drop for enhanced security
* @param message - String or WordArray to encrypt
* @param key - String or WordArray encryption key
* @param cfg - Optional configuration object with drop parameter
* @returns CipherParams object containing encrypted data
*/
CryptoJS.RC4Drop.encrypt(message, key, cfg)
CryptoJS.RC4Drop.decrypt(ciphertext, key, cfg)Usage Example:
var CryptoJS = require("crypto-js");
// RC4Drop with default drop (192 keystream words)
var encrypted = CryptoJS.RC4Drop.encrypt('my message', 'secret key 123');
console.log(encrypted.toString());
// RC4Drop with custom drop value
var customEncrypted = CryptoJS.RC4Drop.encrypt('my message', 'secret key 123', {
drop: 256 // Drop 256 keystream words for extra security
});
var decrypted = CryptoJS.RC4Drop.decrypt(customEncrypted, 'secret key 123', {
drop: 256 // Must match encryption drop value
});
console.log(decrypted.toString(CryptoJS.enc.Utf8));Rabbit stream cipher providing fast encryption with good security properties.
/**
* Encrypts a message using Rabbit stream cipher
* @param message - String or WordArray to encrypt
* @param key - String or WordArray encryption key
* @param cfg - Optional configuration object
* @returns CipherParams object containing encrypted data
*/
CryptoJS.Rabbit.encrypt(message, key, cfg)
/**
* Decrypts Rabbit encrypted data
* @param ciphertext - CipherParams object or string to decrypt
* @param key - String or WordArray decryption key
* @param cfg - Optional configuration object
* @returns WordArray containing decrypted data
*/
CryptoJS.Rabbit.decrypt(ciphertext, key, cfg)
/**
* Legacy Rabbit stream cipher for compatibility
*/
CryptoJS.RabbitLegacy.encrypt(message, key, cfg)
CryptoJS.RabbitLegacy.decrypt(ciphertext, key, cfg)Blowfish symmetric cipher with variable key length.
/**
* Encrypts a message using Blowfish algorithm
* @param message - String or WordArray to encrypt
* @param key - String or WordArray encryption key
* @param cfg - Optional configuration object
* @returns CipherParams object containing encrypted data
*/
CryptoJS.Blowfish.encrypt(message, key, cfg)
/**
* Decrypts Blowfish encrypted data
* @param ciphertext - CipherParams object or string to decrypt
* @param key - String or WordArray decryption key
* @param cfg - Optional configuration object
* @returns WordArray containing decrypted data
*/
CryptoJS.Blowfish.decrypt(ciphertext, key, cfg)var CryptoJS = require("crypto-js");
var message = "Sensitive data";
var key = "encryption-key";
var iv = CryptoJS.lib.WordArray.random(128/8); // Random IV
// AES encryption with custom mode and padding
var encrypted = CryptoJS.AES.encrypt(message, key, {
mode: CryptoJS.mode.CFB,
padding: CryptoJS.pad.AnsiX923,
iv: iv
});
// Access cipher components
console.log("Ciphertext:", encrypted.ciphertext.toString());
console.log("Salt:", encrypted.salt.toString());
console.log("IV:", encrypted.iv.toString());var CryptoJS = require("crypto-js");
// Generate secure random key and IV
var key = CryptoJS.lib.WordArray.random(256/8); // 256-bit key
var iv = CryptoJS.lib.WordArray.random(128/8); // 128-bit IV
var message = "Secret message";
// Encrypt with explicit key and IV
var encrypted = CryptoJS.AES.encrypt(message, key, { iv: iv });
// Store key and IV securely (this is just an example)
var keyHex = key.toString();
var ivHex = iv.toString();
// Later, decrypt with stored key and IV
var storedKey = CryptoJS.enc.Hex.parse(keyHex);
var storedIv = CryptoJS.enc.Hex.parse(ivHex);
var decrypted = CryptoJS.AES.decrypt(encrypted, storedKey, { iv: storedIv });
console.log(decrypted.toString(CryptoJS.enc.Utf8));var CryptoJS = require("crypto-js");
// Encrypt binary data (e.g., from Buffer)
var binaryData = new Uint8Array([1, 2, 3, 4, 5]);
var wordArray = CryptoJS.lib.WordArray.create(binaryData);
var encrypted = CryptoJS.AES.encrypt(wordArray, "binary-key");
var decrypted = CryptoJS.AES.decrypt(encrypted, "binary-key");
// Convert back to Uint8Array if needed
var decryptedBytes = new Uint8Array(decrypted.words.length * 4);
for (var i = 0; i < decrypted.words.length; i++) {
var word = decrypted.words[i];
decryptedBytes[i * 4] = (word >> 24) & 0xff;
decryptedBytes[i * 4 + 1] = (word >> 16) & 0xff;
decryptedBytes[i * 4 + 2] = (word >> 8) & 0xff;
decryptedBytes[i * 4 + 3] = word & 0xff;
}// Encryption Configuration
EncryptionConfig {
iv?: WordArray; // Initialization vector
mode?: Object; // Cipher mode (default: CBC)
padding?: Object; // Padding scheme (default: Pkcs7)
format?: Object; // Output formatter (default: OpenSSL)
}
// Cipher Result
CipherParams {
ciphertext: WordArray; // Encrypted data
key?: WordArray; // Encryption key (when password-based)
iv?: WordArray; // Initialization vector (when generated)
salt?: WordArray; // Salt value (when password-based)
algorithm?: Object; // Algorithm reference
mode?: Object; // Cipher mode used
padding?: Object; // Padding scheme used
blockSize?: number; // Block size in words
formatter?: Object; // Output formatter used
toString(formatter?): string; // Serialize to string
}
// Stream Cipher Configuration
StreamCipherConfig {
drop?: number; // Number of keystream words to drop (RC4Drop)
}