Back to CryptoCipher class

Method encrypt

public string
encrypt
(mixed $data, \Joomla\Crypt\Key $key)
Method to encrypt a data string.
Parameters
  • string $data The data string to encrypt.
  • \Joomla\Crypt\Key $key The key object to use for encryption.
Returns
  • string The encrypted data string.
Since
  • 3.5
-
  • \RuntimeException
Class: CryptoCipher
Project: Joomla

Method encrypt - Source code

/**
 * 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);
    }
}