nklein logo

Javascript implementation of Blowfish Encryption:

This a Javascript implementation of Bruce Schneier's Blowfish encryption algorithm.

Feedback of any sort is welcome to blowfish@nklein.com


There are a few Javascript files available here:


This file implements the Blowfish algorithm. The _encrypt_block() and _decrypt_block() methods take an array of two 32-bit numbers and replace those numbers with the encrypted/decrypted values. The encrypt_block() method takes an array of eight 8-bit integers which are assembled into the two 32-bit numbers to be fed to the _encrypt_block() method and places the result back into the input array.

var password = [ 0x33, 0x55, 0x77, 0x12, 0x33 ]; var bf = new Blowfish(); bf.init( password ); var vals = [ 0x01234567, 0xFEDCBA98 ]; bf._encrypt_block( vals ); bf._decrypt_block( vals ); var vec = [ 0x20, 0x6E, 0x6B, 0x6C, 0x65, 0x69, 0x6E, 0x20 ]; bf.encrypt_block( vec );

This file has one function StandardBlowfishTests() which runs the Blowfish implementation against the 58 standard test vectors. Note: on slower machines, this can take quite some time. If your browser asks you to continue or stop the script, you should continue.


This file implements a class CFBStream which takes any block cipher that supports an encode_block() method and a BLOCKSIZE property like the Blowfish algorithm above does and turns it into a Cipher Feedback Stream.

var password = [ 0x33, 0x55, 0x77, 0x12, 0x33 ]; var bf = new Blowfish(); bf.init( password ); var password = [ 0x33, 0x55, 0x77, 0x12, 0x33 ]; var iv = [ 0x11, 0x22, 0xA1, 0xB2, 0xC7, 0x99, 0x6E, 0x20 ]; var ecb = new CFBStream( bf, iv ); var plaintext = "The magic words are squeamish ossifrage."; var ciphertext = ecb.encrypt( plaintext ); var dcb = new CFBStream( bf, iv ); var decoded = dcb.decrypt( plaintext );

Note: the CFBStream is stateful. So, to decrypt, you need to start it with the same initial vector iv that you started the encrypting with.

(copyright 2008) (gpg keys)

Valid XHTML 1.0! Valid CSS!