PHP code example of justmd5 / crypto

1. Go to this page and download the library: Download justmd5/crypto 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/ */

    

justmd5 / crypto example snippets


use Justmd5\Crypto\Rsa\KeyPair;
use Justmd5\Crypto\Rsa\PrivateKey;
use Justmd5\Crypto\Rsa\PublicKey;

// generating an RSA key pair
list($privateKey,$publicKey)   = (new KeyPair())->generate();

// when passing paths, the generated keys will be written those paths
(new KeyPair())->generate($pathToPrivateKey, $pathToPublicKey);

$data = 'my secret data';

$privateKey = PrivateKey::fromFile($pathToPrivateKey);
$encryptedData = $privateKey->encrypt($data); // returns something unreadable

$publicKey = PublicKey::fromFile($pathToPublicKey);
$decryptedData = $publicKey->decrypt($encryptedData); // returns 'my secret data'

use Justmd5\Crypto\Rsa\KeyPair;

 list($privateKey,$publicKey)   = (new KeyPair())->generate();

// when passing paths, the generate keys will to those paths
(new KeyPair())->generate($pathToPrivateKey, $pathToPublicKey);

 $generate = (new KeyPair())->password('my-password')->generate();
list(passwordProtectedPrivateKey,$publicKey)   = (new KeyPair())->generate();

Justmd5\Crypto\Rsa\PrivateKey::fromFile($pathToPrivateKey);
Justmd5\Crypto\Rsa\PublicKey::fromFile($pathToPublicKey);

Justmd5\Crypto\Rsa\PrivateKey::fromString($privateKeyString);
Justmd5\Crypto\Rsa\PublicKey::fromString($publicKeyString);

Justmd5\Crypto\Rsa\PrivateKey::fromFile($pathToPrivateKey, $password);
Justmd5\Crypto\Rsa\PrivateKey::fromString($privateKeyString, $password);

$data = 'my secret data';

$privateKey = Justmd5\Crypto\Rsa\PrivateKey::fromFile($pathToPrivateKey);
$encryptedData = $privateKey->encrypt($data); // encrypted data contains something unreadable

$publicKey = Justmd5\Crypto\Rsa\PublicKey::fromFile($pathToPublicKey);
$decryptedData = $publicKey->decrypt($encryptedData); // decrypted data contains 'my secret data'

$data = 'my secret data';

$publicKey = Justmd5\Crypto\Rsa\PublicKey::fromFile($pathToPublicKey);
$encryptedData = $publicKey->encrypt($data); // encrypted data contains something unreadable

$privateKey = Justmd5\Crypto\Rsa\PrivateKey::fromFile($pathToPrivateKey);
$decryptedData = $privateKey->decrypt($encryptedData); // decrypted data contains 'my secret data'

Justmd5\Crypto\Rsa\PrivateKey::fromFile($pathToPrivateKey)->canDecrypt($data); // returns a boolean;
Justmd5\Crypto\Rsa\PublicKey::fromFile($pathToPublicKey)->canDecrypt($data); // returns a boolean;

$signature = Justmd5\Crypto\Rsa\PrivateKey::fromFile($pathToPrivateKey)->sign('my message'); // returns a string

$publicKey = Justmd5\Crypto\Rsa\PublicKey::fromFile($pathToPublicKey);

$publicKey->verify('my message', $signature) // returns true;
$publicKey->verify('my modified message', $signature) // returns false;