【问题标题】:How do I make an JSON Obfuscator? UTF 8 to UTF 16 (Js)如何制作 JSON 混淆器? UTF 8 到 UTF 16 (Js)
【发布时间】:2021-10-05 14:54:55
【问题描述】:

我正在尝试制作一个从 UTF-8 到 UTF-16 的 Unicode 转换器,但是我只想在 JSON 中转换字符串和对象之类的东西,而不是 JSON 的结构,因为这会使程序无法读取。

输入 UTF-8:

{
    "hi":{
        "this":["is","just","some","example"]
    }
}

预期的 UTF-16:

{
    "\u0068\u0069":{
        "\u0074\u0068\u0069\u0073":["\u0069\u0073","\u006a\u0075\u0073\u0074","\u0073\u006f\u006d\u0065","\u0065\u0078\u0061\u006d\u0070\u006c\u0065"]
    }
}

有人可以帮忙吗?另外,我对 Javascript 还很陌生,所以不要太苛刻。

【问题讨论】:

  • 使用分词器查找所有字符串,每个 the JSON spec 只能是“以双引号开头和结尾的东西”。然后用重新编码的值替换每个找到的条目。
  • @Mike'Pomax'Kamermans 但我该如何重新编码?这是我的大问题
  • 如果这是您的问题,那么您的问题与 JSON 无关,您应该更新您的帖子。在搜索“如何将 JS 字符串转换为 UTF16”后,看看互联网上是否有答案(有,even here on SO)。

标签: javascript json unicode utf-8 converters


【解决方案1】:

这是完成您需要的工作的好方法:

let json_string = '{ "hi": {"this": ["is", "just", "some", "example"]}}'
function encode_to_utf16(x) {
  var res = "";
  for (var i = 0; i < x.length; i++) res+= "\\u" + ("000" + x[i].charCodeAt(0).toString(16)).substr(-4);
  return res;
}

// get all the values present between the double quotes
let arr = json_string.match(/"[^"]+"/gi)
arr.forEach((str)=>{
  // replace double quotes with empty string
  str = str.replace(/"/gi,"");
  // encode and replace them
  json_string = json_string.replace(str,encode_to_utf16(str))
})
console.log(json_string)

然而,如果在字符串中的某处有一个带有转义字符 (\") 的双引号,那么它就会崩溃。也许,你可以即兴发挥。

【讨论】:

    猜你喜欢
    • 2020-01-28
    • 1970-01-01
    • 1970-01-01
    • 2021-01-30
    • 2011-01-11
    • 2012-08-18
    • 2021-01-06
    • 1970-01-01
    • 2013-11-19
    相关资源
    最近更新 更多