/**
  * {@inheritdoc}
  */
 public function encrypt($value)
 {
     try {
         $ciphertext = Crypto::Encrypt($value, $this->keyReader->read());
         if (!$this->binaryOutput) {
             $ciphertext = base64_encode($ciphertext);
             if ($ciphertext === false) {
                 throw new SymmetricEncryptionException('Cannot encode message to base64');
             }
         }
         return $ciphertext;
     } catch (CryptoTestFailedException $e) {
         throw SymmetricEncryptionException::cryptoTestFailed($e);
     } catch (CannotPerformOperationException $e) {
         throw SymmetricEncryptionException::cannotPerformOperation($e);
     }
 }
 /**
  * {@inheritdoc}
  */
 public function decrypt($value)
 {
     try {
         if (!$this->binaryOutput) {
             $value = base64_decode($value, true);
             if ($value === false) {
                 throw SymmetricEncryptionException::invalidCiphertext(new InvalidCiphertextException());
             }
         }
         return Crypto::Decrypt($value, $this->keyReader->read());
     } catch (InvalidCiphertextException $e) {
         throw SymmetricEncryptionException::invalidCiphertext($e);
     } catch (CryptoTestFailedException $e) {
         throw SymmetricEncryptionException::cryptoTestFailed($e);
     } catch (CannotPerformOperationException $e) {
         throw SymmetricEncryptionException::cannotPerformOperation($e);
     }
 }
 /**
  * Reader should throw the error when a key file unable to read
  *
  * @expectedException \Gtt\Bundle\CryptBundle\Exception\KeyReaderException
  */
 public function testReadError()
 {
     unlink($this->filename);
     $subject = new KeyReader($this->filename);
     @$subject->read();
 }
 /**
  * Test encryption attempt with invalid key
  *
  * @expectedException \Gtt\Bundle\CryptBundle\Exception\SymmetricEncryptionException
  * @expectedExceptionMessage Cannot safely perform decryption
  */
 public function testCannotPerformOperation()
 {
     $this->keyReader->expects($this->once())->method('read')->willReturn('');
     $encryptor = new AesEncryptor($this->keyReader, true);
     $encryptor->encrypt(Fixtures::PLAIN_TEXT);
 }
 /**
  * {@inheritdoc}
  */
 protected function setUp()
 {
     $this->keyReader = $this->getMockBuilder('Gtt\\Bundle\\CryptBundle\\Bridge\\Aes\\KeyReader')->disableOriginalConstructor()->getMock();
     $this->keyReader->expects($this->once())->method('read')->willReturn(Fixtures::key());
 }