PHP code example of sentinelphp / encrypt

1. Go to this page and download the library: Download sentinelphp/encrypt library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

sentinelphp / encrypt example snippets


use SentinelPHP\Encrypt\Encryptor;

// Generate a new encryption key
$key = Encryptor::generateKey();

// Create encryptor with the key
$encryptor = new Encryptor($key);

// Encrypt data
$plaintext = 'sensitive data';
$ciphertext = $encryptor->encrypt($plaintext);

// Decrypt data
$decrypted = $encryptor->decrypt($ciphertext);

use SentinelPHP\Encrypt\Encryptor;

// Generate a new key (base64-encoded)
$key = Encryptor::generateKey();
// Store this securely (e.g., environment variable, secrets manager)

// Check if encryption is enabled
$encryptor = new Encryptor($key);
if ($encryptor->isEnabled()) {
    $encrypted = $encryptor->encrypt($data);
}

use SentinelPHP\Encrypt\Encryptor;
use SentinelPHP\Encrypt\Exception\EncryptionException;
use SentinelPHP\Encrypt\Exception\InvalidKeyException;

try {
    $encryptor = new Encryptor($key);
    $decrypted = $encryptor->decrypt($ciphertext);
} catch (InvalidKeyException $e) {
    // Invalid or missing encryption key
} catch (EncryptionException $e) {
    // Encryption/decryption failed (tampered data, wrong key, etc.)
}