Luminova Framework

PHP Luminova: Encrypting and Decrypting Data with Crypter

Last updated: 2026-11-28 20:57:12

Encrypt and decrypt application data using the configured encryption handler.

The Crypter class is a simple helper for encryption and decryption, it makes encryption in Luminova easier, safer, and more consistent.

It automatically uses your application’s Encryption Configuration to select a handler and configure the cipher. The examples below use the OpenSSL configuration. Keep the handler, cipher, and key settings consistent when decrypting stored data.

This means you can:

  • Encrypt and decrypt data with just a single call.
  • Use the same application API for the configured handler.
  • Avoid dealing with the low-level details of each handler.

Usages

Crypter Examples

Encrypt and decrypt message.

use Luminova\Security\Encryption\Crypter;

$encrypted = Crypter::encrypt('Hello world');

if($encrypted === false){
  echo 'Encryption failed';
  return;
}

$plaintext = Crypter::decrypt($encrypted);
if ($plaintext !== false) {
    echo $plaintext;
}

Note

Using encrypt or decrypt logs exceptions in production and return false instead.


Generate a random nonce

use Luminova\Security\Encryption\Crypter;

$nonce = Crypter::nonce(16);

Application Cypher Object

Use getInstance() to work directly with the configured driver. Generate a nonce before encrypting, and pass the encrypted payload to a fresh instance for decryption.

use Luminova\Security\Encryption\Crypter;

$writer = Crypter::getInstance();
$reader = Crypter::getInstance();

try {
    $payload = $writer->setNonce()->setData('Hello world')->encrypt();
    echo $reader->setData($payload)->decrypt(); // Hello world
} finally {
    $writer->free();
    $reader->free();
}

Class Definition

  • Class namespace: Luminova\Security\Encryption\Crypter
  • This class is marked as final and can't be subclassed

Methods

getInstance

Returns a configured application encryption handler instance.

Resolves the active encryption driver (OpenSSL or Sodium), applies the encryption key, cipher method, and key size, and returns a ready-to-use handler.

If no key is provided, the application key (env('app.key')) is used.

public static function getInstance(?string $key = null): EncryptionInterface

Parameters:

ParameterTypeDescription
$key?stringOptional encryption key (default: env('app.key')).

Return:

EncryptionInterface - Return instance of application encryption handler (e.g, Openssl or Sodium).

Throws:

Configuration:

The application configuration lives in app/Config/Encryption.php.


nonce

Generate a random nonce, or return from a string.

This method generates drivers specific nonce.

public static function nonce(int $length, ?string $string = null): ?string

Parameters:

ParameterTypeDescription
$lengthintThe nonce length to generate.
$string?stringOptional string to drive the nonce from.

Return:

string|null - Return the generated encryption nonce string or null if failed.


encrypt

Encrypts plaintext using the default encryption configuration.

This method automatically selects the configured handler (OpenSSL or Sodium), generates a noncewhen required, and returns the encrypted payload.

public static function encrypt(
    string $data,
    ?string $key = null,
    ?string $nonce = null,
    ?string $aad = null
): string|bool

Parameters:

ParameterTypeDescription
$datastringThe plaintext to encrypt.
$key?stringOptional encryption key (default: env('app.key')).
$nonce?stringOptional nonce. Leave it as null to generate a fresh nonce for the cipher.
$aad?stringAdditional authenticated data for a compatible AEAD cipher (default null). Use the same bytes for encryption and decryption.

Return:

string|bool - The encrypted payload, or false if encryption fails in production.

Throws:

Example

use Luminova\Security\Encryption\Crypter;

$encrypted = Crypter::encrypt('Hello world');

decrypt

Decrypts encrypted data using the default encryption configuration.

This method resolves the active handler, validates the input, and restores the original plaintext, using the default application encryption configuration or a custom key.

public static function decrypt(
    string $data,
    array|string|null $key = null,
    ?string $aad = null
): string|bool

Parameters:

ParameterTypeDescription
$datastringThe encrypted data to decrypt.
$keystring[]|string|nullOne key or a list of keys to try. With null or an empty list, uses App\Kernel::getApplicationKeys().
$aad?stringAdditional authenticated data for a compatible AEAD cipher (default null). Use the same bytes for encryption and decryption.

Return:

string|bool - The decrypted plaintext, or false when decryption fails in production.

Throws: