Does Node.js have built-in Base64 encoding support?

Does Node.js have built-in Base64 encoding support?

I’m asking because the final() method from the crypto module can only output hexadecimal, binary, or ASCII data.

For example:

var cipher = crypto.createCipheriv('des-ede3-cbc', encryption_key, iv);
var ciph = cipher.update(plaintext, 'utf8', 'hex');
ciph += cipher.final('hex');

var decipher = crypto.createDecipheriv('des-ede3-cbc', encryption_key, iv);
var txt = decipher.update(ciph, 'hex', 'utf8');
txt += decipher.final('utf8');

According to the documentation, update() can output Base64-encoded data, but final() doesn’t support Base64, and attempting to use it this way will break.

If I do this:

var ciph = cipher.update(plaintext, 'utf8', 'base64');
ciph += cipher.final('hex');

What should I use for decryption—hexadecimal or Base64?

I’m looking for a function to Node.js base64 encode my encrypted hexadecimal output.

You can manually Base64-encode the hexadecimal output from the crypto module.

Encrypt and encode to Base64:

const crypto = require('crypto');
const cipher = crypto.createCipheriv('des-ede3-cbc', encryption_key, iv);
let ciph = cipher.update(plaintext, 'utf8', 'hex');
ciph += cipher.final('hex');

// Convert hex to base64
const base64Encoded = Buffer.from(ciph, 'hex').toString('base64');
console.log(base64Encoded);

Decrypt and decode from Base64:

const decipher = crypto.createDecipheriv('des-ede3-cbc', encryption_key, iv);
// Convert base64 to hex
const hexEncoded = Buffer.from(base64Encoded, 'base64').toString('hex');

let txt = decipher.update(hexEncoded, 'hex', 'utf8');
txt += decipher.final('utf8');
console.log(txt);

You can directly use Base64 encoding in the update() method and then handle Base64 decoding accordingly.

Encrypt and encode to Base64:

const crypto = require('crypto');

const cipher = crypto.createCipheriv('des-ede3-cbc', encryption_key, iv);
let ciph = cipher.update(plaintext, 'utf8', 'base64');
ciph += cipher.final('base64');
console.log(ciph);

Decrypt from Base64:

const decipher = crypto.createDecipheriv('des-ede3-cbc', encryption_key, iv);

let txt = decipher.update(ciph, 'base64', 'utf8');
txt += decipher.final('utf8');
console.log(txt);

For larger data, you can use streams to handle Base64 encoding and decoding.

Encrypt and encode to Base64 using streams:

const crypto = require('crypto');
const { Transform } = require('stream');

const cipher = crypto.createCipheriv('des-ede3-cbc', encryption_key, iv);
const base64Encode = new Transform({
    transform(chunk, encoding, callback) {
        callback(null, chunk.toString('base64'));
    }
});

const inputStream = new Readable({
    read() {
        this.push(plaintext);
        this.push(null);
    }
});

inputStream.pipe(cipher).pipe(base64Encode).on('data', (encoded) => {
    console.log(encoded);
});

Decrypt from Base64 using streams:

const decipher = crypto.createDecipheriv('des-ede3-cbc', encryption_key, iv);
const base64Decode = new Transform({
    transform(chunk, encoding, callback) {
        callback(null, Buffer.from(chunk, 'base64').toString('hex'));
    }
});

const inputStream = new Readable({
    read() {
        this.push(ciph);
        this.push(null);
    }
});

inputStream.pipe(base64Decode).pipe(decipher).on('data', (decoded) => {
    console.log(decoded.toString('utf8'));
});