PHP code example of krak / crypto

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

    

krak / crypto example snippets




use Krak\Crypto;

$key = random_bytes(16);
$hmac_key = random_bytes(16);

$crypt = new Crypto\OpenSSLCrypt($key);
$crypt = new Crypto\Base64Crypt(new Crypto\HmacCrypt($crypt, $hmac_key));

$encrypted = $crypt->encrypt('data');
echo $crypt->decrypt($encrypted);
// outputs: data



use Krak\Crypto;

$pad = new Crypto\Pkcs7Pad();
$padded = $pad->pad('abc');
echo $pad->strip($padded);
// outputs: abc



$crypt = new Krak\Crypt\GnuPGCliCrypt('User Name', $passphrase = 'secret', $gpg_executable_path = 'gpg');



use Krak\Crypto;

$stream = Crypto\str_stream('this is some data'); // create a stream from raw string.
$stream = new Crypto\StreamPipe($stream);

$crypt_stream = new Crypto\Stream\CryptStream(new Crypto\OpenSSLCrypt($key), 16); // encrypt/decrypt 16 byte chunks at a time
$base64_stream = new Crypto\Stream\Base64Stream(64); // encode/decode 64 byte chunks at a time

$key = random_bytes(16);
$dst = fopen('php://stdout', 'w');
$stream->pipe(Crypto\map_stream('strtoupper'))
    ->pipe($crypt_stream->encrypt())
    ->pipe($base64_stream->encode())
    ->pipe(Crypt\write_stream($dst));
// at this point, stdout will have encrypted uppercased info.