Encrypt string with Blowfish in NodeJS

9.8k views Asked by At

I need to encrypt a string but I almost get the output I desire, I read online that it has something to do with padding and iv_vector at the end to complete for the remaining 8 bytes to be same length as txtToEncrypt.

I'm using this library 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==

If anyone has any clue please let me know. I searched all over Google all day.

1

There are 1 answers

1
H I On BEST ANSWER

Finally, here is how to encrypt a string in NodeJS with Blowfish

// 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==