PHP code example of piggly / php-encryption

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

    

piggly / php-encryption example snippets


// Chaves
$keys = BasicCrypto::createKeys();
// <- salve $keys['secret_key'] em um arquivo e leia-o posteriormente em uma constante, ambiente ou afins...

// Inicialize as chaves
BasicCrypto::setKeys( SECRET_KEY );

// ...
// Mais tarde no código
// ...
$encrypted = BasicCrypto::encrypt('this is my secret message');
$decrypted = BasicCrypto::decrypt($encrypted);

if ( $descrypted !== false )
{ /** O conteúdo foi descriptografado */ }

// Chaves
$keys = SodiumCrypto::createKeys();
// <- salve todas as chaves em um arquivo e leia-o posteriormente em uma constante, ambiente ou afins...

// Inicialize as chaves
SodiumCrypto::setKeys( 
	PUBLIC_KEY,
	PRIVATE_KEY,
	SIGNATURE_PUBLIC_KEY,
	SIGNATURE_PRIVATE_KEY 
);

// ...
// Mais tarde no código
// ...
$encrypted = SodiumCrypto::encrypt('this is my secret message');
$decrypted = SodiumCrypto::decrypt($encrypted);

if ( $descrypted !== false )
{ /** O conteúdo foi descriptografado */ }

// Para criação de assinaturas
$signedMessage  = SodiumCrypto::sign('i am signed');
$validSignature = SodiumCrypto::checkSignature($signedMessage);

if ( $validSignature !== false )
{ /** O conteúdo é autentico */ }

$signedMessage  = SodiumCrypto::sign('i am signed');
$encrypted = SodiumCrypto::encrypt($signedMessage);
$decrypted = SodiumCrypto::decrypt($encrypted);
$validSignature = SodiumCrypto::checkSignature($decrypted);

if ( $validSignature !== false )
{ /** O conteúdo é autentico */ }