Node.js现在是否内置了Base64编码?
我之所以问这个问题是因为来自crypto
的final()
只能输出十六进制、二进制或ASCII数据。例如:
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');
根据文档,update()
方法可以输出 Base64 编码的数据。然而,final()
不支持 Base64。我尝试过了,这样做会导致出错。
如果我这样做:
var ciph = cipher.update(plaintext, 'utf8', 'base64');
ciph += cipher.final('hex');
那么解密时我应该使用什么?十六进制还是Base64?
因此,我正在寻找一个将我的加密十六进制输出进行Base64编码的函数。
cipher.final('base64')
是有效的解决方法。 - avck