Top Related Projects
High-level cryptography interface powered by libsodium
PHP Secure Communications Library
Compatibility with the password_* functions that ship with PHP 5.5
Quick Overview
defuse/php-encryption is a secure PHP encryption library that provides a simple interface for encrypting and decrypting data. It aims to make it easy for developers to implement strong encryption in their PHP applications without having to be cryptography experts.
Pros
- Easy to use API for encryption and decryption
- Implements modern cryptographic standards (AES-256-CTR with HMAC-SHA256)
- Actively maintained and well-documented
- Provides key generation and management utilities
Cons
- Requires PHP 5.6 or later
- Limited to symmetric encryption (no public key cryptography)
- May have performance overhead for large-scale operations
- Depends on PHP's built-in cryptographic functions
Code Examples
Encrypting a string:
use Defuse\Crypto\Crypto;
use Defuse\Crypto\Key;
$key = Key::createNewRandomKey();
$plaintext = "Secret message";
$ciphertext = Crypto::encrypt($plaintext, $key);
Decrypting a string:
use Defuse\Crypto\Crypto;
use Defuse\Crypto\Key;
$decrypted = Crypto::decrypt($ciphertext, $key);
echo $decrypted; // Outputs: Secret message
Storing an encryption key:
use Defuse\Crypto\Key;
$key = Key::createNewRandomKey();
$keyAscii = $key->saveToAsciiSafeString();
file_put_contents('key.txt', $keyAscii);
Getting Started
- Install the library using Composer:
composer require defuse/php-encryption
- Include the Composer autoloader in your PHP script:
require 'vendor/autoload.php';
- Use the library to encrypt and decrypt data:
use Defuse\Crypto\Crypto;
use Defuse\Crypto\Key;
$key = Key::createNewRandomKey();
$plaintext = "Hello, World!";
$ciphertext = Crypto::encrypt($plaintext, $key);
$decrypted = Crypto::decrypt($ciphertext, $key);
echo $decrypted; // Outputs: Hello, World!
Competitor Comparisons
High-level cryptography interface powered by libsodium
Pros of Halite
- Built on top of libsodium, providing a higher-level abstraction for easier use
- Offers a wider range of cryptographic operations, including key derivation and digital signatures
- Actively maintained with regular updates and security improvements
Cons of Halite
- Requires the libsodium extension, which may not be available on all hosting environments
- Slightly steeper learning curve due to more advanced features and options
- Potentially slower performance for simple encryption tasks compared to php-encryption
Code Comparison
Halite encryption:
use ParagonIE\Halite\KeyFactory;
use ParagonIE\Halite\Symmetric\Crypto as Symmetric;
$key = KeyFactory::generateEncryptionKey();
$message = 'This is a secret message.';
$ciphertext = Symmetric::encrypt($message, $key);
php-encryption encryption:
use Defuse\Crypto\Key;
use Defuse\Crypto\Crypto;
$key = Key::createNewRandomKey();
$message = 'This is a secret message.';
$ciphertext = Crypto::encrypt($message, $key);
Both libraries provide secure encryption, but Halite offers more advanced features at the cost of additional complexity and dependencies.
PHP Secure Communications Library
Pros of phpseclib
- Broader range of cryptographic functions and protocols (SSH, SFTP, X.509, etc.)
- Pure PHP implementation, ensuring cross-platform compatibility
- Actively maintained with regular updates and improvements
Cons of phpseclib
- Larger library size, which may impact performance in some scenarios
- Steeper learning curve due to its extensive feature set
- May require more configuration for specific use cases
Code Comparison
php-encryption:
use Defuse\Crypto\Key;
use Defuse\Crypto\Crypto;
$key = Key::createNewRandomKey();
$ciphertext = Crypto::encrypt($message, $key);
phpseclib:
use phpseclib3\Crypt\AES;
$aes = new AES('cbc');
$aes->setKey($key);
$ciphertext = $aes->encrypt($message);
Summary
phpseclib offers a more comprehensive cryptographic toolkit with support for various protocols, making it suitable for complex security implementations. However, php-encryption provides a more focused and user-friendly approach to encryption, with a simpler API and easier setup for basic encryption tasks. The choice between the two depends on the specific requirements of your project, with phpseclib being more versatile but potentially overkill for simpler use cases, while php-encryption offers a streamlined solution for basic encryption needs.
Compatibility with the password_* functions that ship with PHP 5.5
Pros of password_compat
- Focused specifically on password hashing and verification
- Provides a forward-compatible implementation of PHP's password_* functions
- Lightweight and easy to integrate into existing projects
Cons of password_compat
- Limited to password-related functionality
- Not actively maintained (last commit in 2018)
- Lacks additional cryptographic features found in php-encryption
Code Comparison
password_compat:
$hash = password_hash("password123", PASSWORD_DEFAULT);
if (password_verify("password123", $hash)) {
echo "Password is valid!";
}
php-encryption:
use Defuse\Crypto\Crypto;
$ciphertext = Crypto::encrypt("secret message", $key);
$plaintext = Crypto::decrypt($ciphertext, $key);
Summary
password_compat is a lightweight library focused on password hashing and verification, providing forward compatibility for PHP's built-in password functions. It's easy to use but limited in scope and no longer actively maintained.
php-encryption, on the other hand, offers a broader range of cryptographic functions, including encryption and decryption of data. It's actively maintained and provides a more comprehensive solution for various cryptographic needs beyond just password handling.
Choose password_compat for simple password hashing in older PHP versions, or php-encryption for a more robust and actively maintained cryptographic toolkit.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
php-encryption
composer require defuse/php-encryption
This is a library for encrypting data with a key or password in PHP. It requires PHP 5.6 or newer and OpenSSL 1.0.1 or newer. We recommend using a version of PHP that still has security support, which at the time of writing means PHP 8.0 or later. Using this library with an unsupported version of PHP could lead to security vulnerabilities.
The current version of php-encryption
is v2.4.0. This library is expected to
remain stable and supported by its authors with security and bugfixes until at
least January 1st, 2024.
The library is a joint effort between Taylor Hornby and Scott Arciszewski as well as numerous open-source contributors.
What separates this library from other PHP encryption libraries is, firstly, that it is secure. The authors used to encounter insecure PHP encryption code on a daily basis, so they created this library to bring more security to the ecosystem. Secondly, this library is "difficult to misuse." Like libsodium, its API is designed to be easy to use in a secure way and hard to use in an insecure way.
Dependencies
This library requires no special dependencies except for PHP 5.6 or newer with the OpenSSL extensions (version 1.0.1 or later) enabled (this is the default). It uses random_compat, which is bundled in with this library so that your users will not need to follow any special installation steps.
Getting Started
Start with the Tutorial. You can find instructions for obtaining this library's code securely in the Installing and Verifying documentation.
After you've read the tutorial and got the code, refer to the formal documentation for each of the classes this library provides:
If you encounter difficulties, see the FAQ answers. The fixes to the most commonly-reported problems are explained there.
If you're a cryptographer and want to understand the nitty-gritty details of how this library works, look at the Cryptography Details documentation.
If you're interested in contributing to this library, see the Internal Developer Documentation.
Other Language Support
This library is intended for server-side PHP software that needs to encrypt data at rest. If you are building software that needs to encrypt client-side, or building a system that requires cross-platform encryption/decryption support, we strongly recommend using libsodium instead.
Examples
If the documentation is not enough for you to understand how to use this library, then you can look at an example project that uses this library:
Security Audit Status
This code has not been subjected to a formal, paid, security audit. However, it has received lots of review from members of the PHP security community, and the authors are experienced with cryptography. In all likelihood, you are safer using this library than almost any other encryption library for PHP.
If you use this library as a part of your business and would like to help fund a formal audit, please contact Taylor Hornby.
Public Keys
The GnuPG public key used to sign the current and new releases is available in dist/signingkey-new.asc. Its fingerprint is:
6DD6 E677 0281 5846 FC85 25A3 DD2E 507F 7BDB 1669
You can verify it against Taylor Hornby's contact page and twitter.
Older releases were signed with a (now-expired) available in dist/signingkey-old.asc. The old key's fingerprint is:
2FA6 1D8D 99B9 2658 6BAC 3D53 385E E055 A129 1538
The old key's fingerprint can be verified against Taylor Hornby's contact page and twitter.
A signature of this new key by the old key is available in dist/signingkey-new.asc.sig.
Top Related Projects
High-level cryptography interface powered by libsodium
PHP Secure Communications Library
Compatibility with the password_* functions that ship with PHP 5.5
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot