Bootstrap

国密 sm2 算法在 php56 环境 thinkphp32 框架下的应用配置

国密算法sm2在php56环境thinkphp32框架下的应用配置

本人在CentOS7 CentOS8 上面使用宝塔面板安装php5.6成功通过

注意 :

  • 需要 gmp 扩展
  • 在线生成 sm2公钥私钥对 https://const.net.cn/tool/sm2/genkey/
  • 依赖项目 https://github.com/wzhih/guomi
  • please make sure you upgrade to Composer 2+
git clone https://gitee.com/liu21st/thinkphp32.git sm2_thinkphp32
composer install
composer require mdanter/ecc:0.4.7
composer require wzhih/guomi

设置 Application/Common/Conf/config.php

<?php
return array(
    // https://const.net.cn/tool/sm2/genkey/ 
    'sm2_privatekey' => '2CF65879B2C8684BBE6C3D3F295C62F9F84AC6C417D028582538A631A3D1AEF1',
    'sm2_publickey' => 'A33337CEA6024821169B0E748FEA46FAE6F2AB65F52D3BE5185E94AB34D7E1EF5A1A4E42C5C386D72AD431DB565D57EE1395E8F1A1B1BCFEDADCC014C02F18C1',
);

设置 Application/Common/Common/function.php

<?php

require_once APP_PATH . '/../vendor/autoload.php';

//
function encrypt_sm2($str, $publickey)
{
    $sm2 = new \Rtgm\sm\RtSm2();
    $m2EncryptData = $sm2 ->doEncrypt($str, $publickey);
    return $m2EncryptData;
}

function decrypt_sm2($m2EncryptData, $privatekey)
{
    $sm2 = new \Rtgm\sm\RtSm2();
    $m2DecryptData = $sm2 ->doDecrypt($m2EncryptData, $privatekey);
    return $m2DecryptData;
}

function sign_sm2($document, $privatekey)
{
    $sm2 = new \Rtgm\sm\RtSm2('base64');
    return $sm2 ->doSign($document, $privatekey);
}

function verify_sign_sm2($document, $sign, $publickey)
{
    $sm2 = new \Rtgm\sm\RtSm2('base64');
    return $sm2 ->verifySign($document, $sign, $publickey);
}

在控制器里面代码

http://localhost/home/index/sm2

<?php
namespace Home\Controller;

use Think\Controller;

class IndexController extends Controller
{

    public function phpinfo()
    {
        echo phpinfo();
        exit();
    }

    public function sm2()
    {
        $privatekey = C('sm2_privatekey');
        $publickey = C('sm2_publickey');
        $str = "你好中国,😁xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; echo "\n<br/>";
        echo "明文: \n<br/>"; echo $str; echo "\n<br/>";
        $m2EncryptData = encrypt_sm2($str, $publickey);

        echo "m2EncryptData: \n<br/>";
        echo $m2EncryptData; echo "\n<br/>";

        $m2DecryptData = decrypt_sm2($m2EncryptData, $privatekey);

        echo "m2DecryptData: \n<br/>";
        echo $m2DecryptData; echo "\n<br/>";

        echo "do sign: \n<br/>";
        $sign = sign_sm2($str, $privatekey);
        print_r($sign); echo "\n<br/>";

        echo "verify_sign_sm2: \n<br/>";
        var_dump(verify_sign_sm2( $str, $sign, $publickey));
        echo "\n<br/>";
        exit();
    }
}

运行结果

明文:
你好中国,😁xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
m2EncryptData:
58409d2233333976e0fb3a9408d544f4e058356c843c564ae85fd18b4dc6583d3734da6a5331e0cc78214aec42e11c7dcb1064ed2cb230f332914378eabdec71b23dc795aa7a57a4d3bd7ebbd5f9b57e714a51131fdda13b021c513b84e4af9659dae4d7f0b8e59405cf760293c31763a76657ad3ec87ee09efc7274123229954305d7a4770ef3d6bf30057d55c47c9d6fab860d850fd316bec4750ecb3f9ae7d180b151bd89d47f
m2DecryptData:
你好中国,😁xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
do sign:
MEYCIQCYGBob83sdWiSe+oRPgiSB40s0AWikykv2ZdZAMPBD6wIhAPahW/o6lhoALmtRf2W/6uYo6nwnqmvJ5VH8a+B1rYKV
verify_sign_sm2:
bool(true)
;