PhpCrypt is a package for encryption, decryption, and hashing data in PHP projects. It provides an easy-to-use and fluent interface.
Features:
- Symmetric encryption/decryption using AES and other symmetric methods.
- Asymmetric encryption/decryption using the RSA method.
- Hashing and verifying data (e.g. passwords) using the BCrypt method.
- v5.x.x
- v4.x.x
- v3.x.x (Unsupported)
- v2.x.x (Unsupported)
- v1.x.x (Unsupported)
Install Composer and run the following command in your project's root directory:
composer require miladrahimi/phpcrypt "5.*"
This example shows how to encrypt and decrypt data using symmetric algorithms like AES.
use MiladRahimi\PhpCrypt\Symmetric;
$symmetric = new Symmetric();
$encryptedData = $symmetric->encrypt('secret');
echo $symmetric->decrypt($encryptedData); // secret
It generates a random key and uses aes-256-cbc
method for encrypting/decrypting data.
If you have already a key, you can use your own key like this:
use MiladRahimi\PhpCrypt\Symmetric;
$key = '1234567890123456';
// Set the key using the constructor
$symmetric = new Symmetric($key);
// Or set the key using the setter
$symmetric = new Symmetric();
$symmetric->setKey($key);
// And get the key using the getter
$myKey = $symmetric->getKey();
The method generateKey
can help you to generate a new random key.
See the snippet below.
use MiladRahimi\PhpCrypt\Symmetric;
$key = Symmetric::generateKey();
In default, The Symmetric
class uses aes-256-cbc
method to encrypt/decrypt data.
You can use your preferred method as well.
See the following example.
use MiladRahimi\PhpCrypt\Exceptions\MethodNotSupportedException;
use MiladRahimi\PhpCrypt\Symmetric;
try {
$symmetric = new Symmetric();
$symmetric->setMethod('aria-256-ctr');
// ...
} catch (MethodNotSupportedException $e) {
// The method is not supported.
}
If you want to know which methods do your installed OpenSSL extension support, see the snippet below:
use MiladRahimi\PhpCrypt\Symmetric;
print_r(Symmetric::supportedMethods());
RSA is a popular asymmetric encryption/decryption algorithm. The examples below illustrate how to encrypt/decrypt data using the RSA algorithm.
In this example, we encrypt data with a private key and decrypt it with the related public key.
use MiladRahimi\PhpCrypt\PrivateRsa;
use MiladRahimi\PhpCrypt\PublicRsa;
$privateRsa = new PrivateRsa('private_key.pem');
$publicRsa = new PublicRsa('public_key.pem');
$result = $privateRsa->encrypt('secret');
echo $publicRsa->decrypt($result); // secret
In this example, we encrypt data with a public key and decrypt it with the related private key.
use MiladRahimi\PhpCrypt\PrivateRsa;
use MiladRahimi\PhpCrypt\PublicRsa;
$privateRsa = new PrivateRsa('private_key.pem');
$publicRsa = new PublicRsa('public_key.pem');
$result = $publicRsa->encrypt('secret');
echo $privateRsa->decrypt($result); // secret
In default, the encrypted data returned by PrivateRsa::encrypt()
and PublicRsa::encrypt()
methods will be Base64 encoded.
You can disable this encoding like the example below.
use MiladRahimi\PhpCrypt\PrivateRsa;
use MiladRahimi\PhpCrypt\PublicRsa;
$privateRsa = new PrivateRsa('private_key.pem');
$publicRsa = new PublicRsa('public_key.pem');
// Disable Base64 encoding for public encryption
$result = $publicRsa->encrypt('secret', false);
// Disable Base64 encoding for private encryption
$result = $privateRsa->encrypt('secret', false);
This example shows how to hash data and verify it.
use MiladRahimi\PhpCrypt\Hash;
$hash = new Hash();
$hashedPassword = $hash->make('MyPassword');
echo $hash->verify('MyPassword', $hashedPassword); // true
echo $hash->verify('AnotherPassword', $hashedPassword); // false
The Symmetric
, PrivateRsa
, PublicRsa
, and Hash
classes may throw these exceptions:
EncryptionException
: When it cannot encrypt data.DecryptionException
: When it cannot decrypt data.HashingException
: When it cannot hash data.MethodNotSupportedException
: When the passed encryption method to theSymmetric
class is not supported.InvalidKeyException
: When the passed key toPrivateRsa
orPublicRsa
classes is not valid.
PhpCrypt is initially created by Milad Rahimi and released under the MIT License.