PHP Luminova: Encrypting and Decrypting Data with Crypter
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
encryptordecryptlogs 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): EncryptionInterfaceParameters:
| Parameter | Type | Description |
|---|---|---|
$key | ?string | Optional encryption key (default: env('app.key')). |
Return:
EncryptionInterface - Return instance of application encryption handler (e.g, Openssl or Sodium).
Throws:
- Luminova\Exceptions\EncryptionException
- If the encryption key is missing.
- If the handler is invalid or its extension is not available.
- If the installed Sodium version is unsupported (< 1.0.14).
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): ?stringParameters:
| Parameter | Type | Description |
|---|---|---|
$length | int | The nonce length to generate. |
$string | ?string | Optional 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|boolParameters:
| Parameter | Type | Description |
|---|---|---|
$data | string | The plaintext to encrypt. |
$key | ?string | Optional encryption key (default: env('app.key')). |
$nonce | ?string | Optional nonce. Leave it as null to generate a fresh nonce for the cipher. |
$aad | ?string | Additional 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:
- Luminova\Exceptions\EncryptionException - If encryption fails or input is invalid.
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|boolParameters:
| Parameter | Type | Description |
|---|---|---|
$data | string | The encrypted data to decrypt. |
$key | string[]|string|null | One key or a list of keys to try. With null or an empty list, uses App\Kernel::getApplicationKeys(). |
$aad | ?string | Additional 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:
- Luminova\Exceptions\EncryptionException - If decryption fails or input is invalid.