<?php
/**
*
* @author Benjamin Gillissen <hide@address.com>
*
* **************************************************************
Copyright (C) 2009 Benjamin Gillissen
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details at:
http://www.gnu.org/copyleft/gpl.html
* **************************************************************
*/
class crypt {
private $cipher, $mode;
private $key;
private $td;
private $iv;
public function __construct($cipher, $mode='ECB', $key=NULL){
if ( ! CORE::isphpModLoaded('mcrypt') ){
if ( ! CORE::LoadphpMod('mcrypt') ){
errors::raise('Missing PHP extention, ask your admin to reinstall php with the mcrypt support', CORE_LOG_ALERT, 'CRYPT');
}
}
$c = 'MCRYPT_'.$cipher;
if ( !defined($c) ){
errors::raise('Unknow mcrypt cipher "'.$cipher.'", plz check your conf', CORE_LOG_ALERT, 'CRYPT');
}
$this->cipher = constant($c);
unset($c);
$m = 'MCRYPT_MODE_'.$mode;
if ( !defined($m) ){
errors::raise('Unknow mcrypt mode "'.$mode.'", plz check your conf', CORE_LOG_ALERT, 'CRYPT');
}
$this->mode = constant($m);
unset($m);
$this->td = mcrypt_module_open($this->cipher, '', $this->mode, '');
$this->iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($this->td), MCRYPT_RAND);
if ( $this->key !== NULL ){
$this->setkey($key);
}
}
public function setkey($key){
$this->key = $key;
mcrypt_generic_init($this->td, $this->key, $this->iv);
}
public function encrypt($str){
//$str = base64_encode($str);
return mcrypt_encrypt($this->cipher, $this->key, $str, $this->mode, $this->iv);
// return mcrypt_encrypt($this->cipher, $this->key, $str, $this->mode);
}
public function decrypt($str){
if (!$str){ return FALSE; }
//$str = base64_decode($str);
return trim(mcrypt_decrypt($this->cipher, $this->key, $str, $this->mode, $this->iv));
// return trim(mcrypt_decrypt($this->cipher, $this->key, $str, $this->mode));
}
}