我尝试加密文本文件中的每一行,而不是文本文件本身。这是我用来加密单行文本的代码。
crypto.pbkdf2(password, salt, iteration, keylen, digest, (error, derivedKey) => {
const iv = Buffer.from('myiv', 'hex');
const cipher = crypto.createCipheriv(algorithm, derivedKey, iv);
let encryptThis = `Encrypt me`;
let encrypted = '';
cipher.on('readable', () => {
let chunk;
while (null !== (chunk = cipher.read())) {
encrypted += chunk.toString('base64');
}
});
cipher.on('end', () => {
console.log(`Example string: ${encryptThis}`);
});
cipher.write(encryptThis);
cipher.end();
});
我知道我也可以使用cipher.update(text)
和cipher.final()
进行加密,我也尝试过这种方法,但没有成功。问题是如何逐行读取文件并对每一行进行加密。我尝试过这两种方法,但总是导致只有一行被加密或出现错误。我希望能够通过流转换来实现这一点,比如。
readStream
.pipe(encryptLine)
.pipe(writeStream)
.on('finish', err => {
if (err) console.log(err);
});
https://stackoverflow.com/questions/55789017
复制相似问题