seffeng/cryptlib

PHP extension cryptlib

v0.1.3 2023-08-02 07:59 UTC

This package is auto-updated.

Last update: 2024-05-01 00:17:06 UTC


README

安装

# 安装
$ composer require seffeng/cryptlib

目录说明

├─src
│  │  Crypt.php
│  │  openssl.cnf
│  ├─Clients
│  │    RSA.php
│  │    SM3.php
│  │    SM4.php
│  ├─Exceptions
│  │    CryptException.php
│  └─Interfaces
│       CryptInterface.php
└─tests
    CryptTest.php

方法

$crypt = new Crypt();
# 1、生成 KEY,保存秘钥对
$keys = $crypt->createKey();
# 1、加密
$crypt->loadKey($publicKey);
$crypt->encrypt($plaintext);

# 2、解密
$crypt->loadKey($privateKey);
$crypt->decrypt($entext);

# 3、签名
$crypt->loadKey($privateKey);
$crypt->sign($message);

# 4、签名验证
$crypt->loadKey($publicKey);
$crypt->verify($message, $sign);

# 5、SM3
$crypt = new Crypt('SM3');
$crypt->encrypt('abcd');

# 6、SM4
$crypt = new Crypt('SM4');
$iv = 'EZIwtOeuqf8BI/j3D0CjuQ==';//$crypt->createKey();
$plaintext = '123456';
$entext = $crypt->setIv($iv)->encrypt($plaintext);
$detext = $crypt->setIv($iv)->decrypt($entext);

示例

/**
 * SiteController
 */
use Seffeng\Cryptlib\Crypt;

class SiteController extends Controller
{
    public function index()
    {
        $crypt = new Crypt();
        // 生成 KEY
        $keys = $crypt->createKey();
        $privateKey = isset($keys['privateKey']) ? $keys['privateKey'] : null;
        $publicKey = isset($keys['publicKey']) ? $keys['publicKey'] : null;
        //$crypt->setPrivateKey($privateKey)->setPublicKey($publicKey);
        $crypt->setEncryptionMode(1);

        $plaintext = '123456';
        // 加密[公钥]
        $entext = $crypt->loadKey($publicKey)->encrypt($plaintext);
        // 解密[私钥]
        $detext = $crypt->loadKey($privateKey)->decrypt($entext);
        var_dump(base64_encode($entext), $detext);

        //$crypt = new Crypt();
        //$keys = $crypt->createKey();
        //$privateKey = isset($keys['privateKey']) ? $keys['privateKey'] : null;
        //$publicKey = isset($keys['publicKey']) ? $keys['publicKey'] : null;

        $plaintext = '654321';
        // 加密[私钥]
        $entext = $crypt->loadKey($privateKey)->encryptByPrivateKey($plaintext);
        // 解密[公钥]
        $detext = $crypt->loadKey($publicKey)->decryptByPublicKey($entext);
        var_dump(base64_encode($entext), $detext);

        //$crypt = new Crypt();
        //$keys = $crypt->createKey();
        //$privateKey = isset($keys['privateKey']) ? $keys['privateKey'] : null;
        //$publicKey = isset($keys['publicKey']) ? $keys['publicKey'] : null;

        $message = 'a=aaa&b=bbb&c=ccc';
        // 签名[私钥]
        $sign = $crypt->loadKey($privateKey)->sign($message);
        // 签名验证[公钥]
        $verify = $crypt->loadKey($publicKey)->verify($message, $sign);

        var_dump(base64_encode($sign), $verify);
    }
}

备注