当前位置:网站首页>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 .
边栏推荐
- R语言使用epiDisplay包的lsNoFunction函数列出当前空间中的所有对象、除了用户自定义的函数对象
- 《病人家属,请来一下》读书笔记
- R language dplyr package filter function filters dataframe data. If the name of the data column (variable) to be filtered contains quotation marks, you need to use!! SYM syntax processing, otherwise n
- 全链路数字化转型下,零售企业如何打开第二增长曲线
- 移动机器人路径规划:人工势场法[通俗易懂]
- R语言ggplot2可视化:可视化折线图、使用labs函数为折线图添加自定义的X轴标签信息
- mybatiesHelperPro工具必须的可以生成到对应项目文件夹下
- How to play when you travel to Bangkok for the first time? Please keep this money saving strategy
- Installation of thingsboard, an open source IOT platform
- 论文导读 | 机器学习在数据库基数估计中的应用
猜你喜欢

M2dgr: slam data set of multi-source and multi scene ground robot (ICRA 2022)

Mysql高级篇学习总结7:Mysql数据结构-Hash索引、AVL树、B树、B+树的对比

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

MySQL advanced learning summary 8: overview of InnoDB data storage structure page, internal structure of page, row format

According to the atlas of data security products and services issued by the China Academy of information technology, meichuang technology has achieved full coverage of four major sectors

In pytorch function__ call__ And forward functions

Hospital online inquiry source code hospital video inquiry source code hospital applet source code

全链路数字化转型下,零售企业如何打开第二增长曲线

Introduction to the paper | analysis and criticism of using the pre training language model as a knowledge base

论文导读 | 机器学习在数据库基数估计中的应用
随机推荐
Tutorial (5.0) 09 Restful API * fortiedr * Fortinet network security expert NSE 5
2022 compilation principle final examination recall Edition
ICDE 2023|TKDE Poster Session(CFP)
[论文阅读] CA-Net: Leveraging Contextual Features for Lung Cancer Prediction
全志A33使用主线U-Boot
Obligatoire pour les débutants, cliquez sur deux boutons pour passer à un contenu différent
High frequency interview questions
使用CLion编译OGLPG-9th-Edition源码
R language ggplot2 visualization: visualize the line chart and add customized X-axis label information to the line chart using labs function
Emmet基础语法
Memory management of C
R语言使用epiDisplay包的cox.display函数获取cox回归模型汇总统计信息(风险率HR、调整风险率及其置信区间、模型系数的t检验的p值、Wald检验的p值和似然比检验的p值)、汇总统计
从list转化成map的时候,如果根据某一属性可能会导致key重复而异常,可以设置处理这种重复的方式
Mysql高级篇学习总结6:索引的概念及理解、B+树产生过程详解、MyISAM与InnoDB的对比
yolov3 训练自己的数据集之生成train.txt
高频面试题
R language ggplot2 visualization: gganimate package creates dynamic histogram animation (GIF) and uses transition_ The States function displays a histogram step by step along a given dimension in the
Introduction to the paper | analysis and criticism of using the pre training language model as a knowledge base
Markdown basic grammar
2022编译原理期末考试 回忆版