在 javascript 和 Java 中使用 forge 解码 UTF8 之间的奇怪差异

Strange differences between decoding UTF8 with forge in javascript and Java

我正在尝试在 java 脚本中使用 forge.util.decodeUtf8,但我的结果与 java 不同,你能帮帮我吗?

var a = forge.util.hexToBytes("037A4078C3AD65C38863226AC3BD64C2B5392C6CCB8646617075342B473079C3954FC2A553C3BE6D");
aDecoded = forge.util.decodeUtf8(a);
console.log(forge.util.bytesToHex(aDecoded);
>> 037a4078ed65c863226afd64b5392c6c2c646617075342b473079d54fa553fe6d

这是 java

中的结果
byte[] a = hexToBytes("037A4078C3AD65C38863226AC3BD64C2B5392C6CCB8646617075342B473079C3954FC2A553C3BE6D");

String aDecoded = new String(a, Charset.forName("UTF-8"));
byte[] r = test.getBytes();

System.out.println(bytesToHex(r));
>> 037A4078ED65E863226AFD64B5392C6C8866617075342B673079F56FA573FE6D

结果的不同就在这里

037a4078ed65c863226afd64b5392c6c**2c64**6617075342b473079d54fa553fe6d
037A4078ED65E863226AFD64B5392C6C**8866**6617075342B673079F56FA573FE6D

我不明白为什么我有不同的结果。

发布的字节序列可以解码为 UTF8 字符串,可以使用 UTF8 table. Note, however, that generally an arbitrary byte sequence cannot be converted into a UTF8 string, here 轻松验证。

Forge 代码包含导致错误结果的错误:十六进制字符串被转换为具有 hexToBytes and decoded into a UTF8 string with decodeUtf8. For the reverse, the UTF8 string must first be encoded into a binary encoded string of bytes with encodeUtf8 and converted into a hexadecimal string with bytesToHex 的二进制编码字节字符串。在发布的 Forge 代码中,缺少带有 encodeUtf8 的编码。有

console.log(forge.util.bytesToHex(forge.util.encodeUtf8(aDecoded))); 

显示正确的结果。

Java 代码中有两个小问题:变量 test 未定义,必须由变量 aDecoded 替换。此外,当为 bytesToHex 调用方法 getBytes, the UTF8 encoding should be specified, otherwise the default platform charset is used, which is a possible error source. Apart from that, the Java code seems to be correct. However, since the two methods hexToBytes and bytesToHex haven't been posted, it cannot be ruled out that the error may be here. If e.g. this implementation is used for hexToBytes and this 实现时,会显示正确的结果。