/** * Method to encrypt a data string. * * @param string $data The data string to encrypt. * @param Key $key The key object to use for encryption. * * @return string The encrypted data string. * * @since 3.5 * @throws RuntimeException */ public function encrypt($data, Key $key) { // Validate key. if ($key->getType() != 'crypto') { throw new InvalidArgumentException('Invalid key of type: ' . $key->getType() . '. Expected crypto.'); } // Encrypt the data. try { return Crypto::Encrypt($data, $key->getPublic()); } catch (CryptoTestFailedException $ex) { throw new RuntimeException('Cannot safely perform encryption', $ex->getCode(), $ex); } catch (CannotPerformOperationException $ex) { throw new RuntimeException('Cannot safely perform encryption', $ex->getCode(), $ex); } }