/**
  * Encrypt
  *
  * @param  string $data
  * @throws Exception\InvalidArgumentException
  * @return string
  */
 public function encrypt($data)
 {
     // Cannot encrypt empty string
     if (!is_string($data) || $data === '') {
         throw new Exception\InvalidArgumentException('The data to encrypt cannot be empty');
     }
     if (null === $this->getKey()) {
         throw new Exception\InvalidArgumentException('No key specified for the encryption');
     }
     if (null === $this->getSalt() && $this->getSaltSize() > 0) {
         throw new Exception\InvalidArgumentException('The salt (IV) cannot be empty');
     }
     if (null === $this->getPadding()) {
         throw new Exception\InvalidArgumentException('You must specify a padding method');
     }
     // padding
     $data = $this->padding->pad($data, $this->getBlockSize());
     $iv = $this->getSalt();
     // encryption
     $result = openssl_encrypt($data, 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));
     }
     return $iv . $result;
 }
Пример #2
0
 /**
  * Encrypt
  *
  * @param  string $data
  * @return string
  */
 public function encrypt($data)
 {
     if (empty($data)) {
         throw new Exception\InvalidArgumentException('The data to encrypt cannot be empty');
     }
     if (null === $this->getKey()) {
         throw new Exception\InvalidArgumentException('No key specified for the encryption');
     }
     if (null === $this->getSalt()) {
         throw new Exception\InvalidArgumentException('The salt (IV) cannot be empty');
     }
     if (null === $this->getPadding()) {
         throw new Exception\InvalidArgumentException('You have to specify a padding method');
     }
     // padding
     $data = $this->padding->pad($data, $this->getBlockSize());
     // get the correct iv size
     $iv = substr($this->iv, 0, $this->getSaltSize());
     // encryption
     $result = mcrypt_encrypt(
         $this->supportedAlgos[$this->algo],
         substr($this->key, 0, $this->getKeySize()),
         $data,
         $this->supportedModes[$this->mode],
         $iv
     );
     return $iv . $result;
 }
Пример #3
0
 /**
  * Encrypt
  *
  * @param  string                             $data
  * @throws Exception\InvalidArgumentException
  * @return string
  */
 public function encrypt($data)
 {
     // Cannot encrypt empty string
     if (!is_string($data) || $data === '') {
         throw new Exception\InvalidArgumentException('The data to encrypt cannot be empty');
     }
     if (null === $this->getKey()) {
         throw new Exception\InvalidArgumentException('No key specified for the encryption');
     }
     if (null === $this->getSalt()) {
         throw new Exception\InvalidArgumentException('The salt (IV) cannot be empty');
     }
     if (null === $this->getPadding()) {
         throw new Exception\InvalidArgumentException('You have to specify a padding method');
     }
     // padding
     $data = $this->padding->pad($data, $this->getBlockSize());
     $iv = $this->getSalt();
     // encryption
     $result = mcrypt_encrypt($this->supportedAlgos[$this->algo], $this->getKey(), $data, $this->supportedModes[$this->mode], $iv);
     return $iv . $result;
 }