1.安装两个依赖
//sm3 sm4 xiuchuan/ecc //要求php7.4 //sm2 lpilp/splsm2
2.测试
<?php include './vendor/autoload.php'; use Xiuchuan\Ecc\MySm2; use Xiuchuan\Ecc\MySm3; use Xiuchuan\Ecc\MySm4; use Lpilp\Splsm2\smecc\SPLSM2\SimpleSm2; //生成证书 $a = new MySm2(); $b = $a->generatekey(); var_dump($b); $pri_key = $b[0]; $pub_key = $b[1]; $str = str_repeat('abc',20); $str2 = $a->doSign($str,$pri_key); var_dump($str2); //sm3可用 $str = 'aaaa'; $sm3 = new MySm3(); $sign = $sm3->digest($str); var_dump($sign); //sm4可用 $sm4 = new MySm4(); $key = md5(1); $file = file_get_contents('./api.pdf'); $sm4key = $sm4->setKey($key); $enc = $sm4key->encryptData($file); var_dump($enc); $dec = $sm4key->decryptData($enc); file_put_contents('333.pdf',$dec); //lpilp sm2测试 // $publicKey = '0470f2d14e2e7131e6fe4c00c7d9b4ab19f41bdc6a7fa9c59120e9ba2d3aa43af6b65cf57cbf8e9a296bcdd223ba908d3312dfcab26730f97250cb27c8c3bd5030'; $privateKey = 'a95470032827e012ba6166b2dc876b800821f67942d0aca7ea33b0c0d2ec4403'; $userId = '1234567812345678'; $document = "我爱你ILOVEYOU!"; $ssm2 = new SimpleSm2($privateKey,$publicKey); // 不随机变,就是固定的文本,固定的密钥,生成的固定的签名, // 不设置的话缺省是true, 每次的签名都不一样,看对方的需求,有些银行要求每次都变 // $ssm2->set_rand_sign_flag(false); $ssm2->set_fix_foreignkey_flag(true); $document = '我爱你ILOVEYOU!'; $ed = $ssm2->encrypt($publicKey,$document); var_dump($ed); $val = $ssm2->decrypt($privateKey,$ed); var_dump($val);
本文为看恩吧原创文章,转载无需和我联系,但请注明来自knsay.com