国密 sm2 算法在 php56 环境 thinkphp32 框架下的应用配置
短信预约 -IT技能 免费直播动态提醒
国密算法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_thinkphp32composer installcomposer require mdanter/ecc:0.4.7composer require wzhih/guomi
设置 Application/Common/Conf/config.php
'2CF65879B2C8684BBE6C3D3F295C62F9F84AC6C417D028582538A631A3D1AEF1', 'sm2_publickey' => 'A33337CEA6024821169B0E748FEA46FAE6F2AB65F52D3BE5185E94AB34D7E1EF5A1A4E42C5C386D72AD431DB565D57EE1395E8F1A1B1BCFEDADCC014C02F18C1',);
设置 Application/Common/Common/function.php
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
"; echo "明文: \n
"; echo $str; echo "\n
"; $m2EncryptData = encrypt_sm2($str, $publickey); echo "m2EncryptData: \n
"; echo $m2EncryptData; echo "\n
"; $m2DecryptData = decrypt_sm2($m2EncryptData, $privatekey); echo "m2DecryptData: \n
"; echo $m2DecryptData; echo "\n
"; echo "do sign: \n
"; $sign = sign_sm2($str, $privatekey); print_r($sign); echo "\n
"; echo "verify_sign_sm2: \n
"; var_dump(verify_sign_sm2( $str, $sign, $publickey)); echo "\n
"; exit(); }}
运行结果
明文:你好中国,😁xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxm2EncryptData:58409d2233333976e0fb3a9408d544f4e058356c843c564ae85fd18b4dc6583d3734da6a5331e0cc78214aec42e11c7dcb1064ed2cb230f332914378eabdec71b23dc795aa7a57a4d3bd7ebbd5f9b57e714a51131fdda13b021c513b84e4af9659dae4d7f0b8e59405cf760293c31763a76657ad3ec87ee09efc7274123229954305d7a4770ef3d6bf30057d55c47c9d6fab860d850fd316bec4750ecb3f9ae7d180b151bd89d47fm2DecryptData:你好中国,😁xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxdo sign:MEYCIQCYGBob83sdWiSe+oRPgiSB40s0AWikykv2ZdZAMPBD6wIhAPahW/o6lhoALmtRf2W/6uYo6nwnqmvJ5VH8a+B1rYKVverify_sign_sm2:bool(true)
来源地址:https://blog.csdn.net/leeyisoft/article/details/127551311
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341