我试图用管道加密/解密流中的文件。然而,在解密时,加密是有效的,但我得到了错误:
错误:不支持状态或无法对数据进行身份验证
在Decipheriv._flush (节点:内部/加密/密码:160:29),在Decipheriv.final as _final at callFinal (节点:内部/流/可写:694:27),在预完成(节点:内部/流/可写:719:7)在finishMaybe (节点:内部/流/可写:729:5),在Decipheriv.Writable.end (节点:内部/流/可写:631:5),在IOStream.onend (节点:内部/流/可读性:693:10),在Object.onceWrapper (节点:事件:509:28)IOStream.emit (节点:事件:402:35)在endReadableNT (节点:内部/流/可读性:1343:12)发出关于Decipheriv.onerror的“错误”事件(节点:内部/流/可读性:773:14),在Decipheriv.emit (节点:事件:390:28),在emitErrorNT (节点:内部/流/销毁:157:8),在emitErrorCloseNT (节点:内部/流/销毁:122:3)在processTicksAndRejections (节点:内部/流程/任务队列:83:21)
代码(最后一行产生错误):
const crypto = require('crypto');
const fs = require('fs');
const secret = crypto.randomBytes(32);
const iv = crypto.randomBytes(16);
const cipher = crypto.createCipheriv('aes-256-gcm', secret, iv);
const decipher = crypto.createDecipheriv('aes-256-gcm', secret, iv);
fs.createReadStream('data.txt').pipe(cipher).
pipe(fs.createWriteStream('encrypted.txt'));
fs.createReadStream('encrypted.txt').pipe(decipher).
pipe(fs.createWriteStream('decrypted.txt'));
发布于 2021-12-14 19:44:38
它的工作方式是这样的,在开始编写/加密之前等待它完成读取/加密。
const crypto = require('crypto');
const fs = require('fs');
const secret = crypto.randomBytes(32);
const iv = crypto.randomBytes(16);
const cipher = crypto.createCipheriv('aes-256-gcm', secret, iv);
const decipher = crypto.createDecipheriv('aes-256-gcm', secret, iv);
fs.createReadStream('data.txt').pipe(cipher)
.pipe(fs.createWriteStream('encrypted.txt'))
.on('end', () => {
fs.createReadStream('encrypted.txt')
.pipe(decipher)
.pipe(fs.createWriteStream('decrypted.txt'));
});
https://stackoverflow.com/questions/70297766
复制相似问题