How do I encode Base64 in node.js?

Posted by edwin_h on Mon, 30 Dec 2019 06:22:39 +0100

Does node.js have a built-in base64 encoding?

I said this because crypto final() only outputs hex, 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 document, update() can output base64-encoded data.However, final() does not support base64.I tried it. It will break.

If I do:

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

Then what should I decrypt with?Hex or base64?

So I'm looking for a function to base64 encode my encrypted hexadecimal output.

Thank you.

#1st floor

crypto now supports base64 ( Reference resources ):

cipher.final('base64') 

So you can do this:

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

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

#2nd floor

The accepted answers are contained in versions of nodes larger than 6 that are considered security issues (although this use case seems to always cast the input to a string).

according to File Buffer constructors are not recommended.

Following Is an example of a vulnerability that may result from using it in the ws library.

The code snippet should be:

console.log(Buffer.from("Hello World").toString('base64'));
console.log(Buffer.from("SGVsbG8gV29ybGQ=", 'base64').toString('ascii'));

#3rd floor

I use the following code to decode the base64 string in the node API nodejs version 10.7.0

let data = 'c3RhY2thYnVzZS5jb20=';  // Base64 string
let buff = new Buffer(data, 'base64');  //Buffer
let text = buff.toString('ascii');  //this is the data type that you want your Base64 data to convert to
console.log('"' + data + '" converted from Base64 to ASCII is "' + text + '"'); 

Please do not try to run the above code in the browser's console, it will not work properly.Place the code in the server-side file of nodejs.I use the above code in API development.

#4th floor

Buffer Can be used to get strings or data and base64 encode the results.For example:

> console.log(Buffer.from("Hello World").toString('base64'));
SGVsbG8gV29ybGQ=
> console.log(Buffer.from("SGVsbG8gV29ybGQ=", 'base64').toString('ascii'))
Hello World

Buffer is a global object, so it is not needed.Buffers created with strings can use optional encoding parameters to specify the encoding in which the string resides.The available toString and Buffer constructors are coded as follows:

'ascii'- For 7-bit ASCII data only.This encoding method is very fast and will strip off high bits if set.

'utf8'- Multibyte encoded Unicode character.Many Web pages and other document formats use UTF-8.

'ucs2'- 2 bytes, small-end encoded Unicode characters.It can only encode BMP (basic multilingual plane, U + 0000 - U + FFFF).

'base64'- Base64 string encoding.

'binary'- A method of encoding raw binary data as strings by using only the first 8 bits of each character.This encoding method is not recommended and Buffer objects should be avoided as much as possible.This encoding will be removed in a future version of Node.

Topics: encoding ascii