Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encrypt string with Blowfish in NodeJS

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.

like image 901
Hassan Ila Avatar asked Jan 05 '23 21:01

Hassan Ila


1 Answers

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==
like image 85
Hassan Ila Avatar answered Jan 07 '23 10:01

Hassan Ila