【问题标题】:Read a binary with javascript使用 javascript 读取二进制文件
【发布时间】:2021-12-04 07:38:43
【问题描述】:

我怎样才能读取像下面这样的二进制文件?

Binary2 {
  sub_type: 0,
  buffer: Buffer(16) [
    12,  15,  64, 88, 174,  93,
    16, 250, 162,  5, 122, 223,
    16,  98, 207, 68
  ],
  position: 16
}

我尝试了几件事,比如制作一个 parseInt:

错误: NaN

或使用新的 Uint16Array():

错误:

error: TS2769 [ERROR]: No overload matches this call.
  The last overload gave the following error.
    Argument of type 'string' is not assignable to parameter of type 'ArrayBufferLike'.
    console.log('test :', new Uint16Array(contact.firstname))
TS2771 [ERROR]:     The last overload is declared here.
        new(buffer: ArrayBufferLike, byteOffset?: number, length?: number): Uint16Array;

有人可以帮我读取这个二进制文件的缓冲区并解密我的 aes 数据吗?

【问题讨论】:

  • 该错误消息抱怨new Uint16Array(contact.firstname),其中contact.firstname 是字符串类型(与Buffer 无关),并且此contact 和语句均不在您提供的代码中。你能让你的问题保持一致吗?
  • @trincot 我的应用程序使用 Deno 和三方模块 god_crypto 来加密 AES 数据。我在 mongoDB 中插入“字符串”格式的数据,并使用相同的三方模块 god_crypto 解密。当我使用方法 console.log() 我有这个输出 Binary2 { sub_type: 0, buffer: Buffer(16) [ 12, 15, 64, 88, 174, 93, 16, 250, 162, 5, 122, 223, 16, 98, 207, 68 ], position: 16 } 我只想得到这个对象的键“缓冲区”的值来设置在 await aes.decrypt(<BUFFER>) 就像在这里解释 - > github.com/invisal/god_crypto

标签: javascript node.js typescript binary


【解决方案1】:

您并没有真正指定文件格式,对于这种情况我创建了一个代码,如果您需要其他情况,您可以修改(我的代码现在只是使用 StrBetween 提取从“[”到“]”的数字):

 function StrBetween(str, s1, s2){    
     let pos1 = str.indexOf(s1);
     if(pos1>-1){
       pos1 += s1.length;
       return str.substr(pos1, str.indexOf(s2, pos1)-pos1);
     } else {
       return "";
     } 
    } 

  function Convert(){         
    var data = `
      Binary2 {
      sub_type: 0,
      buffer: Buffer(16) [
        12,  15,  64, 88, 174,  93,
        16, 250, 162,  5, 122, 223,
        16,  98, 207, 68
      ],
      position: 16
    }
    `;
    
    let values = StrBetween(data, "[", "]").split(",");
    var uint16 = new Uint16Array(values.length);
    
    for(let i=0; i<values.length; i++){
     values[i] = values[i].replace('\n','').trim(); //Remove line breaks and spaces
     uint16[i] = parseInt(values[i]);
    }
    
    console.log(uint16); //Show the array               
    }

【讨论】:

    【解决方案2】:

    好吧,我很愚蠢。我找到了解决方案。

    1.像这样在 mongoDB 中使用 hex() 加密和设置数据:

    let firstname = await aes.encrypt(data.fields.firstname)
    data.fields.firstname = firstname.hex()
    

    2。 mongoDB中的字符串格式数据:

    b9314d7b7f24e79bc08518e956f9125a
    

    3.将十六进制字符串转换为字节数组并解密来自 mongoDB 的数据:

    for (var bytes = [], c = 0; c < contact.firstname.length; c += 2) {
          bytes.push(parseInt(contact.firstname.substr(c, 2), 16));
        }
    let cipher = await aes.decrypt(new Uint8Array(bytes))
    contact.firstname = cipher.toString()
    

    感谢您的帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-20
      • 2019-04-13
      • 2020-11-11
      • 2012-09-11
      • 2019-09-25
      • 2017-06-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多