PHP code example of shetabit / crypto

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

    

shetabit / crypto example snippets


$crypto = new Crypto;

// generate private key
$privateKey = $crypto->createPrivateKey();
echo $privateKey;

// generate public key
$publicKey = $crypto->createPublicKey($privateKey);
echo $publicKey;

$data = 'a simple text';

// encrypt, plain data
$encryptedData = $crypto->encrypt($publicKey, $data);
echo $encryptedData;

// decrypt, encrypted data
$decryptedData = $crypto->decrypt($privateKey, $encryptedData);
echo $decryptedData;

$crypt = new Crypt;

$privateKey = $crypt->createPrivateKey();

echo $privatekey;

$crypt = new Crypt;

$privateKey = $crypt->createPrivateKey(Crypto::OPENSSL_KEYTYPE_RSA);

echo $privatekey;

$privatekey = '-----BEGIN EC PRIVATE KEY-----
MIHuAgEBBEgC1SvKxAMTrXYmC9CV+euaL8KVemuuU6I9A5moUh4HgTzESYt35lgc
CiwMetwIaB9RHFM7869D4rClXvnxFy91nMklcY7IsCmgBwYFK4EEACehgZUDgZIA
BAHBdOduvUMAft3s3xq/70CyHtJTfbFAMmyE6rIOAXYlOcvCvwfLRTbAXkZ+PnU+
5SkSqpC036F02Hr8GNxToie+gLrEAsmjQwLImuN9o+1UPwYR3LdG8N4JsnFkIMYr
qTvrA+usw5pjfZmqE4hHlFVdIaIb7r2m8w2OEZTfsfxQjRzY6uE9P3711NWdZdP2
Rg==
-----END EC PRIVATE KEY-----';

$crypt = new Crypt;

$publicKey = $crypt->createPublicKey($privateKey);

echo $publicKey;

$publicKey = "-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAzLBs9Y2+QSr8l5LV8beY
nidHPHhM0/zMy1V7CiTWQyQVyxsZKHQJZGP+zJNovSH+KG9wfUe5XnC7qVDU80Bi
ABfQFhLe+t7w8UnBbRnDxMnLCjoSrtG8ZlJBkfmvTMf4MdmvMUqzqUD+ssB76BOP
Cce7qGntIlhQOzj3crliLex6E4OmwUP9RgPtIz/GQuI+O/XSf6irkkRjE8Sq9J7S
zU7z/asXcSi9PMzql/3Z/K47azWBZJazpSf6rkfVQzsITcai+CaZvVItCrMq7z94
/poX24CX2MgOxWxv8thRG7jO7nNCT5Smc+wi1j3HXaxbnA3vcAt6yQ6ctXpf1rGI
FwIDAQAB
-----END PUBLIC KEY-----";

$data = 'this is a simple text';

$encryptedData = $crypto->encrypt($publicKey, $data);

echo $encryptedData;

$privateKey='private key';

$encryptedData = 'encrypted data';

$decryptedData = $crypto->decrypt($privateKey, $encryptedData);

echo $decryptedData;

$privateKey = "-----BEGIN PRIVATE KEY-----
MIHuAgEBBEgC1SvKxAMTrXYmC9CV+euaL8KVemuuU6I9A5moUh4HgTzESYt35lgc
CiwMetwIaB9RHFM7869D4rClXvnxFy91nMklcY7IsCmgBwYFK4EEACehgZUDgZIA
BAHBdOduvUMAft3s3xq/70CyHtJTfbFAMmyE6rIOAXYlOcvCvwfLRTbAXkZ+PnU+
5SkSqpC036F02Hr8GNxToie+gLrEAsmjQwLImuN9o+1UPwYR3LdG8N4JsnFkIMYr
qTvrA+usw5pjfZmqE4hHlFVdIaIb7r2m8w2OEZTfsfxQjRzY6uE9P3711NWdZdP2
Rg==
-----END EC PRIVATE KEY-----";

$data = 'this is a simple text';

$signature = $crypto->sign($privateKey, $data);

echo $signature;

$publicKey = "-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAzLBs9Y2+QSr8l5LV8beY
nidHPHhM0/zMy1V7CiTWQyQVyxsZKHQJZGP+zJNovSH+KG9wfUe5XnC7qVDU80Bi
ABfQFhLe+t7w8UnBbRnDxMnLCjoSrtG8ZlJBkfmvTMf4MdmvMUqzqUD+ssB76BOP
Cce7qGntIlhQOzj3crliLex6E4OmwUP9RgPtIz/GQuI+O/XSf6irkkRjE8Sq9J7S
zU7z/asXcSi9PMzql/3Z/K47azWBZJazpSf6rkfVQzsITcai+CaZvVItCrMq7z94
/poX24CX2MgOxWxv8thRG7jO7nNCT5Smc+wi1j3HXaxbnA3vcAt6yQ6ctXpf1rGI
FwIDAQAB
-----END PUBLIC KEY-----";

$signature = 'this is a signature';

$verification = $crypto->encrypt($publicKey, $data);

echo $encryptedData ? 'true' : 'false';