Source for file CBC.php
Documentation is available at CBC.php
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2008 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.02 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Colin Viebrock <colin@viebrock.ca> |
// +----------------------------------------------------------------------+
// $Id: CBC.php,v 1.7 2008/10/01 21:23:11 cmv Exp $
* Class to emulate Perl's Crypt::CBC module
* Blowfish support that is compatable with Perl requires libmcrypt >= 2.4.9.
* If you are using libmcrypt <= 2.4.8, Blowfish encryption will work,
* but your data will not be readable by Perl scripts. It will work
* "internally" .. i.e. this class will be able to encode/decode the data.
* Blowfish support that is compatable with PHP applications using
* libmcrypt <= 2.4.8 requies you to use 'BLOWFISH-COMPAT' when
* specifying the cipher. Check the libmcrypt docs when in doubt.
* This class no longer works with libmcrypt 2.2.x versions.
* NOTE: the cipher names in this class may change depending on how
* the author of libcrypt decides to name things internally.
* @version $Revision: 1.7 $
* @author Colin Viebrock <colin@easydns.com>
'BLOWFISH' => MCRYPT_BLOWFISH ,
'BLOWFISH-COMPAT' => MCRYPT_BLOWFISH_COMPAT ,
* crypt resource, for 2.4.x
* crypt deinit function, for backwards compatability
* source type of the initialization vector for creation
* possible types are MCRYPT_RAND or MCRYPT_DEV_URANDOM or MCRYPT_DEV_RANDOM
* $key is the key to use for encryption. $cipher can be DES, BLOWFISH or
* @param $key encryption key
* @param $cipher which algorithm to use, defaults to DES
* @return $return either a PEAR error or true
return $this->raiseError ('mcrypt module is not compiled into PHP', null ,
PEAR_ERROR_DIE , null , 'compile PHP using "--with-mcrypt"' );
return $this->raiseError ('libmcrypt version insufficient', null ,
PEAR_ERROR_DIE , null , 'this class requires libmcrypt >= 2.4.x, preferably >= 2.5.5' );
return $this->raiseError ('PHP version insufficient', null ,
PEAR_ERROR_DIE , null , 'this class requires PHP >= 4.0.2, preferably >= 4.1.1' );
return $this->raiseError ('no key specified');
return $this->raiseError ('unknown cipher "'. $cipher. '"' );
$this->TD = mcrypt_module_open ($this->cipher, '', 'ecb', '');
$this->blocksize = mcrypt_enc_get_block_size ($this->TD);
$this->keysize = mcrypt_enc_get_key_size ($this->TD);
/* mangle key with MD5 */
$this->keyhash = $this->_md5perl ($key);
@mcrypt_module_close ($this->TD);
* @param $clear plaintext
* @return $crypt encrypted text, or PEAR error
$this->last_clear = $clear;
/* new IV for each message */
/* create the message header */
if (mcrypt_generic_init ($this->TD, $this->key, $iv) < 0 ) {
return $this->raiseError ('mcrypt_generic_init failed' );
$cblock = mcrypt_generic ($this->TD, $iv^ $block );
$this->last_crypt = $crypt;
* @param $crypt encrypted text
* @return $clear plaintext, or PEAR error
$this->last_crypt = $crypt;
/* get the IV from the message header */
$header = substr($crypt, 0 , $iv_offset);
return $this->raiseError ('no initialization vector');
/* decrypt the message */
if (mcrypt_generic_init ($this->TD, $this->key, $iv) < 0 ) {
return $this->raiseError ('mcrypt_generic_init failed' );
$block = $iv ^ mdecrypt_generic ($this->TD, $cblock);
/* remove the padding from the end of the cleartext */
$clear = substr($clear, 0 , - $padsize);
$this->last_clear = $clear;
* Emulate Perl's MD5 function, which returns binary data
* @param $string string to MD5
* @return $hash binary hash
function _md5perl ($string)
Documentation generated on Mon, 11 Mar 2019 15:25:48 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|