当前位置:网站首页>PHP非对称加密方法私钥及公钥加密解密的方法
PHP非对称加密方法私钥及公钥加密解密的方法
2022-07-02 17:59:00 【游戏编程】
私钥公钥获取方式如下:
①. 访问在线生成非对称加密公钥私钥对: http://web.chacuo.net/netrsakeypair
点击生成密钥对,删除RSA生成格式-> BEGIN RSA PRIVATE KEY 和 END PUBLIC KEY ,即第一行和最后一行。
②. 访问在线字符串文本压缩工具,删除文本中的所有新行、制表键、空格、回车、换行 字符: http://www.ab173.com/other/compress.php
将生成的公钥和私钥复制到压缩文本框中去除换行和空格
③. 把公钥上传至商户后台,平台会使用该公钥进行解密并且验签。
④. 私钥自己隐秘保存(加密用)。
<?php/** * Description: 平台公钥解密获取数据(验签) * Date: 2022/7/1 * @param $data */public function decrypt($data){ ksort($data); $toSign = ''; foreach ($data as $key => $value) { if (strcmp($key, 'sign') != 0 && $value != '') { $toSign .= $key . '=' . $value . '&'; } } $str = rtrim($toSign, '&'); $encrypted = ''; // 公钥 $pem = chunk_split($this->platform_public_key, 64, "\n"); $pem = "-----BEGIN PUBLIC KEY-----\n" . $pem . "-----END PUBLIC KEY-----\n"; $publickey = openssl_pkey_get_public($pem); $base64 = str_replace(array('-', '_'), array('+', '/'), $data['sign']); $crypto = ''; foreach (str_split(base64_decode($base64), 128) as $chunk) { openssl_public_decrypt($chunk, $decrypted, $publickey); $crypto .= $decrypted; } if ($str != $crypto) { // 验签失败 record_log([ 'msg' => '验签失败', 'data' => [ 'params' => $data, 'str' => $str, 'crypto' => $crypto, ], ], 'globalpay_payin_error_'); exit('Signature verification failed'); }}/** * Description: 私钥加密生成签名 * Date: 2022/7/1 * @param $data * @return mixed */public function encrypt($data){ ksort($data); $str = ''; foreach ($data as $k => $v) { if (!empty($v)) { $str .= (string)$k . '=' . $v . '&'; } } $str = rtrim($str, '&'); $encrypted = ''; // 私钥 $pem = chunk_split($this->mch_private_key, 64, "\n"); $pem = "-----BEGIN PRIVATE KEY-----\n" . $pem . "-----END PRIVATE KEY-----\n"; $private_key = openssl_pkey_get_private($pem); $crypto = ''; foreach (str_split($str, 117) as $chunk) { openssl_private_encrypt($chunk, $encryptData, $private_key); $crypto .= $encryptData; } $encrypted = base64_encode($crypto); $encrypted = str_replace(array('+', '/', '='), array('-', '_', ''), $encrypted); $data['sign'] = $encrypted; return $data;}作者:LordForce
游戏编程,一个游戏开发收藏夹~
如果图片长时间未显示,请使用Chrome内核浏览器。
边栏推荐
- Introduction to sap s/4hana OData mock service
- 【测试开发】软件测试—概念篇
- 【JVM调优实战100例】02——虚拟机栈与本地方法栈调优五例
- 第一次去曼谷旅游怎么玩?这份省钱攻略请收好
- Yolov3 trains its own data set to generate train txt
- 中国信通院《数据安全产品与服务图谱》,美创科技实现四大板块全覆盖
- R language uses lrtest function of epidisplay package to perform likelihood ratio test on multiple GLM models (logisti regression). Compare whether the performance of the two models is different, and
- [论文阅读] CA-Net: Leveraging Contextual Features for Lung Cancer Prediction
- Page title component
- 高频面试题
猜你喜欢

为什么要做企业固定资产管理系统,企业如何加强固定资产管理

Comprendre complètement le tutoriel de traitement de Point Cloud basé sur open3d!

What is 9D movie like? (+ common sense of dimension space)

【每日一题】第一天

医院在线问诊源码 医院视频问诊源码 医院小程序源码

Novice must see, click two buttons to switch to different content

Excel如何进行隔行复制粘贴

Excel finds the same value in a column, deletes the row or replaces it with a blank value

Yolov3 trains its own data set to generate train txt

How can retail enterprises open the second growth curve under the full link digital transformation
随机推荐
教程篇(5.0) 10. 故障排除 * FortiEDR * Fortinet 网络安全专家 NSE 5
Exness in-depth good article: dynamic series - Case Analysis of gold liquidity (V)
Kubernetes three open interfaces first sight
R语言使用epiDisplay包的lsNoFunction函数列出当前空间中的所有对象、除了用户自定义的函数对象
How to play when you travel to Bangkok for the first time? Please keep this money saving strategy
材质UV遮罩的技巧
Stratégie touristique d'été de Singapour: un jour pour visiter l'île de San taosha à Singapour
【测试开发】软件测试—概念篇
PyTorch函数中的__call__和forward函数
[daily question] the next day
Introduction to the paper | application of machine learning in database cardinality estimation
Mini Golf Course: a good place for leisure and tourism in London
ICDE 2023|TKDE Poster Session(CFP)
"Patient's family, please come here" reading notes
ICDE 2023|TKDE Poster Session(CFP)
How to enable the run dashboard function of idea
MySQL advanced learning summary 7: MySQL data structure - Comparison of hash index, AVL tree, B tree and b+ tree
Google's official response: we have not given up tensorflow and will develop side by side with Jax in the future
M2dgr: slam data set of multi-source and multi scene ground robot (ICRA 2022)
The difference between interceptor and filter