Last active
November 15, 2024 01:39
-
-
Save vlucas/2bd40f62d20c1d49237a109d491974eb to your computer and use it in GitHub Desktop.
Stronger Encryption and Decryption in Node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const crypto = require('crypto'); | |
const ENCRYPTION_KEY = process.env.ENCRYPTION_KEY; // Must be 256 bits (32 characters) | |
const IV_LENGTH = 16; // For AES, this is always 16 | |
function encrypt(text) { | |
let iv = crypto.randomBytes(IV_LENGTH); | |
let cipher = crypto.createCipheriv('aes-256-cbc', Buffer.from(ENCRYPTION_KEY), iv); | |
let encrypted = cipher.update(text); | |
encrypted = Buffer.concat([encrypted, cipher.final()]); | |
return iv.toString('hex') + ':' + encrypted.toString('hex'); | |
} | |
function decrypt(text) { | |
let textParts = text.split(':'); | |
let iv = Buffer.from(textParts.shift(), 'hex'); | |
let encryptedText = Buffer.from(textParts.join(':'), 'hex'); | |
let decipher = crypto.createDecipheriv('aes-256-cbc', Buffer.from(ENCRYPTION_KEY), iv); | |
let decrypted = decipher.update(encryptedText); | |
decrypted = Buffer.concat([decrypted, decipher.final()]); | |
return decrypted.toString(); | |
} | |
module.exports = { decrypt, encrypt }; |
Great work @nijynot !
Nice work by @nijynot! If anyone is facing "TypeError: Cannot read properties of undefined (reading 'randomBytes')", change the import import crypto from 'crypto';
to import { createCipheriv, createDecipheriv, randomBytes } from 'crypto'
. Wonder why ES gives this error, but this fix should work
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm surprised by the glaring mistakes (?) in how the encryption key is actually 16 bytes and IV is 8 bytes?
This is largely because he's reading a string as a buffer, without using the
hex
argument.Fixes
hex
argument, it's parsed incorrectly. All hex values are read half their size - so 32 bytes is actually 16 bytes and 16 bytes is 8 bytes. Amending this also fixes theInvalid key length
errors that people have in the thread..slice
your string if it's 16 bytes. If you generate a string of 16 bytes, that should fit fully into thecreateCipheriv
function from Node'scrypto
package.keyGen()
function that generates a key for you, so that there's no misunderstanding or ambiguity in how you should generate this key. Saw some people mention other random string generation packages, but there's no reason to have that dependency when you can usecrypto
'srandomBytes
directly.const
for variables instead oflet
.Contribute
There are ways to improve the code futher. If you want to improve this, then you could:
0x
for less ambiguityI'll leave that to someone else to figure out although, if someone wants to.
Updated code (2024-02-15)
You should use this instead of OP and OP's TypeScript version posted above.