To use Blowish in PHP you must include two files. The first is the main include file, the second is the constants file. You must then instantiate two objects. The first is the constants object. The second is the Blowfish object, which requires the first. You may forget the constants object after this step.
Once you have created the Blowfish object, you simply need to set the key (if you are using LiteFish then the key is already set) and then use the object to encrypt or decrypt data.
The first thing you need is the main blowfish include file:
require_once "blowfish.inc";
Next, if you are using standard Blowfish, you must include the standard constants file:
require_once "blowfish_constants.inc";
If you are using LiteFish then you must include the LiteFish constants file instead:
require_once "litefish_constants.inc";
Before you can create the Blowfish object, you must first instantiate the defaults object:
$blowfish_defaults = new blowfish_defaults_object();
If you are using litefish then the defaults object has a slightly different name (this is just in case you want to use both in the same script):
$blowfish_defaults = new litefish_defaults_object();
To use the cipher, you must instatiate the Blowfish object. You must pass it an instance of the defaults object you just created:
$blowfish = new blowfish_cipher( $blowfish_defaults );
If you are using LiteFish then you should use the litefish_cipher object in exactly the same way:
$blowfish = new litefish_cipher( $blowfish_defaults );
If you are using LiteFish then the key is already set. You cannot use another key with Litefish.
If you try to set the key with Litefish, you will end up with a completely different, combined key. It is impossible to predict what the new key will be, so you could lose any data that you encrypt using it.
As long as you use the litefish_cipher object, you cannot call setKey. However, you must be careful not to accidentally use the blowfish_cipher object with litefish constants.
You set the key with the setKey function of the Blowfish object:
$blowfish->setKey( $key );
You can encrypt and decrypt strings simply by calling the encrypt and decrypt functions of the Blowfish object:
Note: The Blowfish object presently only supports the ECB chaining mode.
$encrypted = $blowfish->encrypt( $plainText ); $decrypted = $blowfish->decrypt( $cipherText );
Here is a complete example that takes a base-64 encoded string sent with POST, decrypts it, and displays the result:
// Includes require_once "blowfish.inc"; require_once "blowfish_constants.inc"; // Initialise $blowfish_defaults = new blowfish_defaults_object(); $blowfish = new blowfish_cipher( $blowfish_defaults ); // Set Key $blowfish->setKey( "SecretKey123" ); // Decrypt $encoded = @$_POST[ 'data' ]; $cipherText = base64_decode( $encoded ); $decrypted = $blowfish->decrypt( $cipherText ); // Display result echo $decrypted;
Here is the same example using LiteFish:
// Includes require_once "blowfish.inc"; require_once "litefish_constants.inc"; // Initialise $blowfish_defaults = new litefish_defaults_object(); $blowfish = new litefish_cipher( $blowfish_defaults ); // Decrypt $encoded = @$_POST[ 'data' ]; $cipherText = base64_decode( $encoded ); $decrypted = $blowfish->decrypt( $cipherText ); // Display result echo $decrypted;
LiteFish is a quicker version of Blowfish designed for communicating with Shockwave or some other client application. It is useful where there is a fixed secret key stored in the client application, so it never changes. LiteFish takes out the step of initialising the key, which takes quite a lot of CPU time.
LiteFish is appropriate whenever you only need to use a single key. If your key will change or you need to deal with several keys then you should stick with standard Blowfish.
To use LiteFish, you must first initialise it, which is done using the provided key generator utility.
If you are viewing this on a webserver running PHP, simply click on the link and the generator utility will write the "litefish_constants.inc" file. You must ensure that PHP has the appropriate write permissions to create this file. On a Unix server, if unsure, simply delete the file and it will be created automatically.
To use a different key with LiteFish, simply edit the "litefish_key.inc" file and run the generator utility again.
On a live webserver, you do not need to upload the "litefish_key.inc" file. Remember, if you are generating your key constants on a development system (recommended) then whenever you change your key you must upload the new "litefish_constants.inc" file to the live webserver.
Copyright © 2002 Killing Moon Ltd.