Luminova Framework

PHP Luminova: Crypter Encryption and Decryption Helper Class

Last updated: 2025-10-30 17:49:25

Luminova Crypter makes encryption simple. Encrypt and decrypt data easily with Sodium or OpenSSL using one unified, beginner-friendly static method.

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 settings, so you don’t have to manually set up or switch handlers. Whether you choose Sodium or OpenSSL, Crypter provides one easy interface for both.

This means you can:

  • Encrypt and decrypt data with just a single call.
  • Switch between handlers without changing your code.
  • Avoid dealing with the low-level details of each handler.

Class Definition

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

Methods

getInstance

Create and return the configured encryption handler instance.

Initializes the selected handler with the encryption key, cipher method, and key size.

You may pass an explicit key via the $key argument, fall back to the application key (env app.key).Configuration class is located in your controller configuration directory in /app/Config/Encryption.php.

public static getInstance(?string $key = null): Luminova\Interface\EncryptionInterface

Parameters:

ParameterTypeDescription
$keystring|nullOptional encryption key (default: env('app.key')).

Return Value:

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

Throws:


encrypt

Encrypt plaintext with the default encryption configuration.

Generates a nonce if required and uses the configured handler (OpenSSL or Sodium) to perform encryption.

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

Parameters:

ParameterTypeDescription
$datastringThe plaintext to encrypt.
$keystring|nullOptional encryption key (default: env('app.key')).
$noncestring|nullOptional nonce (default: auto-generate).

Return Value:

string|false - Return encrypted data in binary or base64, or false on failure.

Throws:

Example

use Luminova\Security\Encryption\Crypter;

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

decrypt

Decrypt a ciphertext with the default application encryption configuration or custom key.

Restores plaintext from the encrypted input. Validates data and propagates errors from the underlying handler.

public static decrypt(string $data, ?string $key = null): string|false

Parameters:

ParameterTypeDescription
$datastringThe encrypted data to decrypt.
$keystring|nullOptional encryption key (default: env('app.key')).

Return Value:

string|false - Return decrypted plaintext or false on failure.

Throws:

Example

use Luminova\Security\Encryption\Crypter;

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

echo Crypter::decrypt($encrypted);