Decrypting mcrypt encoded text with node.js

被刻印的时光 ゝ 提交于 2019-12-05 12:27:09

https://github.com/tugrul/node-mcrypt

Encrypt:

var mcrypt = require('mcrypt');

var bfEcb = new mcrypt.MCrypt('blowfish', 'cfb');
var iv = bfEcb.generateIv();

bfEcb.open('somekey', iv);

var cipherText = bfEcb.encrypt('sometext');

console.log(Buffer.concat([iv, cipherText]).toString('base64'));

Decrypt:

var mcrypt = require('mcrypt');
var bfEcb = new mcrypt.MCrypt('blowfish', 'cfb');

var ivAndCiphertext = new Buffer('AyvfjTyg24Y9fVCdjzRPEw==', 'base64');

var ivSize = bfEcb.getIvSize();
var iv = new Buffer(ivSize);
var cipherText = new Buffer(ivAndCiphertext.length - ivSize);

ivAndCiphertext.copy(iv, 0, 0, ivSize);
ivAndCiphertext.copy(cipherText, 0, ivSize);

bfEcb.open('somekey', iv);

console.log(bfEcb.decrypt(cipherText).toString());

I'm not sure if there are any other errors, but when using an IV you should use the createDecipheriv method:

http://nodejs.org/docs/latest/api/crypto.html#crypto.createDecipheriv

You are using ivAndCiphertext = "base64-encoded-ciphertext" followed directly by ivAndCiphertext = new Buffer(ivAndCiphertext, 'base64');. Your variable will point to the new buffer, so you get the result of decrypting the new buffer.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!