在NodeJS中使用Blowfish加密字符串

时间:2016-12-20 18:09:01

标签: javascript node.js encryption base64 blowfish

我需要加密一个字符串,但我几乎得到了我想要的输出,我在网上读到它与填充有关,而iv_vector在最后完成剩余的8个字节与txtToEncrypt的长度相同。

我正在使用此库https://github.com/agorlov/javascript-blowfish

// function in Java that I need
// javax.crypto.Cipher.getInstance("Blowfish/CBC/NoPadding").doFinal("spamshog")


var iv_vector = "2278dc9wf_178703";
var txtToEncrypt = "spamshog";
var bf = new Blowfish("spamshog", "cbc");

var encrypted = bf.encrypt(txtToEncrypt, iv_vector);

console.log(bf.base64Encode(encrypted));

Actual output: /z9/n0FzBJQ=
 What I need: /z9/n0FzBJRGS6nPXso5TQ==
  

如果有人有任何线索请告诉我。我整天都在谷歌搜索过。

1 个答案:

答案 0 :(得分:3)

最后,这里是如何使用Blowfish加密NodeJS中的字符串

// Module crypto already included in NodeJS
var crypto = require('crypto');

var iv = "spamshog";
var key = "spamshog";
var text = "2278dc9wf_178703";
var decipher = crypto.createCipheriv('bf-cbc', key, iv);
decipher.setAutoPadding(false);
var encrypted = decipher.update(text, 'utf-8', "base64");
encrypted += decipher.final('base64');

console.log(encrypted);  

Returns: /z9/n0FzBJRGS6nPXso5TQ==