How to Decrypt Data in Node.js Using Crypto Module
In Node.js, you can decrypt data using the
crypto module's createDecipheriv method by providing the algorithm, key, and initialization vector (IV). Then, use decipher.update() and decipher.final() to get the original plaintext.Syntax
To decrypt data in Node.js, use the crypto.createDecipheriv(algorithm, key, iv) method. Here:
- algorithm: The encryption algorithm used (e.g.,
'aes-256-cbc'). - key: The secret key used for encryption/decryption (must match the encryption key).
- iv: Initialization vector, a random value used during encryption (must match the encryption IV).
Then call decipher.update(encryptedData, inputEncoding, outputEncoding) to decrypt chunks of data, and decipher.final(outputEncoding) to finish and get remaining decrypted data.
javascript
const crypto = require('crypto'); const algorithm = 'aes-256-cbc'; const key = Buffer.from('your-32-byte-long-key-goes-here-1234'); const iv = Buffer.from('your-16-byte-iv1234'); const decipher = crypto.createDecipheriv(algorithm, key, iv); let decrypted = decipher.update(encryptedData, 'hex', 'utf8'); decrypted += decipher.final('utf8'); console.log(decrypted);
Example
This example shows how to decrypt a message encrypted with AES-256-CBC. It uses the same key and IV for decryption to get back the original text.
javascript
const crypto = require('crypto'); const algorithm = 'aes-256-cbc'; const key = crypto.randomBytes(32); // 32 bytes key for aes-256 const iv = crypto.randomBytes(16); // 16 bytes IV const message = 'Hello, Node.js encryption!'; // Encrypt const cipher = crypto.createCipheriv(algorithm, key, iv); let encrypted = cipher.update(message, 'utf8', 'hex'); encrypted += cipher.final('hex'); console.log('Encrypted:', encrypted); // Decrypt const decipher = crypto.createDecipheriv(algorithm, key, iv); let decrypted = decipher.update(encrypted, 'hex', 'utf8'); decrypted += decipher.final('utf8'); console.log('Decrypted:', decrypted);
Output
Encrypted: <hexadecimal string>
Decrypted: Hello, Node.js encryption!
Common Pitfalls
- Key and IV mismatch: Using a different key or IV than used for encryption will cause decryption to fail or produce garbage.
- Wrong encoding: Make sure to use the correct input and output encodings in
updateandfinalmethods. - Buffer length: The key and IV must have the correct byte length for the chosen algorithm (e.g., 32 bytes for AES-256 key, 16 bytes for IV).
- Not handling errors: Always wrap decryption in try-catch to handle exceptions from invalid data.
javascript
try { const decipher = crypto.createDecipheriv(algorithm, wrongKey, iv); let decrypted = decipher.update(encrypted, 'hex', 'utf8'); decrypted += decipher.final('utf8'); console.log(decrypted); } catch (error) { console.error('Decryption failed:', error.message); }
Quick Reference
Tips for decrypting data in Node.js:
- Use
crypto.createDecipherivwith the exact algorithm, key, and IV used for encryption. - Ensure key and IV lengths match algorithm requirements.
- Use consistent encoding formats (e.g., 'hex' for encrypted data, 'utf8' for plaintext).
- Wrap decryption code in try-catch to handle errors gracefully.
Key Takeaways
Use crypto.createDecipheriv with the correct algorithm, key, and IV to decrypt data.
Ensure key and IV lengths match the encryption algorithm requirements exactly.
Match input and output encodings when calling decipher.update and decipher.final.
Always handle errors during decryption to avoid crashes from invalid data.
Use the same key and IV that were used to encrypt the data for successful decryption.