PHP code example of davodm / dgcrypt-php

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

    

davodm / dgcrypt-php example snippets


use Dgcrypt\Dgcrypt;

// Initialize the Dgcrypt class with the desired encryption method
$dgcrypt = new Dgcrypt('aes-256-gcm'); // Can be 'aes-256-cbc', 'aes-256-gcm', or 'chacha20-poly1305'

// Set a secret key
$dgcrypt->setKey('your-secret key');

// Encrypt a string
$plainText = 'Hello, World!';
$encrypted = $dgcrypt->encrypt($plainText);

// Output the encrypted string
echo $encrypted;

// Decrypt the previously encrypted string
$decrypted = $dgcrypt->setCipherMethod('aes-256-cbc')->decrypt($encrypted);

// Output the decrypted string
echo $decrypted;

// Generate a secure random key
$generatedKey = $dgcrypt->generateKey();

// Output the generated key
echo $generatedKey; // Display the hexadecimal key

// Optionally, set a custom IV (12 bytes for GCM or ChaCha20, 16 bytes for CBC)
// If no IV is set, a secure random IV will be generated automatically
$dgcrypt->setIV();
bash
composer