/usr/share/php/Crypt/CBC.php is in php-crypt-cbc 1.0.1-1build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 | <?php
/**
* 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.
*
*
* @category Encryption
* @package Crypt_CBC
* @author Colin Viebrock <colin@viebrock.com>
* @copyright 2002-2012 Colin Viebrock
* @version $Revision: 322853 $
* @license http://www.opensource.org/licenses/bsd-license.php BSD
* @link http://pear.php.net/package/Crypt_CBC
*/
require_once 'PEAR.php';
class Crypt_CBC extends PEAR {
/**
* supported procedures
* @var array
*/
var $known_ciphers = array (
'DES' => MCRYPT_DES,
'BLOWFISH' => MCRYPT_BLOWFISH,
'BLOWFISH-COMPAT' => MCRYPT_BLOWFISH_COMPAT,
);
/**
* used cipher
* @var string
*/
var $cipher;
/**
* crypt resource, for 2.4.x
* @var string
*/
var $TD;
/**
* crypt deinit function, for backwards compatability
* @var string
*/
var $deinit_function;
/**
* blocksize of cipher
* @var string
*/
var $blocksize;
/**
* keysize of cipher
* @var int
*/
var $keysize;
/**
* mangled key
* @var string
*/
var $keyhash;
/**
* source type of the initialization vector for creation
* possible types are MCRYPT_RAND or MCRYPT_DEV_URANDOM or MCRYPT_DEV_RANDOM
* @var int
*/
var $rand_source = MCRYPT_RAND;
/**
* header
* @var string
*/
var $header_spec = 'RandomIV';
/**
* debugging
* @var string
*/
var $_last_clear;
/**
* debugging
* @var string
*/
var $_last_crypt;
/**
* Constructor
* $key is the key to use for encryption. $cipher can be DES, BLOWFISH or
* BLOWFISH-COMPAT
*
* @param $key encryption key
* @param $cipher which algorithm to use, defaults to DES
*
* @return $return either a PEAR error or true
*
* @access public
*
*/
function Crypt_CBC ($key, $cipher='DES')
{
if (!extension_loaded('mcrypt')) {
return $this->raiseError('mcrypt module is not compiled into PHP', null,
PEAR_ERROR_DIE, null, 'compile PHP using "--with-mcrypt"' );
}
if (!function_exists('mcrypt_module_open')) {
return $this->raiseError('libmcrypt version insufficient', null,
PEAR_ERROR_DIE, null, 'this class requires libmcrypt >= 2.4.x, preferably >= 2.5.5' );
}
if (function_exists('mcrypt_generic_deinit')) {
$this->deinit_function = 'mcrypt_generic_deinit';
} else if (function_exists('mcrypt_generic_end')) {
$this->deinit_function = 'mcrypt_generic_end';
} else {
return $this->raiseError('PHP version insufficient', null,
PEAR_ERROR_DIE, null, 'this class requires PHP >= 4.0.2, preferably >= 4.1.1' );
}
/* initialize */
$this->header_spec = 'RandomIV';
/* check for key */
if (!$key) {
return $this->raiseError('no key specified');
}
/* check for cipher */
$cipher = strtoupper($cipher);
if (!isset($this->known_ciphers[$cipher])) {
return $this->raiseError('unknown cipher "'.$cipher.'"' );
}
$this->cipher = $this->known_ciphers[$cipher];
/* initialize 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);
while( strlen($this->keyhash) < $this->keysize ) {
$this->keyhash .= $this->_md5perl($this->keyhash);
}
$this->key = substr($this->keyhash, 0, $this->keysize);
return true;
}
/**
* Destructor
*
*/
function _Crypt_CBC ()
{
@mcrypt_module_close($this->TD);
}
/**
* Encryption method
*
* @param $clear plaintext
*
* @return $crypt encrypted text, or PEAR error
*
* @access public
*
*/
function encrypt($clear)
{
$this->last_clear = $clear;
/* new IV for each message */
$iv = mcrypt_create_iv($this->blocksize, $this->rand_source);
/* create the message header */
$crypt = $this->header_spec . $iv;
/* pad the cleartext */
$padsize = $this->blocksize - (strlen($clear) % $this->blocksize);
$clear .= str_repeat(pack ('C*', $padsize), $padsize);
/* do the encryption */
$start = 0;
while ( $block = substr($clear, $start, $this->blocksize) ) {
$start += $this->blocksize;
if (mcrypt_generic_init($this->TD, $this->key, $iv) < 0 ) {
return $this->raiseError('mcrypt_generic_init failed' );
}
$cblock = mcrypt_generic($this->TD, $iv^$block );
$iv = $cblock;
$crypt .= $cblock;
call_user_func($this->deinit_function, $this->TD);
}
$this->last_crypt = $crypt;
return $crypt;
}
/**
* Decryption method
*
* @param $crypt encrypted text
*
* @return $clear plaintext, or PEAR error
*
* @access public
*
*/
function decrypt($crypt) {
$this->last_crypt = $crypt;
/* get the IV from the message header */
$iv_offset = strlen($this->header_spec);
$header = substr($crypt, 0, $iv_offset);
$iv = substr ($crypt, $iv_offset, $this->blocksize);
if ( $header != $this->header_spec ) {
return $this->raiseError('no initialization vector');
}
$crypt = substr($crypt, $iv_offset+$this->blocksize);
/* decrypt the message */
$start = 0;
$clear = '';
while ( $cblock = substr($crypt, $start, $this->blocksize) ) {
$start += $this->blocksize;
if (mcrypt_generic_init($this->TD, $this->key, $iv) < 0 ) {
return $this->raiseError('mcrypt_generic_init failed' );
}
$block = $iv ^ mdecrypt_generic($this->TD, $cblock);
$iv = $cblock;
$clear .= $block;
call_user_func($this->deinit_function, $this->TD);
}
/* remove the padding from the end of the cleartext */
$padsize = ord(substr($clear, -1));
$clear = substr($clear, 0, -$padsize);
$this->last_clear = $clear;
return $clear;
}
/**
* Emulate Perl's MD5 function, which returns binary data
*
* @param $string string to MD5
*
* @return $hash binary hash
*
* @access private
*
*/
function _md5perl($string)
{
return pack('H*', md5($string));
}
}
?>
|