Source for file Blowfish.php
Documentation is available at Blowfish.php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
* Crypt_Blowfish allows for encryption and decryption on the fly using
* the Blowfish algorithm. Crypt_Blowfish does not require the MCrypt
* PHP extension, but uses it if available, otherwise it uses only PHP.
* Crypt_Blowfish supports encryption/decryption with or without a secret key.
* @package Crypt_Blowfish
* @author Matthew Fonda <mfonda@php.net>
* @copyright 2005-2008 Matthew Fonda
* @license http://www.opensource.net/licenses/bsd-license.php New BSD
* @version CVS: $Id: Blowfish.php,v 1.86 2008/08/30 21:53:50 jausions Exp $
* @link http://pear.php.net/package/Crypt_Blowfish
* Required PEAR package(s)
* Engine choice constants
* To let the Crypt_Blowfish package decide which engine to use
define('CRYPT_BLOWFISH_AUTO', 1 );
* To use the MCrypt PHP extension.
define('CRYPT_BLOWFISH_MCRYPT', 2 );
* To use the PHP-only engine.
define('CRYPT_BLOWFISH_PHP', 3 );
* Example using the factory method in CBC mode
* $bf =& Crypt_Blowfish::factory('cbc');
* if (PEAR::isError($bf)) {
* echo $bf->getMessage();
* $key = 'My secret key';
* $bf->setKey($key, $iv);
* $encrypted = $bf->encrypt('this is some example plain text');
* $bf->setKey($key, $iv);
* $plaintext = $bf->decrypt($encrypted);
* if (PEAR::isError($plaintext)) {
* echo $plaintext->getMessage();
* // Encrypted text is padded prior to encryption
* // so you may need to trim the decrypted result.
* echo 'plain text: ' . trim($plaintext);
* To disable using the mcrypt library, define the CRYPT_BLOWFISH_NOMCRYPT
* constant. This is useful for instance on Windows platform with a buggy
* mdecrypt_generic() function.
* define('CRYPT_BLOWFISH_NOMCRYPT', true);
* @package Crypt_Blowfish
* @author Matthew Fonda <mfonda@php.net>
* @author Philippe Jausions <jausions@php.net>
* @copyright 2005-2008 Matthew Fonda
* @license http://www.opensource.net/licenses/bsd-license.php New BSD
* @link http://pear.php.net/package/Crypt_Blowfish
* Implementation-specific Crypt_Blowfish object
* Crypt_Blowfish Constructor
* Initializes the Crypt_Blowfish object (in EBC mode), and sets
* @deprecated Since 1.1.0
* @see Crypt_Blowfish::factory()
if (!PEAR ::isError ($this->_crypt)) {
$this->_crypt->setKey ($key);
* Crypt_Blowfish object factory
* This is the recommended method to create a Crypt_Blowfish instance.
* When using CRYPT_BLOWFISH_AUTO, you can force the package to ignore
* the MCrypt extension, by defining CRYPT_BLOWFISH_NOMCRYPT.
* @param string $mode operating mode 'ecb' or 'cbc' (case insensitive)
* @param string $iv initialization vector (must be provided for CBC mode)
* @param integer $engine one of CRYPT_BLOWFISH_AUTO, CRYPT_BLOWFISH_PHP
* or CRYPT_BLOWFISH_MCRYPT
* @return object Crypt_Blowfish object or PEAR_Error object on error
function &factory($mode = 'ecb', $key = null , $iv = null , $engine = CRYPT_BLOWFISH_AUTO )
if (!defined('CRYPT_BLOWFISH_NOMCRYPT')
if (!PEAR ::loadExtension ('mcrypt')) {
return PEAR ::raiseError ('MCrypt extension is not available.');
$class = 'Crypt_Blowfish_' . $mode;
include_once 'Crypt/Blowfish/' . $mode . '.php';
$crypt = new $class(null );
include_once 'Crypt/Blowfish/MCrypt.php';
$result = $crypt->setKey ($key, $iv);
if (PEAR ::isError ($result)) {
* Returns the algorithm's block size
* Returns the algorithm's IV size
* Returns the algorithm's maximum key size
* Deprecated isReady method
* Deprecated init method - init is now a private
* method and has been replaced with _init
return $this->_crypt->init ();
* Value is padded with NUL characters prior to encryption. You may
* need to trim or cast the type when you decrypt.
* @param string $plainText the string of characters/bytes to encrypt
* @return string|PEAR_ErrorReturns cipher text on success, PEAR_Error on failure
return $this->_crypt->encrypt ($plainText);
* Decrypts an encrypted string
* The value was padded with NUL characters when encrypted. You may
* need to trim the result or cast its type.
* @param string $cipherText the binary string to decrypt
* @return string|PEAR_ErrorReturns plain text on success, PEAR_Error on failure
return $this->_crypt->decrypt ($cipherText);
* The key must be non-zero, and less than or equal to
* 56 characters (bytes) in length.
* If you are making use of the PHP MCrypt extension, you must call this
* method before each encrypt() and decrypt() call.
* @return boolean|PEAR_Error Returns TRUE on success, PEAR_Error on failure
return $this->_crypt->setKey ($key);
Documentation generated on Sat, 30 Aug 2008 18:00:05 -0400 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.
|