Back to Aes class

Method encryptString

public string
encryptString
(mixed $stringToEncrypt, mixed $base64encoded = true)
Encrypts a string using AES
Parameters
  • string $stringToEncrypt The plaintext to encrypt
  • bool $base64encoded Should I Base64-encode the result?
Returns
  • string The cryptotext. Please note that the first 16 bytes of the raw string is the IV (initialisation vector) which is necessary for decoding the string.
Class: Aes
Project: Joomla

Method encryptString - Source code

/**
 * Encrypts a string using AES
 *
 * @param   string $stringToEncrypt The plaintext to encrypt
 * @param   bool   $base64encoded   Should I Base64-encode the result?
 *
 * @return   string  The cryptotext. Please note that the first 16 bytes of
 *                   the raw string is the IV (initialisation vector) which
 *                   is necessary for decoding the string.
 */
public function encryptString($stringToEncrypt, $base64encoded = true)
{
    $blockSize = $this->adapter->getBlockSize();
    $randVal = new Randval();
    $iv = $randVal->generate($blockSize);
    $key = $this->getExpandedKey($blockSize, $iv);
    $cipherText = $this->adapter->encrypt($stringToEncrypt, $key, $iv);
    // Optionally pass the result through Base64 encoding
    if ($base64encoded) {
        $cipherText = base64_encode($cipherText);
    }
    // Return the result
    return $cipherText;
}