前段时间接了招行的国密sm2,帮大家避避坑
可以参考github sm2加密源码:https://github.com/lpilp/phpsm2sm3sm4
招商相关Issues:关于招行某些接口的数字信封加密 sm2解密失败问题 · Issue #46 · lpilp/phpsm2sm3sm4 · GitHub
相关扩展:
use Rtgm\sm\RtSm2;use Rtgm\util\FormatSign;
加密:
public function getSign($data){ $sm2 = new RtSm2('base64'); ksort($data); $sign = $sm2->doSign(rawurldecode(http_build_query($data)), $this->privateKey); $sign = trim($this->format_cmbc($sign));// 特殊处理 return $sign;}
解密:
public function checkSign($data){ $sign = array_get($data, 'sign'); unset($data['sign'], $data['_url']); ksort($data); $data = rawurldecode(http_build_query($data)); $data = urldecode($data); $sm2 = new RtSm2('base64'); $sign = (new FormatSign())->run($sign);// 特殊处理 $result = $sm2->verifySign($data, $sign, $this->publicKey); return $result;}
其实很简单,文档和网上好多文章云里雾里,希望大家工作愉快!
来源地址:https://blog.csdn.net/cxs812760493/article/details/130727609