Go和Node.js base64编码,解码问题

Go和Node.js base64编码,解码问题

问题描述:

I am base64 encoding a string in Go

Then decoding in javascript (I have tried 3 different methods)

I cannot get the javascript result to match the original Go string

Go (encode)

a := []byte {138,143,163,224,178,73,161,15,240,121,53,192,198,182,52,245}
fmt.Println("a", string(a), a, len(a))
b := base64.StdEncoding.EncodeToString(a)
fmt.Println("b", b, []byte(b), len([]byte(b)))

js (decode)

 const b = [105,111,43,106,52,76,74,74,111,81,47,119,101,84,88,65,120,114,89,48,57,81,61,61];
 let bString = aesjs.utils.utf8.fromBytes(b);
 console.log("b", bString, b, b.length);

 let a1String = atob(bString);
 let a2String = Base64.decode(bString);
 let a3String = Buffer.from(bString, 'base64').toString('utf8');
 let a1 = aesjs.utils.utf8.toBytes(a1String);
 let a2 = aesjs.utils.utf8.toBytes(a2String);
 let a3 = aesjs.utils.utf8.toBytes(a3String);
 console.log("a", a1, a1.length, a2, a2.length, a3, a3.length);

All 3 methods fail, i.e. a1 != a, a2 != a, a3 != a

I assumed base64 encoding/decoding would be simple

What am I missing? Thanks

EDIT: The js code had a 'typo' - but the problem remains, that ai != a, even a1 != a2 == a3

EDIT: If the original a is simple, then everything works. But when a contains non-printable chars, I cannot decode in js Example where it works: a := []byte {65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65} Here it does not works: a := []byte {138, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65}

我在Go中使用base64编码字符串 p>

然后在javascript中解码( 我尝试了3种不同的方法) p>

我无法获得与原始Go字符串匹配的javascript结果 p>

Go(编码) strong> p>

  a:= [] byte {138,143,163,224,178,73,161,15,240,121,53,192,198,182,52,245} 
fmt.Println(“ a”,string(a),a,len  (a))
b:= base64.StdEncoding.EncodeToString(a)
fmt.Println(“ b”,b,[] byte(b),len([] byte(b)))
  code>   pre> 
 
 

js(解码) strong> p>

  const b = [105,111,43,106,52,76,74,  74,111,81,47,119,101,84,88,65,120,114,89,48,57,81,61,61]; 
让bString = aesjs.utils.utf8.fromBytes(b); 
 console.log(“ b”  ,bString,b,b.length); 
 
让a1String = atob(bString); 
让a2String = Base64.decode(bString); 
让a3String = Buffer.from(bString,'base64')。  toString('utf8'); 
让a1 = aesjs.utils.utf8.toBytes(a1String); 
让a2 = aesjs.utils.utf8.toBytes(a2String); 
让a3 = aesj  s.utils.utf8.toBytes(a3String); 
 console.log(“ a”,a1,a1.length,a2,a2.length,a3,a3.length); 
  code>  pre>  
 
 

所有3种方法均会失败,即a1!= a,a2!= a,a3!= a p>

我认为base64编码/解码将很简单 p >

我缺少什么? 谢谢 p>

编辑:js代码中有'typo'-但问题仍然存在,即ai!= a,甚至a1!= a2 == a3 p>

编辑:如果原始的a很简单,那么一切正常。 strong>但是,当a包含不可打印的字符时,我无法在js 中解码它的工作方式: a:= [] byte {65,65,65,65,65,65,65,65,65,65,65,65,65,65,65,65} 此处不起作用: a:= [] byte {138, 65,65,65,65,65,65,65,65,65,65,65,65,65,65,65} p> div>

In Go, strings are UTF8:

in := "Good afternoon, or こんにちは"
buf := []byte(in)
str := base64.StdEncoding.EncodeToString(buf)
fmt.Println(str)

That prints the base64 encoded (ASCII safe) representation: R29vZCBhZnRlcm5vb24sIG9yIOOBk+OCk+OBq+OBoeOBrw==

JS strings are UTF-16. So you need to both decode from base64 and convert from utf8 to utf-16. You could use a library, or a helper function, for example:

function b64DecodeUnicode(str) {
    return decodeURIComponent(atob(str).split('').map(function(c) {
        return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
    }).join(''));
}

var str = "R29vZCBhZnRlcm5vb24sIG9yIOOBk+OCk+OBq+OBoeOBrw==";
var result = b64DecodeUnicode(str);
"Good afternoon, or こんにちは"

The b64DecodeUnicode() function is copied from this answer, see also the link to MDN for detailed info.

From: https://github.com/ricmoo/aes-js/blob/master/README.md

UTF8 should NOT be used to store arbitrary binary data as it is a string encoding format, not a binary encoding format

So don't used utf8 related functions to convert raw (non utf8) binary data.