【发布时间】:2011-09-05 03:43:34
【问题描述】:
Node.js 有内置的 Base64 编码吗?
我问这个的原因是final() from crypto只能输出十六进制、二进制或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 编码的函数。
【问题讨论】:
-
对于在 2016 年或之后寻找此问题的人
cipher.final('base64')工作 -
试试 base122 github.com/kevinAlbs/Base122