A simple PHP class to encrypt a string, and decrypt an encrypted string
- Add the following to your
composer.json
file-
{
"require": {
"mukto90/ncrypt": "dev-master"
}
}
- Run
composer install
command. - Include your autoloader file (if not already), like this-
include 'vendor/autoload.php';
- Copy
class.ncrypt.php
file fromncrypt/src/
directory to your project. - Include the class in your project file, like this-
include 'src/class.ncrypt.php';
$ncrypt = new mukto90\Ncrypt;
- Optionally set secret key, secret IV and cipher
$ncrypt->set_secret_key( '^&-my-key-&^' ); // optional, but STRONGLY recommended
$ncrypt->set_secret_iv( '#@)-my-iv-#*$' ); // optional, but STRONGLY recommended
$ncrypt->set_cipher( 'AES-256-CBC' ); // optional
- Pass your string to
encrypt()
method-
$encrypted = $ncrypt->encrypt( 'Hello World!' ); // output: SFpQVWk0WjFxdW5lSGFXaUdWUEx3Zz09
- Pass the already encrypted string to
decrypt()
method-
$decrypted = $ncrypt->decrypt( 'SFpQVWk0WjFxdW5lSGFXaUdWUEx3Zz09' ); // output: Hello World!
- PHP 5.3.0
php_openssl
library needs to be enabled. See here