PHP Luminova: Encrypt and Decrypt Data
Encrypt and decrypt a value with Crypter, OpenSSL, or Sodium.
Start with Crypter when your project already has an application key and a configured encryption driver. The driver examples show how to work directly with OpenSSL or Sodium. Keep keys on the server and use sample data while testing. See the Encrypting and Decrypting Data with Crypter.
Crypter
Before using Luminova's Crypter, ensure that your application key is properly configured in the environment file app.key.It is recommended to utilize the php novakit generate:key command for generating a new key when necessary.By default, an application key is generated during the installation of Luminova.
Furthermore, you can configure additional optional settings in /app/Config/Encryption.php, such as the encryption handler class, encryption mode, hash digest, etc.
Encrypting
This section describes how to encrypt data using the Crypter class.
use Luminova\Database\Query\Builder;
use Luminova\Security\Encryption\Crypter;
use Luminova\Exceptions\EncryptionException;
/**
* Store a User token.
*/
public function addToken(): void
{
try{
$id = escape($this->request->getPost('user_id'));
$tbl = Builder::table('user_tokens');
$tbl->insert([
'user_id' => $id,
'token' => Crypter::encrypt($this->request->getPost('token')),
]);
response(200)->json(['status' => 'ok']);
}catch(EncryptionException $e){
// ...
}
}Decrypting
This section describes how to decrypt data using the Crypter class.
/**
* Retrieve a User token.
*/
public function useToken(): void
{
try{
$id = escape($this->request->getPost('user_id'));
$tbl = Builder::table('user_tokens');
$tbl->where('user_id', '=', $id);
$row = $tbl->find(['token'])->get();
if (!$row) {
response(404)->json(['status' => 'not found']);
return;
}
$token = Crypter::decrypt($row->token);
// Use $token for the server-side operation that needs it.
response(200)->json(['status' => 'ok']);
}catch(EncryptionException $e){
// ...
}
}Single Encryption Class Usage
If you're looking to encrypt data in your applications, you have the option to choose between two encryption classes provided by the Luminova framework: OpenSSL and Sodium.These classes offer different approaches to encryption, each with its own strengths and benefits. Let's take a closer look at how you can use OpenSSL and Sodium effectively for securing your sensitive data.
OpenSSL
The OpenSSL class provides methods for encrypting and decrypting data using OpenSSL encryption.
use Luminova\Security\Encryption\Driver\Openssl;
// Initialize the encryption key and message
$key = random_bytes(32);
$message = "hello this is awesome secret";
// Create an instance of OpenSSL
$crypt = new Openssl($key, 'aes-256-gcm');
// Set a nonce (initialization vector)
$crypt->setNonce();
// Set the data to be encrypted
$crypt->setData($message);
// Encrypt the data
$encrypted = $crypt->encrypt();
// Output the encrypted message
echo "Encrypted message: " . $encrypted;
// Decrypt the encrypted message
$crypt->setKey($key); // Set the key for decryption
$crypt->setData($encrypted); // Set the encrypted data
echo "Decrypted message: " . $crypt->decrypt();Sodium
The Sodium class provides methods for encrypting and decrypting data using Sodium encryption.
use Luminova\Security\Encryption\Driver\Sodium;
// Initialize the encryption key and message
$key = random_bytes(32);
$message = "hello this is awesome secret";
// Create an instance of Sodium
$crypt = new Sodium();
$crypt->setKey($key, salt: '');
// Set a nonce (initialization vector)
$crypt->setNonce();
// Set the data to be encrypted
$crypt->setData($message);
// Encrypt the data
$encrypted = $crypt->encrypt();
// Output the encrypted message
echo "Encrypted message: " . $encrypted;
// Decrypt the encrypted message
$crypt->setKey($key, salt: ''); // Set the key for decryption
$crypt->setData($encrypted); // Set the encrypted data
echo "Decrypted message: " . $crypt->decrypt();