最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

如何在节点js中解压缩二进制数据

IT培训 admin 5浏览 0评论

如何在节点js中解压缩二进制数据

将基数为64的数据解密为二进制后,我想解压缩该数据。

在PHP语言中,我们具有gzdecode()内置函数,但是我在项目中使用了节点js,但我不知道如何使用gzip解压缩二进制数据。我在请求正文中获得了加密数据,然后将其解密,然后想解压缩解密的数据。完成此步骤后,我将解密后的数据存储在mongodb数据库中

我的用于将数据从base 64解密为二进制形式的代码

function decrypt(text, salt, iv) {
const password = "password";
crypto.pbkdf2(password, salt, 65536, 256, "sha256", (err, key) => {
  try {
    const key32 = key.slice(0, 32);
    const decipher = crypto.createDecipheriv("aes-256-cbc", key32, iv);
    let decrypted = decipher.update(text, "base64", "binary");
    decrypted += decipher.final("binary");
    // and then I want decompress my `decrypted` variable
    // like gzdecode(decrypted) in php
  } catch (error) {
    throw new Error(error)
  }
});
}
  decrypt(plainText, salt, iv)
回答如下:

Node.js具有内置的ZLib支持。解密完成后,可以使用zlib.unzip(https://nodejs/api/zlib.html#zlib_zlib_unzip_buffer_options_callback)或zlib.unzipSync(https://nodejs/api/zlib.html#zlib_zlib_unzipsync_buffer_options)进行解压缩,或使用inflate / inflateSync(https://nodejs/api/zlib.html#zlib_zlib_inflate_buffer_options_callback)进行膨胀

希望有帮助!

如何在节点js中解压缩二进制数据

将基数为64的数据解密为二进制后,我想解压缩该数据。

在PHP语言中,我们具有gzdecode()内置函数,但是我在项目中使用了节点js,但我不知道如何使用gzip解压缩二进制数据。我在请求正文中获得了加密数据,然后将其解密,然后想解压缩解密的数据。完成此步骤后,我将解密后的数据存储在mongodb数据库中

我的用于将数据从base 64解密为二进制形式的代码

function decrypt(text, salt, iv) {
const password = "password";
crypto.pbkdf2(password, salt, 65536, 256, "sha256", (err, key) => {
  try {
    const key32 = key.slice(0, 32);
    const decipher = crypto.createDecipheriv("aes-256-cbc", key32, iv);
    let decrypted = decipher.update(text, "base64", "binary");
    decrypted += decipher.final("binary");
    // and then I want decompress my `decrypted` variable
    // like gzdecode(decrypted) in php
  } catch (error) {
    throw new Error(error)
  }
});
}
  decrypt(plainText, salt, iv)
回答如下:

Node.js具有内置的ZLib支持。解密完成后,可以使用zlib.unzip(https://nodejs/api/zlib.html#zlib_zlib_unzip_buffer_options_callback)或zlib.unzipSync(https://nodejs/api/zlib.html#zlib_zlib_unzipsync_buffer_options)进行解压缩,或使用inflate / inflateSync(https://nodejs/api/zlib.html#zlib_zlib_inflate_buffer_options_callback)进行膨胀

希望有帮助!

发布评论

评论列表 (0)

  1. 暂无评论