* @package Mammut\Security */ class MCrypt extends \Mammut\StrictObject { private $key = NULL; private $cipher; private $iv = NULL; const CIPHER_AES = MCRYPT_RIJNDAEL_128; const CIPHER_BLOWFISH = MCRYPT_BLOWFISH; const CIPHER_RIJNDAEL_128 = MCRYPT_RIJNDAEL_128; public function __construct($cipher) { if(!extension_loaded('mcrypt')) throw new \Mammut\Exception\ExtensionException('mcrypt'); $this->cipher = $cipher; $this->iv = ''; for($i = 0; $i < mcrypt_get_iv_size($cipher, MCRYPT_MODE_CBC); $i++) { $this->iv .= chr(65 + $i % 6); } } public function setKey($key) { $this->key = $key; } /** * encodes a string with the parameters given * * @param string $data * paintext * @return string the encoded string */ public function encode($data, $key = false) { if(empty($key)) $key = $this->key; return mcrypt_encrypt($this->cipher, $key, $data, MCRYPT_MODE_CBC, $this->iv); } /** * decodes a string with the parameters given * * @param string $data * crypttext * @return string the decoded string */ public function decode($data, $key = false) { if(empty($key)) $key = $this->key; return mcrypt_decrypt($this->cipher, $key, $data, MCRYPT_MODE_CBC, $this->iv); } }