Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
171 views
in Technique[技术] by (71.8m points)

node.js - 如何在node.js中进行Base64编码?(How to do Base64 encoding in node.js?)

Does node.js have built-in base64 encoding yet? (node.js有内置的base64编码吗?)

The reason why I ask this is that final() from crypto can only output hex, binary or ascii data. (我之所以这么说是因为来自crypto final()只能输出hex,binary或ascii数据。) 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 docs, update() can output base64-encoded data. (根据文档, update()可以输出base64编码的数据。) However, final() doesn't support base64. (但是, final()不支持base64。) I tried and it will break. (我试过了,它会破裂。)

If I do this: (如果我这样做:)

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

Then what should I use for decryption? (然后我应该用什么解密?) Hex or base64? (Hex或base64?)

Therefore, I'm looking for a function to base64-encode my encrypted hex output. (因此,我正在寻找一个函数来对我的加密十六进制输出进行base64编码。)

Thanks. (谢谢。)

  ask by murvinlai translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Buffers can be used for taking a string or piece of data and doing base64 encoding of the result. (缓冲区可用于获取字符串或数据,并对结果进行base64编码。) For example: (例如:)

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

Buffers are a global object, so no require is needed. (缓冲区是一个全局对象,因此不需要。) Buffers created with strings can take an optional encoding parameter to specify what encoding the string is in. The available toString and Buffer constructor encodings are as follows: (使用字符串创建的缓冲区可以使用可选的编码参数来指定字符串所在的编码。可用的toStringBuffer构造函数编码如下:)

'ascii' - for 7 bit ASCII data only. ('ascii' - 仅适用于7位ASCII数据。) This encoding method is very fast, and will strip the high bit if set. (这种编码方法非常快,如果设置将剥离高位。)

'utf8' - Multi byte encoded Unicode characters. ('utf8' - 多字节编码的Unicode字符。) Many web pages and other document formats use UTF-8. (许多网页和其他文档格式使用UTF-8。)

'ucs2' - 2-bytes, little endian encoded Unicode characters. ('ucs2' - 2字节,小端编码的Unicode字符。) It can encode only BMP(Basic Multilingual Plane, U+0000 - U+FFFF). (它只能编码BMP(基本多语言平面,U + 0000 - U + FFFF)。)

'base64' - Base64 string encoding. ('base64' - Base64字符串编码。)

'binary' - A way of encoding raw binary data into strings by using only the first 8 bits of each character. ('binary' - 一种通过仅使用每个字符的前8位将原始二进制数据编码为字符串的方法。) This encoding method is deprecated and should be avoided in favor of Buffer objects where possible. (不推荐使用此编码方法,应尽可能避免使用Buffer对象。) This encoding will be removed in future versions of Node. (在将来的Node版本中将删除此编码。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...