我真的希望这能奏效,但是...

我在标题中得到了base64字符串...我想将其编码为UTF8。

strInit = req.headers['authorization']
buf = new Buffer(strInit.length)
i = 0

while i < strInit.length
  buf[i] = strInit.charCodeAt(i)
  i++
str = buf.toString()
str2 = new Buffer(str, 'base64').toString()
console.log("AUTH REQUEST :",strInit, buf, str, str2)


AUTH REQUEST : Basic dXNlckBnbWFpbC5jb206cXdlcnR5
<Buffer 42 61 73 69 63 20 64 58 4e 6c 63 6b 42 6e 62 57 46 70 62 43 35 6a
62 32 30 36 63 58 64 6c 63 6e 52 35> Basic dXNlckBnbWFpbC5jb206cXdlcnR5
�"q�͕������������ݕ��


我尝试在线解码它,并按预期显示([email protected]:qwerty)

例如,在这里工作正常:http://www.base64decode.org

我想念什么?

最佳答案

解决了:
OK,实际上我找到了它……我必须从字符串中删除“ Basic”,以使解码器不会感到困惑。

所以解决方案是:

new Buffer(req.headers['authorization'].replace("Basic ",""),"base64").toString()


这样工作。

10-08 11:18