public function decrypt($text, $key = null)
{
if (!function_exists("mcrypt_get_iv_size")) {
throw new Exception("mcrypt extension is required");
}
if ($key === null) {
$decryptKey = $this->key;
} else {
$decryptKey = $key;
}
if (empty($decryptKey)) {
throw new Exception("Decryption key cannot be empty");
}
$ivSize = mcrypt_get_iv_size($this->cipher, $this->mode);
$keySize = strlen($decryptKey);
if ($keySize > $ivSize) {
throw new Exception("Size of key is too large for this algorithm");
}
if ($keySize > strlen($text)) {
throw new Exception("Size of IV is larger than text to decrypt. Are you trying to decrypt an uncrypted text?");
}
$data = substr($text, $ivSize);
$decrypted = mcrypt_decrypt($this->cipher, $decryptKey, $data, $this->mode, substr($text, 0, $ivSize));
$blockSize = mcrypt_get_block_size($this->cipher, $this->mode);
if ($this->mode == MCRYPT_MODE_CBC || $this->mode == MCRYPT_MODE_ECB) {
return $this->cryptUnpadText($decrypted, $this->mode, $blockSize, $this->padding);
}
return $decrypted;
}