defuse/php-encryption
Secure, hard-to-misuse PHP encryption library for encrypting data with keys or passwords. Requires PHP 5.6+ and OpenSSL 1.0.1+. Simple API inspired by libsodium, built to avoid common crypto mistakes.
composer require defuse/php-encryption.vendor/bin/generate-defuse-key
FileEncryption and EnvironmentEncryption classes for common cases. For in-memory data, use Encryption::encrypt() and Encryption::decrypt() with Key objects.$key = Key::loadFromJsonString(getenv('DEFUSE_KEY')); // key as JSON string from env
$encrypted = Encryption::encrypt('my_secret_password', $key);
$key = Key::loadFromJsonString($secretKeyJson);
$decrypted = Encryption::decrypt($encryptedConfigValue, $key);
EncryptThenMac (used internally) to prevent tampering. Avoid manual MAC handling—rely on the library’s defaults.FileEncryption::encryptFile($plaintextFile, $ciphertextFile, $key) for large data without loading into memory.Key::loadFromJsonString($json)).Encryption::decrypt() throws CryptoException on invalid MAC/ciphertext or WrongKeyException on incorrect key. Always wrap decryption in try/catch with specific logging (avoid leaking info in exceptions).Encryption::decryptWithPossibleKeys($ciphertext, $possibleKeys).How can I help you explore Laravel packages today?