JavaScript Blowfish Encryption Library

1.0.4 · maintenance · verified Sun Apr 19

This library implements the Blowfish symmetric-key block cipher for JavaScript, supporting both browser and Node.js environments. Currently at version 1.0.4, the package provides basic encryption and decryption capabilities, including Electronic Codebook (ECB) and Cipher Block Chaining (CBC) modes. A key feature is its robust handling of UTF-8 strings and automatic zero-padding to ensure input data conforms to Blowfish's 8-byte block length, with a utility to `trimZeroes` after decryption for text. It also offers built-in base64 encoding/decoding for handling the binary output of encryption. While Blowfish was designed for speed and flexibility with variable key lengths (32-448 bits) in 1993, its 64-bit block size is now considered a security weakness due to susceptibility to 'Sweet32' birthday attacks, especially for large data volumes (over 4GB). It's generally recommended for legacy systems or specific constrained environments rather than new applications, where modern ciphers like AES or Twofish are preferred. The library itself appears to be in a maintenance or inactive state, with no recent updates since 2018.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates basic Blowfish encryption and decryption using a secret key. It covers handling the binary output of encryption, including trimming zero padding for text data and using base64 encoding for convenient representation.

import { Blowfish } from 'javascript-blowfish';

const encryptionKey = process.env.BLOWFISH_KEY ?? 'my-super-secret-key-1234567890';
const message = 'This is a secret message to be encrypted.';

// Initialize Blowfish in ECB mode (default)
const bf = new Blowfish(encryptionKey);

// Encrypt the message
const encrypted = bf.encrypt(message);
console.log('Encrypted (binary string):', encrypted);

// Decrypt the message
let decrypted = bf.decrypt(encrypted);

// Trim zero padding for string/text information
decrypted = bf.trimZeroes(decrypted);

console.log('Decrypted message:', decrypted);

// Example with Base64 encoding for easier handling of binary output
const bfBase64 = new Blowfish(encryptionKey);
const encryptedBase64 = bfBase64.base64Encode(bfBase64.encrypt(message));
console.log('Encrypted (Base64):', encryptedBase64);

let decryptedBase64 = bfBase64.decrypt(bfBase64.base64Decode(encryptedBase64));
decryptedBase64 = bfBase64.trimZeroes(decryptedBase64);
console.log('Decrypted (from Base64):', decryptedBase64);

view raw JSON →