我有一个程序,我正在重写成Node.js。它是一个Python程序,base64对其进行解码和解压缩。
下面是Python的原始代码:
import base64
import zlib
def decode_str(data: str) -> str:
base64_decoded = base64.urlsafe_b64decode(data.encode())
decompressed = zlib.decompress(base64_decoded, 15 | 32)
return decompressed.decode()
我想在下面的字符串上使用它:H4sIAAAAAAAAC5VV25HDIAxsSM4gCfGY-7oaUgAFXAtX_EngC5DYJsmMUeLVrh4I8nPnBFhcIX1YH8RCIgWpGa7Glw1LKOicK7FgQbElqXsq-PsBHU_p6Kg074VEPpQwn0Z4S4SK8c9zye_JyKnMh_mEhdC6uXTIjhVgZetymcMhHQO-FZwP2PDzjQzOjDQTmvGga_se25vdJDN3zvUXAeraBCrw7evaUHTNILgvBFJLEDMwYIggEeyFwBYgwJadOqgaeVAPgnRLUUnZGQmzKCtoihzUR7BSLSBZJI2T9QNs8ZIoQfS4PEI2dpQa8oYGYAz-CaGGjBTtHYjF02wPeAMcZvJQY77ZO3utlW08lLY7J3P2jv-j7C35b8ZEQfbBlJKFFxnaAbGKaS-GLoSk_YNgLYqu7sEk4EMXiCaQDgS2S4XcFcQUwqcKQl2BTcGvi2gzsWfAnZ9tJDaUPhWDzqPhtmsvW1vb3V1oHpgnNF2iconSOng9IZsW_FA4H4iJkvN0qK4IaZkGY6-DB3Q_ASPsp7PRkEE7jWfjBY0nkdkvXd7I37_kP6J4iW7X8NkW77mldYV46GL_M-pTB3sa71qNu4me23bdxdoBuwXzcMF64pcbNl8NQ4v0fCUfXca7f5DpenPTjPJZlHouOHKvdzWlXupYe-7bcEb5AxMHn1whCQAA
当我用Python运行它时,它运行得很好,但是当我用JavaScript重写它时,它出错了,代码如下:
const zlib = require('zlib')
let b64decrypt = (encodedData) => {
return Buffer.from(encodedData, 'base64').toString();
}
let decrypt = (ls) => {
let fin = b64decrypt(ls);
return zlib.gunzipSync(fin, { windowBits: 15 | 32 });
}
let l = 'H4sIAAAAAAAAC5VV25HDIAxsSM4gCfGY-7oaUgAFXAtX_EngC5DYJsmMUeLVrh4I8nPnBFhcIX1YH8RCIgWpGa7Glw1LKOicK7FgQbElqXsq-PsBHU_p6Kg074VEPpQwn0Z4S4SK8c9zye_JyKnMh_mEhdC6uXTIjhVgZetymcMhHQO-FZwP2PDzjQzOjDQTmvGga_se25vdJDN3zvUXAeraBCrw7evaUHTNILgvBFJLEDMwYIggEeyFwBYgwJadOqgaeVAPgnRLUUnZGQmzKCtoihzUR7BSLSBZJI2T9QNs8ZIoQfS4PEI2dpQa8oYGYAz-CaGGjBTtHYjF02wPeAMcZvJQY77ZO3utlW08lLY7J3P2jv-j7C35b8ZEQfbBlJKFFxnaAbGKaS-GLoSk_YNgLYqu7sEk4EMXiCaQDgS2S4XcFcQUwqcKQl2BTcGvi2gzsWfAnZ9tJDaUPhWDzqPhtmsvW1vb3V1oHpgnNF2iconSOng9IZsW_FA4H4iJkvN0qK4IaZkGY6-DB3Q_ASPsp7PRkEE7jWfjBY0nkdkvXd7I37_kP6J4iW7X8NkW77mldYV46GL_M-pTB3sa71qNu4me23bdxdoBuwXzcMF64pcbNl8NQ4v0fCUfXca7f5DpenPTjPJZlHouOHKvdzWlXupYe-7bcEb5AxMHn1whCQAA';
console.log(decrypt(l))
我试着运行它,它给了我这个错误:
RangeError [ERR_OUT_OF_RANGE]: The value of "options.windowBits" is out of range. It must be >= 8 and <= 15. Received 47
当我删除windowBits
选项时,它给了我这个错误:
Error: incorrect header check
如何解决这个问题?
1条答案
按热度按时间8fq7wneg1#
gunzipSync
已经需要gzip输入。它不需要| 32
。只需取消选项。但是,您作为示例提供的数据已损坏。它开始时是有效的gzip流,但包含无效的deflate数据。