PHP code example of moleft / data-encrypt

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

    

moleft / data-encrypt example snippets


$config_option = [
    'format_type' => 'file',
    'private_key' => dirname(__FILE__).'/test_key/private_key',
    'public_key'  => dirname(__FILE__).'/test_key/public_key',
];

use MoLeft\DataCrypt\Config;

$config =  new Config($config_option);

use MoLeft\DataCrypt\Config;
use MoLeft\DataCrypt\DataEncryptException;

try{
    $config =  new Config($config_option);
}catch(DataEncryptException $e){
    die($e->getMessage());
}

// 假设这是返回数据
$data = json_encode(['ret' => '1','msg' => '登陆成功']);
// 假设这是参数
$parm = [
    'act' => 'login',
    'user' => 'root',
    'pass' => 'root'
];

use MoLeft\DataCrypt\Encrypt;

// 实例化一个加密对象
$encrypt = new Encrypt($config);
// 加密,签名,返回加密好的数据
$encrypt_data = $encrypt->encrypt($data)->sign($parm)->json();

use MoLeft\DataCrypt\Decrypt;

// 实例化一个解密对象
$decrypt = new Decrypt($config);
// 设置解密数据 验签 返回解密数据
$decrypt_data = $decrypt->data($encrypt_data)->verify($parm)->decrypt();

原始数据:
{"ret":"1","msg":"\u767b\u9646\u6210\u529f"}


加密数据:
{"data":"Suj8S9srh50P74FIO4s9dHDWvYSB+TxNhrg68OP4mqTeQ9UezO2CZPQ\/Mqzk9r0TUJ\/VReDqxtwrP4O8K6GlmpgyW\/RTXMPy5rGsv1K\/RD45LMPVr1VJyiyKDqjNLZ+D8SLFwNi5dn1uukGqeTO6IzcD\/vf4WuYQ7xq\/yV0hF1e+gh4GkYSQczFWPJNtwWHRDi8Q0rOu48tPbb\/5poduKyObh7CZDiQ3OWuXyS7b2KMMGAL9BM3p0Juzq19QmfSYFl3uBzNu+\/N7LuHhy3egMYNCHpYPkr7xcT9Ra7muaMuTMcJPKSv\/APark3cJnl5ZA+HzJXrDEFJjNIgRAeaBXQ==","sign":"JClGoEWdwIAfUSnIPv1eaqzH7ETC6eeq0c3smEeLq4FkOtEQB\/Q3qLww76ZqWy1Dc4ubyB7eTjGiKouJdusRq0z4glBz0Vze1ZRlQMYVydmUiL4KlCE25fHwyfDTE2KTQs4ZG1bSfe1ENz\/twaLlvBJwhATsoiq7oV1YUAWeAinpREkkdSQL+N1KzkDuIp9JP73b0r3n5g2Rx1QINS1\/T6VIsA2wkqNCHm+43y2A928CCkQdve7VFk\/rL71+XdKWzYRoU+Xde1NPEDOuWP7SzV\/3+DdBwmUjahS\/pIBq0twueQYstPSSd7GU2Lmh\/92+y1SKamW2lIx1kmHcwgcZmA=="}


解密数据:
{"ret":"1","msg":"\u767b\u9646\u6210\u529f"}