PHP code example of miladrahimi / phpcrypt

1. Go to this page and download the library: Download miladrahimi/phpcrypt library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

miladrahimi / phpcrypt example snippets


use MiladRahimi\PhpCrypt\Symmetric;

$symmetric = new Symmetric();
$encryptedData = $symmetric->encrypt('secret');
echo $symmetric->decrypt($encryptedData); // secret

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();

use MiladRahimi\PhpCrypt\Symmetric;

$key = Symmetric::generateKey();

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.
}

use MiladRahimi\PhpCrypt\Symmetric;

print_r(Symmetric::supportedMethods());

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

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

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);

use MiladRahimi\PhpCrypt\Hash;

$hash = new Hash();

$hashedPassword = $hash->make('MyPassword');
echo $hash->verify('MyPassword', $hashedPassword); // true
echo $hash->verify('AnotherPassword', $hashedPassword); // false