PHP code example of evit / php-gm-crypto

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

    

evit / php-gm-crypto example snippets



// If you are using a framework that does not support psr-4 autoloader, you need to explicitly import package from the vendor directory.
 => 'cbc',
    // password, will be processed by substr(md5($key), 0, 16) if $config['hash']
    'key'   => '{replace-your-key-here}',
    // the iv used by 'cbc' mode, will be will be processed by substr(md5($iv), 0, 16) if $config['hash']
    'iv'    => '{replace-your-iv-here}',
    // weather do md5 to key and iv or not
    'hash'  => false
];

$sm4 = new EvitSM4Encryption($config);

// Encrypt
$start = microtime(true);
$encypted = $sm4->sm4encrypt('{replace-your-plaintext-here}');
$end = microtime(true);
var_dump('Encrypt time elapsed:' . number_format($end - $start, 8) . ' s');
var_dump("Cipher text:{$encypted}");

// Decrypt
$decStart = microtime(true);
$decrypted = $sm4->sm4decrypt($encypted);
$end = microtime(true);
var_dump('Decrypt time elapsed:' . number_format($end - $decStart, 8) . ' s');
var_dump("Plain text:{$decrypted}");

// Determine whether openssl library is used
$algorithm = $sm4->isOpenssl() ? 'openssl' : 'php-gm-crypto';
var_dump("Algrithm is:{$algorithm}");
var_dump('Total time elapsed:' . number_format($end - $start, 8) . ' s');
composer 
bash
composer 
 php
// If you are using a framework that does not support psr-4 autoloader, you need to explicitly import package from the vendor directory.
"SM3 hash result of '{$input}' is '{$output}'");