/** * Decrypt * * @param string $data * @throws Exception\InvalidArgumentException * @return string */ public function decrypt($data) { if (empty($data)) { throw new Exception\InvalidArgumentException('The data to decrypt cannot be empty'); } if (null === $this->getKey()) { throw new Exception\InvalidArgumentException('No key specified for the decryption'); } if (null === $this->getPadding()) { throw new Exception\InvalidArgumentException('You have to specify a padding method'); } $iv = substr($data, 0, $this->getSaltSize()); $ciphertext = substr($data, $this->getSaltSize()); $result = mcrypt_decrypt($this->supportedAlgos[$this->algo], $this->getKey(), $ciphertext, $this->supportedModes[$this->mode], $iv); // unpadding return $this->padding->strip($result); }
/** * Decrypt * * @param string $data * @throws Exception\InvalidArgumentException * @return string */ public function decrypt($data) { if (empty($data)) { throw new Exception\InvalidArgumentException('The data to decrypt cannot be empty'); } if (null === $this->getKey()) { throw new Exception\InvalidArgumentException('No decryption key specified'); } if (null === $this->getPadding()) { throw new Exception\InvalidArgumentException('You must specify a padding method'); } $iv = mb_substr($data, 0, $this->getSaltSize(), '8bit'); $ciphertext = mb_substr($data, $this->getSaltSize(), null, '8bit'); $result = openssl_decrypt($ciphertext, strtoupper($this->encryptionAlgos[$this->algo] . '-' . $this->mode), $this->getKey(), OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $iv); if (false === $result) { $errMsg = ''; while ($msg = openssl_error_string()) { $errMsg .= $msg; } throw new Exception\RuntimeException(sprintf('OpenSSL error: %s', $errMsg)); } // unpadding return $this->padding->strip($result); }