当前位置:网站首页>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 .
边栏推荐
- 教程篇(5.0) 10. 故障排除 * FortiEDR * Fortinet 網絡安全專家 NSE 5
- Talk about the design of red envelope activities in e-commerce system
- Have you stepped on the nine common pits in the e-commerce system?
- Transformation of thinking consciousness is the key to the success or failure of digital transformation of construction enterprises
- Learn the knowledge points of eight part essay ~ ~ 1
- Develop fixed asset management system, what voice is used to develop fixed asset management system
- yolov3 训练自己的数据集之生成train.txt
- Novice must see, click two buttons to switch to different content
- Why should we build an enterprise fixed asset management system and how can enterprises strengthen fixed asset management
- #gStore-weekly | gStore源码解析(四):安全机制之黑白名单配置解析
猜你喜欢

MySQL advanced learning summary 7: MySQL data structure - Comparison of hash index, AVL tree, B tree and b+ tree

Transformation of thinking consciousness is the key to the success or failure of digital transformation of construction enterprises

High frequency interview questions

Installation of thingsboard, an open source IOT platform

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

UML class diagram

拦截器与过滤器的区别

Use cheat engine to modify money, life and stars in Kingdom rush

Compile oglpg-9th-edition source code with clion

Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径
随机推荐
以太网PHY层芯片LAN8720A简介
Golang concurrent programming goroutine, channel, sync
移动机器人路径规划:人工势场法[通俗易懂]
[test development] takes you to know what software testing is
日期工具类(不定时更新)
R language ggplot2 visual Facet: gganimate package is based on Transition_ Time function to create dynamic scatter animation (GIF)
Mysql高级篇学习总结8:InnoDB数据存储结构页的概述、页的内部结构、行格式
机器学习笔记 - 时间序列预测研究:法国香槟的月销量
MySQL advanced learning summary 8: overview of InnoDB data storage structure page, internal structure of page, row format
预处理和预处理宏
Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径
2022 software engineering final exam recall Edition
How to copy and paste interlaced in Excel
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
Preprocessing and preprocessing macros
R language uses the lsnofunction function function of epidisplay package to list all objects in the current space, except user-defined function objects
Deep learning mathematics foundation
PHP-Parser羽毛球预约小程序开发require线上系统
R language uses Cox of epidisplay package Display function obtains the summary statistical information of Cox regression model (risk rate HR, adjusted risk rate and its confidence interval, P value of
[0701] [论文阅读] Alleviating Data Imbalance Issue with Perturbed Input During Inference