Node.js pipe decipher file Error: Unsupported state or unable to authenticate data
Node.js pipe decipher file Error: Unsupported state or unable to authenticate data
我正在尝试 encrypt/decrypt 使用管道流式传输文件。加密有效,但是在解密时出现错误:
Error: Unsupported state or unable to authenticate data
at Decipheriv._flush (node:internal/crypto/cipher:160:29)
at Decipheriv.final [as _final] (node:internal/streams/transform:112:25)
at callFinal (node:internal/streams/writable:694:27)
at prefinish (node:internal/streams/writable:719:7)
at finishMaybe (node:internal/streams/writable:729:5)
at Decipheriv.Writable.end (node:internal/streams/writable:631:5)
at IOStream.onend (node:internal/streams/readable:693:10)
at Object.onceWrapper (node:events:509:28)
at IOStream.emit (node:events:402:35)
at endReadableNT (node:internal/streams/readable:1343:12)
Emitted 'error' event on Decipheriv instance at:
at Decipheriv.onerror (node:internal/streams/readable:773:14)
at Decipheriv.emit (node:events:390:28)
at emitErrorNT (node:internal/streams/destroy:157:8)
at emitErrorCloseNT (node:internal/streams/destroy:122:3)
at processTicksAndRejections (node:internal/process/task_queues: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'));
它是这样工作的,等待它完成 reading/encryping 然后再开始 writing/encrypting。
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'));
});
我正在尝试 encrypt/decrypt 使用管道流式传输文件。加密有效,但是在解密时出现错误:
Error: Unsupported state or unable to authenticate data
at Decipheriv._flush (node:internal/crypto/cipher:160:29) at Decipheriv.final [as _final] (node:internal/streams/transform:112:25) at callFinal (node:internal/streams/writable:694:27) at prefinish (node:internal/streams/writable:719:7) at finishMaybe (node:internal/streams/writable:729:5) at Decipheriv.Writable.end (node:internal/streams/writable:631:5) at IOStream.onend (node:internal/streams/readable:693:10) at Object.onceWrapper (node:events:509:28) at IOStream.emit (node:events:402:35) at endReadableNT (node:internal/streams/readable:1343:12) Emitted 'error' event on Decipheriv instance at: at Decipheriv.onerror (node:internal/streams/readable:773:14) at Decipheriv.emit (node:events:390:28) at emitErrorNT (node:internal/streams/destroy:157:8) at emitErrorCloseNT (node:internal/streams/destroy:122:3) at processTicksAndRejections (node:internal/process/task_queues: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'));
它是这样工作的,等待它完成 reading/encryping 然后再开始 writing/encrypting。
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'));
});