当前位置:网站首页>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内核浏览器。
边栏推荐
- R language ggplot2 visual Facet: gganimate package is based on Transition_ Time function to create dynamic scatter animation (GIF)
- 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
- Excel finds the same value in a column, deletes the row or replaces it with a blank value
- 二进制操作
- 在Tensorflow2中使用mnist_784数据集进行手写数字识别
- 消息队列消息丢失和消息重复发送的处理策略
- R语言dplyr包rowwise函数、mutate函数计算dataframe数据中多个数据列在每行的最大值、并生成行最大值对应的数据列(row maximum)
- #gStore-weekly | gStore源码解析(四):安全机制之黑白名单配置解析
- [Yugong series] July 2022 go teaching course 001 introduction to go language premise
- 2022 compilation principle final examination recall Edition
猜你喜欢

Tips for material UV masking

拦截器与过滤器的区别

Industrial software lecture - core technology analysis of 3D CAD design software - the second lecture of the Forum

PyTorch函数中的__call__和forward函数
![[test development] software testing - concept](/img/24/9ee885d46f7200ae7449957ca96b9d.png)
[test development] software testing - concept

新加坡暑假旅遊攻略:一天玩轉新加坡聖淘沙島

性能测试如何创造业务价值

材质UV遮罩的技巧

消息队列消息丢失和消息重复发送的处理策略

仿京东放大镜效果(pink老师版)
随机推荐
Gstore weekly gstore source code analysis (4): black and white list configuration analysis of security mechanism
Use cheat engine to modify money, life and stars in Kingdom rush
C文件输入操作
Transformation of thinking consciousness is the key to the success or failure of digital transformation of construction enterprises
Installation of thingsboard, an open source IOT platform
Singapore summer tourism strategy: play Singapore Sentosa Island in one day
在纽约寻找童真——新泽西州乐高乐园探索中心的美好一天
全志A33使用主线U-Boot
Leetcode (81) -- search rotation sort array II
[100 cases of JVM tuning practice] 03 -- four cases of JVM heap tuning
Websocket of Web real-time communication technology
The difference between SLC, MLC, TLC and QLC NAND SSD: which is better?
Google's official response: we have not given up tensorflow and will develop side by side with Jax in the future
徹底搞懂基於Open3D的點雲處理教程!
The R language dplyr package rowwise function and mutate function calculate the maximum value of multiple data columns in each row in the dataframe data, and generate the data column (row maximum) cor
聊聊电商系统中红包活动设计
Obligatoire pour les débutants, cliquez sur deux boutons pour passer à un contenu différent
消息队列消息丢失和消息重复发送的处理策略
R语言dplyr包rowwise函数、mutate函数计算dataframe数据中多个数据列在每行的最大值、并生成行最大值对应的数据列(row maximum)
How performance testing creates business value