【发布时间】:2018-12-25 09:57:51
【问题描述】:
我有一个用例,我们使用简单的 NodeJS 应用程序将日志数据流式传输到文件。我们希望能够在流式传输数据时对其进行加密,然后根据需要使用 OpenSSL 或类似工具对其进行解密。
我们所做的基本上如下:
var crypto = require('crypto'),
algorithm = 'aes256',
password = 'password';
var fs = require('fs');
var Readable = require('stream').Readable
var r = new Readable
r.push('This is a test')
r.push(null)
var encrypt = crypto.createCipher(algorithm, password);
var decrypt = crypto.createDecipher(algorithm, password)
var w = fs.createWriteStream('file.out');
//Write encrypted stream to file. Decrypt with openssl fails with 'bad magic number'
r.pipe(encrypt).pipe(w)
//Decrypt using cipher library. Decrypted text displays as expected
//r.pipe(encrypt).pipe(decrypt).pipe(w)
假设我们只是在读取数据时加密数据,我假设我们可以使用开放的 OpenSSL 对其进行解密,例如
openssl enc -d -aes256 -in file.out -out file.out.decrypted
但这只是给我错误
Bad magic number
任何帮助将不胜感激。
【问题讨论】:
标签: node.js openssl cryptography cryptojs