opichon/pcrypt
pcrypt is a small PHP/Laravel package that helps you encrypt and decrypt values using password-based cryptography. Useful for securely storing sensitive strings, generating encrypted payloads, and adding an extra layer of protection beyond plain hashing.
Installation
composer require opichon/pcrypt
No additional configuration is required—pcrypt is a lightweight, dependency-free library.
Basic Usage Encrypt a string:
use Opichon\Pcrypt\Facades\Pcrypt;
$encrypted = Pcrypt::encrypt('sensitive-data');
Decrypt a string:
$decrypted = Pcrypt::decrypt($encrypted);
First Use Case Securely store API keys or user tokens in a database:
// Store
$user->api_token = Pcrypt::encrypt($rawToken);
$user->save();
// Retrieve
$rawToken = Pcrypt::decrypt($user->api_token);
Database Storage Encrypt sensitive fields before saving to a database:
$user = new User();
$user->password = Pcrypt::encrypt($request->password);
$user->save();
API Requests/Responses Encrypt payloads before sending to untrusted services:
$encryptedPayload = Pcrypt::encrypt(json_encode($data));
// Send $encryptedPayload via HTTP
Configuration Files
Store secrets in config/app.php:
'services' => [
'stripe' => [
'key' => Pcrypt::decrypt(env('STRIPE_KEY_ENCRYPTED')),
],
],
Laravel Events/Listeners Automatically encrypt/decrypt data in observers:
public function saving(User $user)
{
$user->password = Pcrypt::encrypt($user->password);
}
Form Request Validation Decrypt input before validation:
public function rules()
{
return [
'encrypted_token' => 'required|string',
];
}
public function prepareForValidation()
{
$this->merge([
'token' => Pcrypt::decrypt($this->encrypted_token),
]);
}
API Middleware Decrypt auth tokens in middleware:
public function handle($request, Closure $next)
{
$request->merge([
'token' => Pcrypt::decrypt($request->bearerToken()),
]);
return $next($request);
}
Key Management
env() or a secrets manager (e.g., AWS Secrets Manager).Performance
Error Handling
try-catch:
try {
$data = Pcrypt::decrypt($encrypted);
} catch (\Opichon\Pcrypt\Exceptions\DecryptException $e) {
// Log or handle gracefully
}
Compatibility
Verify Encryption/Decryption Compare original and decrypted data:
$original = 'test';
$encrypted = Pcrypt::encrypt($original);
$decrypted = Pcrypt::decrypt($encrypted);
assert($original === $decrypted);
Check Key Validity If decryption fails, verify the key matches the encryption key:
Pcrypt::setKey('correct-key-here');
Custom Algorithms
Extend the library by implementing Opichon\Pcrypt\Contracts\Encryptor:
class CustomEncryptor implements Encryptor {
public function encrypt($data, $key) { ... }
public function decrypt($data, $key) { ... }
}
Register via service provider:
Pcrypt::extend('custom', function () {
return new CustomEncryptor();
});
Base64 vs. Hex Output Configure output format:
Pcrypt::setOutputFormat(\Opichon\Pcrypt\Contracts\Pcrypt::BASE64);
// or
Pcrypt::setOutputFormat(\Opichon\Pcrypt\Contracts\Pcrypt::HEX);
Logging Log encryption/decryption events for auditing:
Pcrypt::onEncrypting(function ($data, $key) {
\Log::debug('Encrypted data', ['data' => $data, 'key' => $key]);
});
How can I help you explore Laravel packages today?