用javascript读取二进制文件



如何读取像下面这样的二进制文件?

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

或with new 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数据吗?

您没有真正指定文件格式,对于这种情况,我创建了一个代码,如果需要,您可以修改其他情况(我的代码现在只是从"["]";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               
}

好吧,我真笨。我找到解决办法了。

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()

谢谢你的帮助!

最新更新