当前位置:网站首页>PHP asymmetric encryption method private key and public key encryption and decryption method
PHP asymmetric encryption method private key and public key encryption and decryption method
2022-07-02 19:17:00 【Game programming】
The private key and public key are obtained as follows :
①. Access online to generate asymmetric encrypted public and private key pairs : http://web.chacuo.net/netrsakeypair
Click generate key pair , Delete RSA Generate format -> BEGIN RSA PRIVATE KEY and END PUBLIC KEY , That is, the first and last lines .
②. Access the online string text compression tool , Delete all new lines in the text 、 Tabulation key 、 Space 、 enter 、 Line break character : http://www.ab173.com/other/compress.php
Copy the generated public and private keys into the compressed text box to remove line breaks and spaces
③. Upload the public key to the merchant background , The platform will use the public key to decrypt and verify the signature .
④. The private key is kept secretly ( Encryption uses ).
<?php/** * Description: The platform decrypts the public key to obtain data ( attestation ) * 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 = ''; // Public key $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) { // Attestation of failure record_log([ 'msg' => ' Attestation of failure ', 'data' => [ 'params' => $data, 'str' => $str, 'crypto' => $crypto, ], ], 'globalpay_payin_error_'); exit('Signature verification failed'); }}/** * Description: Private key encryption generates signature * 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 = ''; // Private key $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;}author :LordForce
Game programming , A game development favorite ~
If the picture is not displayed for a long time , Please use Chrome Kernel browser .
边栏推荐
- ICDE 2023|TKDE Poster Session(CFP)
- 日期工具类(不定时更新)
- [paper reading] Ca net: leveraging contextual features for lung cancer prediction
- Golang并发编程——goroutine、channel、sync
- 机器学习笔记 - 时间序列预测研究:法国香槟的月销量
- R language uses the lsnofunction function function of epidisplay package to list all objects in the current space, except user-defined function objects
- 开发固定资产管理系统,开发固定资产管理系统用什么语音
- MySQL高级(进阶)SQL语句
- Gstore weekly gstore source code analysis (4): black and white list configuration analysis of security mechanism
- R语言ggplot2可视化分面图(facet):gganimate包基于transition_time函数创建动态散点图动画(gif)
猜你喜欢

Tutorial (5.0) 10 Troubleshooting * fortiedr * Fortinet network security expert NSE 5

机器学习笔记 - 时间序列预测研究:法国香槟的月销量

The difference between interceptor and filter

Deep learning mathematics foundation
![[论文阅读] CA-Net: Leveraging Contextual Features for Lung Cancer Prediction](/img/ef/bb48ee88d5dc6fe876a498ab53106e.png)
[论文阅读] CA-Net: Leveraging Contextual Features for Lung Cancer Prediction

【测试开发】软件测试—概念篇

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

教程篇(5.0) 10. 故障排除 * FortiEDR * Fortinet 網絡安全專家 NSE 5

ICDE 2023|TKDE Poster Session(CFP)

Juypter notebook modify the default open folder and default browser
随机推荐
Processing strategy of message queue message loss and repeated message sending
FastDFS安装
【JVM调优实战100例】01——JVM的介绍与程序计数器
Progress-进度条
STM32G0 USB DFU 升级校验出错-2
9D电影是怎样的?(+维度空间常识)
【ERP软件】ERP体系二次开发有哪些危险?
Excel如何进行隔行复制粘贴
[paper reading] Ca net: leveraging contextual features for lung cancer prediction
2022软件工程期末考试 回忆版
How performance testing creates business value
Reduce -- traverse element calculation. The specific calculation formula needs to be passed in and combined with BigDecimal
教程篇(5.0) 10. 故障排除 * FortiEDR * Fortinet 網絡安全專家 NSE 5
[0701] [paper reading] allowing data imbalance issue with perforated input during influence
Gstore weekly gstore source code analysis (4): black and white list configuration analysis of security mechanism
Binary operation
Compile oglpg-9th-edition source code with clion
Stm32g0 USB DFU upgrade verification error -2
C文件输入操作
M2dgr: slam data set of multi-source and multi scene ground robot (ICRA 2022)