You are here

public function McryptAES128Encryption::encrypt in Two-factor Authentication (TFA) 8

Encrypt text.

Parameters

string $text: The text to be encrypted.

string $key: The key to encrypt the text with.

Return value

string The encrypted text

Throws

\Drupal\encrypt\Exception\EncryptException Thrown when encryption fails.

Overrides EncryptionMethodInterface::encrypt

File

src/Plugin/EncryptionMethod/McryptAES128Encryption.php, line 34

Class

McryptAES128Encryption
Deprecated Mcrypt AES 128 encryption plugin.

Namespace

Drupal\tfa\Plugin\EncryptionMethod

Code

public function encrypt($text, $key) {

  // Backwards compatibility with Mcrypt.
  if (!extension_loaded('openssl') && extension_loaded('mcrypt')) {
    return $this
      ->encryptWithMcrypt($text, $key);
  }

  // Encrypt using OpenSSL.
  $iv = random_bytes(16);
  $ciphertext = openssl_encrypt($text, 'aes-256-cbc', $key, OPENSSL_RAW_DATA, $iv);
  $crypto_data = [
    'version' => self::CRYPT_VERSION,
    'iv_base64' => base64_encode($iv),
    'ciphertext_base64' => base64_encode($ciphertext),
  ];
  return Json::encode($crypto_data);
}