当前位置:网站首页>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内核浏览器。
边栏推荐
- 使用 Cheat Engine 修改 Kingdom Rush 中的金钱、生命、星
- What is 9D movie like? (+ common sense of dimension space)
- [test development] software testing - concept
- Hongmeng's fourth learning
- ICDE 2023|TKDE Poster Session(CFP)
- 电商系统中常见的 9 大坑,你踩过没?
- Thoroughly understand the point cloud processing tutorial based on open3d!
- Page title component
- Websocket of Web real-time communication technology
- codeforces每日5题(均1700)-第四天
猜你喜欢
Leetcode (81) -- search rotation sort array II
Processing strategy of message queue message loss and repeated message sending
27: Chapter 3: develop Passport Service: 10: [registration / login] interface: after the registration / login is OK, save the user session information (uid, utoken) to redis and cookies; (one main poi
ICDE 2023|TKDE Poster Session(CFP)
Installation of thingsboard, an open source IOT platform
[0701] [paper reading] allowing data imbalance issue with perforated input during influence
ICDE 2023|TKDE Poster Session(CFP)
开源物联网平台ThingsBoard的安装
Introduction to the paper | analysis and criticism of using the pre training language model as a knowledge base
Transformation of thinking consciousness is the key to the success or failure of digital transformation of construction enterprises
随机推荐
Websocket of Web real-time communication technology
Progress progress bar
M2DGR:多源多场景 地面机器人SLAM数据集(ICRA 2022 )
页面标题组件
2022软件工程期末考试 回忆版
论文导读 | 关于将预训练语言模型作为知识库的分析与批评
[daily question] first day
Hospital online inquiry source code hospital video inquiry source code hospital applet source code
High frequency interview questions
Excel查找一列中的相同值,删除该行或替换为空值
ICDE 2023|TKDE Poster Session(CFP)
Masa framework - DDD design (1)
R语言使用epiDisplay包的lrtest函数对多个glm模型(logisti回归)执行似然比检验(Likelihood ratio test)对比两个模型的性能是否有差异、广义线性模型的似然比检
UML 类图
【JVM调优实战100例】02——虚拟机栈与本地方法栈调优五例
性能测试如何创造业务价值
Exness in-depth good article: dynamic series - Case Analysis of gold liquidity (V)
R语言ggplot2可视化:gganimate包创建动态柱状图动画(gif)、使用transition_states函数在动画中沿给定维度逐步显示柱状图
SQL training 2
Excel如何进行隔行复制粘贴