Cryptor
Quantum comes with Cryptor
library which uses OpenSSL to provide AES-128
and AES-256
encryption.
Before using the library make sure that the OpenSSL
PHP extension available and the openssl.cnf
config file is exists and in correct place.
Make sure the APP_KEY
exists in .env
file (which should be automatically generated at project installtion). If the APP_KEY
is missing you can use qt
command line tool:
php qt core:key-generate
Quantum's Cryptor
library provides two ways of data encrytion using Symmetric
and Asymmetric
cryptography
To start using the Cryptor
library, first make it available in your code
use Quantum\Libraries\Encryption\Cryptor;
Create new Cryptor
instance to start with symmetric cryptography
$cryptor = new Cryptor();
The encrypt()
method is used to encrypt the data
$encrypted = $cryptor->encrypt('Secret Message');
The decrypt()
method is used to decrypt the data, false
will be returned if it's unable to decrypt the data
out($cryptor->decrypt($encrypted));
To start with asymmetric cryptography pass true
to Cryptor
constructor
$cryptor = new Cryptor(true);
To ensure the Cryptor
is in asymmetric mode use isAsymmetric()
method, which returns true
if it is, otherwise false
will be returned.
if($cryptor->isAsymmetric()) {
// asymmetric mode
} else {
// symmetric mode
}
Before proceeding with encyption with asymmetric cryptography, first public
and private
keys need to be fetched, which can be done in the following way:
$publicKey = $cryptor->getPublicKey();
$privateKey = $cryptor->getPrivateKey();
private
key in safe place and never share it, while the public
key can be shared.
To encrypt the message we use public
key
$encrypted = $cryptor->encrypt('Secret Message', $publicKey);
To decrypt the message we use private
key
out($cryptor->decrypt($encrypted, $privateKey));