nodejs中的三重DES与通过openssl相比
以下示例中xxd -p
拳头在hex的末尾添加了“0a”。 不知道为什么,这就是为什么你注意到我在opensslencryption中使用第一个参数$1
,我已经手动生成了hex,并插入它。
#!/bin/bash KEY=`echo $1 |xxd -p`; openssl enc -e -des-ede -nosalt -K $1 -iv "0000000000000000" -in $2 -out $3;
这样称呼:
sh encrypt.sh 583645585458304c4f39524756514456 settings.conf settings.enc
在node.js中,但我使用以下encryption:
const key = Buffer.from('X6EXTX0LO9RGVQDV'); module.exports = (file, callback) => { const iv = new Buffer(0), cipher = require('crypto').createCipheriv('des-ede', key, iv); callback(null, cipher.update(file, 'utf8', 'binary')); }
…并触发这样的:
require('fs').readFile(__dirname + '/settings.enc', (error, enc) => require('fs').readFile(__dirname + '/settings.conf', (error, conf) => require('./file-encrypt')(conf, (error, enc2) => { enc2 = Buffer.from(enc2, 'binary'); console.log('compare: ', Buffer.compare(enc, enc2)); })));
但输出从来不是“0”(意思是说,它们从来不是一样的 – 用console.log('compare: ', Buffer.compare(enc2, enc2))
来testing)
为什么这些不一样?
在完成提供所有纯文本数据之后,您会错过对cipher.final()
的调用以获取任何剩余的字节。 试试这个:
const crypto = require('crypto'); const key = Buffer.from('X6EXTX0LO9RGVQDV'); module.exports = (file, callback) => { const iv = Buffer.alloc(0); const cipher = crypto.createCipheriv('des-ede', key, iv); callback(null, Buffer.concat([ cipher.update(file), cipher.final() ]); }
并使用像:
require('fs').readFile(__dirname + '/settings.enc', (error, enc) => require('fs').readFile(__dirname + '/settings.conf', (error, conf) => require('./file-encrypt')(conf, (error, enc2) => { console.log('compare: ', Buffer.compare(enc, enc2)); })));
而且,由于IV不被des-ede
-iv "0000000000000000"
,因此可以省略openssl
命令行中的-iv "0000000000000000"
,并避免向控制台输出警告。
- mongodb,node.js和encryption的数据
- Node.js / javascript在PHP中像mcrypt_ecb一样encryptionAES-128
- Node.js Crypto类使用更新的版本返回不同的结果
- NodeJSencryption:重新使用密码对象来提高性能
- encryption通道的重新encryption会在Node.js中返回不同的encryption结果
- mongoose如何处理密码编码很好?
- node.jsencryption模块不能encryption16个以上的字符
- nodejs w / crypto中的SALT和HASH密码
- NodeJS – SHA256密码encryption