gpt4 book ai didi

node.js - 为什么表示加密后解密文件的流的行为与表示原始文件的流不同?

转载 作者:太空宇宙 更新时间:2023-11-03 22:10:47 27 4
gpt4 key购买 nike

我创建了一个传输视频文件的 HTTP 服务器。

http.createServer((req, res) => {
const file = 'example.mp4';
const size = fs.statSync(file).size;
res.writeHead(200, { 'Content-Length': size, 'Content-Type': 'video/mp4' });
fs.createReadStream(file).pipe(res);
}).listen(1911, '127.0.0.1');

我在浏览器或视频播放器中连接到它以验证它是否有效。确实如此。

我加密一个文件:

fs.createReadStream('example.mp4')
.pipe(crypto.createCipher('aes-256-ctr', 'x'))
.pipe(fs.createWriteStream('encrypted_file'));

我将其解密并回放以验证其是否有效。确实如此。

然而,按照以下方式将解密和流式传输结合起来会失败。

const decrypt = crypto.createDecipher('aes-256-ctr', 'x');
http.createServer((req, res) => {
const file = 'encrypted_file';
const size = fs.statSync(file).size;
res.writeHead(200, { 'Content-Length': size, 'Content-Type': 'video/mp4' });
fs.createReadStream(file).pipe(decrypt).pipe(res);
}).listen(1911, '127.0.0.1');

原始文件和加密文件的字节大小相同,并且原始文件和加密后解密的文件都具有相同的 SHA-256 哈希值。鉴于此,我希望 fs.createReadStream(original) 和 fs.createReadStream(encrypted).pipe(decrypt) 的行为相同 - 但事实并非如此。不会向用户发送任何视频数据,但也不会向他们显示错误,并且错误事件永远不会在 http.Server 实例上触发。

我错过了什么?

最佳答案

您的解密代码看起来不错,并且可以在我的短文件变体中正常工作 http://techslides.com/demos/sample-videos/small.mp4在我的测试中(基于 https://gist.github.com/paolorossi/1993068 的代码基于 Video streaming with HTML 5 via node.js ):

var http = require('http'),
fs = require('fs'),
util = require('util'),
crypto = require('crypto');

http.createServer(function (req, res) {
var path = 'encrypted';
var stat = fs.statSync(path);
var total = stat.size;
const decrypt = crypto.createDecipher('aes-256-ctr', 'x');
console.log('ALL: ' + total);
res.writeHead(200, { 'Content-Length': total, 'Content-Type': 'video/mp4' });
fs.createReadStream(path).pipe(decrypt).pipe(res);
}).listen(1912, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1912/');

您的文件可能更大,因此打印到客户端的控制台请求可能会很有用。在我的测试中,未加密和加密的服务器都收到两个请求并发送了 383631 字节。

我的版本和你的版本之间的区别是:这是我的第一个 Node.js 服务器(也是第一个 js 程序之一),但它不是你的第一个。我没有将 decrypt 声明为全局常量,而是声明为本地常量。通过调试打印,我看到来自浏览器的两个请求;第二个尝试修改您的变体中的全局 const decrypt 并出现错误:

Server running at http://127.0.0.1:1912/
ALL: 383631
ALL: 383631
events.js:141
throw er; // Unhandled 'error' event
^

Error: write after end
at writeAfterEnd (_stream_writable.js:159:12)
at Decipher.Writable.write (_stream_writable.js:204:5)
at ReadStream.ondata (_stream_readable.js:528:20)
at emitOne (events.js:77:13)
at ReadStream.emit (events.js:169:7)
at readableAddChunk (_stream_readable.js:146:16)
at ReadStream.Readable.push (_stream_readable.js:110:10)
at onread (fs.js:1743:12)
at FSReqWrap.wrapper [as oncomplete] (fs.js:576:17)

因此解密移至服务器代码内。您的错误是重用无法重用的 Decipher 对象(https://nodejs.org/api/crypto.html#crypto_class_decipher“一旦调用了 decipher.final() 方法,Decipher 对象就不能再用于解密数据。”);并且在第二次调用时,解密器可能会尝试使用错误(非零)计数器值来解码文件。

关于node.js - 为什么表示加密后解密文件的流的行为与表示原始文件的流不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42512794/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com